Question on NIO

How Repeater arechitecture in NIO detect a new client has come for the request?

What's a "repeater architecture" in this context? (ie: what's it got to do with Java networking?)
Are you looking for information on how to use the NIO API, or are you trying to figure out how the NIO API works?
This is where I'd start for learning to use the API:
http://java.sun.com/j2se/1.5.0/docs/guide/nio/
The examples are of particular interest.

Similar Messages

  • JRockit & java.nio

    Hello
    I have a question regarding .nio API in JRokit SDK. Is it implemented there at
    all? We are trying to test a connector application on top of WLS 8.1 and with
    SUN 1.4.1 SDK it works fine, but when we try to use JRocket SDK it gives an error
    during the Selector.open() method call (creation of a selector with a default
    system provider).
    So is the question is: is this feature supported or not? Can it be due to configuration
    settings (and which ones)?
    BR
    VA

    Thats funny back in Nov
    Redhat was in touting RH AS 3.0 and Weblogic 8.1 being certified by BEA.
    As the reps said
    "The new POSIX threading gives a tremendous increase in performance for the JDK"
    Staffan Larsen <[email protected]> wrote:
    JRockit 8.1 is not supported on RHEL 3.0. If you want to try running
    it
    you have to set LD_ASSUME_KERNEL=2.4.1
    /Staffan
    Valeri Atamaniouk wrote:
    Hello again
    Well, we have tried. But before that we have upgraded the system toRed Hat Enterprise
    Linux 3.0 AS edition. Now JRockit just gives core dumps. Right whenthe NodeManager
    is started.
    Files are submitted to [email protected] Hopefully there will be a positive
    response
    If nothing helps we will try to set install RH EL 2.1 back. Unfortunatelythere
    are also some problems with the application server itself, but thatgoes to different
    topic.
    BR
    VA
    BR
    VA
    "Sathish Santhanam" <[email protected]> wrote:
    Can you please try JRockit 81SP1? I remember one similar which has
    been
    fixed in this release.
    http://commerce.bea.com/showallversions.jsp?family=WLJR
    Sathish Santhanam
    Developer Relations Engineer
    BEA Support
    "Valeri Atamaniouk" <[email protected]> wrote in message
    news:3f99284e$[email protected]..
    An update to the question above.
    The implementation works fine on Windows NT, but the problem ariseswith
    Linux
    version. We are currently using Red Hat Advanced Server 2.1.
    If we try to start up a standalone J2SE application over JRockit,
    then
    we
    get
    a message: "/dev/poll unavailable". Probably that would help.
    BR
    VA

  • SSLSocket on client & NIO w/ SSLEngine on server

    Hi, I have a question about NIO & SSL...
    Does it make sense to use SSLSocket on the client-side to connect to a server, which uses NIO w/ SSLEngine?
    In my current code the server side is set up to be non-blocking. The handshaking part on the client side seems to work fine with the server's NIO & SSLEngine code, but after that it behaves oddly on the first read for application data (on the server-side). My SSLEngineResult from the unwrap of the first read has zero bytes for both produced and consumed. Not sure why...any thoughts?

    Thanks for the replies.
    I'm using the sample code that comes with the Java 5.0 SE. Although, I modified it a bit (the example is set up for an HTTP server).
    The read method looks like this:
    int read() throws IOException
            SSLEngineResult result;
            if (!initialHSComplete)
                throw new IllegalStateException();
            int pos = requestBB.position();
            if (sc.read(inNetBB) == -1)
                sslEngine.closeInbound();  // probably throws exception
                return -1;
            do
                resizeRequestBB();    // guarantees enough room for unwrap
                inNetBB.flip();
                result = sslEngine.unwrap(inNetBB, requestBB);
                inNetBB.compact();
                * Could check here for a renegotation, but we're only
                * doing a simple read/write, and won't have enough state
                * transitions to do a complete handshake, so ignore that
                * possibility.
                switch (result.getStatus())
                    case BUFFER_UNDERFLOW:
                    case OK:
                        if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK)
                            doTasks();
                        break;
                    default:
                        throw new IOException("sslEngine error during data read: " +
                                result.getStatus());
            } while ((inNetBB.position() != 0) &&
                    result.getStatus() != Status.BUFFER_UNDERFLOW);
            return (requestBB.position() - pos);
        }It returns from that method with the Status.BUFFER_UNDERFLOW.

  • Easy way to non-blocked sockets

    Use JSSE and NIO for a quick way to implement non-blocking communications
    October 22, 2003
    Although SSL blocking operations -- in which the socket is blocked from access while data is being read from or written to -- provide better I/O-error notification than the non-blocking counterpart, non-blocking operations allow the calling thread to continue. In this article, the author will cover both the client and server side as he describes how to create non-blocking secure connections using the Java Secure Socket Extensions (JSSE) and the Java NIO (new I/O) library, and he will explain the traditional approach to creating a non-blocking socket, as well as an alternative (and necessary) method if you want to use JSSE with NIO.
    http://www-106.ibm.com/developerworks/java/library/j-sslnb.html?ca=dgr-jw03j-sslnb

    MORE IBM SPAM Previous discussion
    I find it interesting spam, but thats a matter of taste. If the OP was truly interested in "trying to get new information out there" he would answer the mulitple questions about NIO and especially NIO mixed with traditional Sockets and NIO vs Secure Sockets. These are all on ALT, NIO is of no interest to New to Java folk.
    Given their budget I think IBM could do a better job of publishing their research.

  • Java.nio read/write question

    Hello,
    I just started to learn the java.nio package so I decided to make a simple Echo server. I made a client which reads a line from the keyboard, sends it to the server and the server returns it. It all works except one little detail. Here's little code from the server:
                                 int n = client.read(buffer);
                                 if ( n > 0)
                                     buffer.flip();
                                     client.write(buffer);
                                     Charset charset = Charset.forName("ISO-8859-1");
                                     CharsetDecoder decoder = charset.newDecoder();
                                     charBuffer = decoder.decode(buffer);
                                     System.out.println(charBuffer.toString());
                                     buffer.clear();
                                  }So that works, I send the data and then I receive it back. But only for the client. I also wanted the server to print the line which is the reason for the charset and the decoder. The above code however prints only a blank line. Thus I tried this:
                                 int n = client.read(buffer);
                                 if ( n > 0)
                                     buffer.flip();
                                     Charset charset = Charset.forName("ISO-8859-1");
                                     CharsetDecoder decoder = charset.newDecoder();
                                     charBuffer = decoder.decode(buffer);
                                     System.out.println(charBuffer.toString());
                                     client.write(buffer);
                                     buffer.clear();
                                  }Or in other words I just moved the write() part downwards. So far so good, now the server was actually printing the lines that the client was sending but nothing was sent back to the client.
    The question is how to make both, the send back line and the print line on the server side to work as intended. Also a little explanation why the events described above are happening is going to be more than welcome :)
    Thanks in advance!

    Strike notice
    A number of the regular posters here are striking in protest at the poor
    management of these forums. Although it is our unpaid efforts which make the
    forums function, the Sun employees responsible for them seem to regard us as
    contemptible. We hope that this strike will enable them to see the value
    which we provide to Sun. Apologies to unsuspecting innocents caught up in
    the cross-fire.

  • NIO design question

    Hi,
    I'm looking for advice on how to implement the following architecture in a scalable way. Can NIO be used for this?
    - My application exposes a bunch of servlets. Multiple clients invoke those servlets simultaneously.
    - I want to limit the number of connections across the application (all servlets) and have a separate limit on the number of connections per specific servlet.
    - When a servlet is invoked, I want to push a Callable onto a worker thread (which imposes the above limits) and have the servlet thread recycled back into the web server without closing the underlying connection.
    - The worker thread would eventually service the client, but a single worker thread would service potentially thousands of waiting clients.
    Why do I want to these limits? Because some servlets are very expensive (cpu and memory) and are likely to cause database update collisions.
    The problem is that I don't know how to return from the servlet body without closing the underlying connection. I believe the web container closes it automatically.
    Any ideas?
    Thanks,
    Gili

    Peter__Lawrey wrote:
    cowwoc wrote:
    1) It's container-specific as you mentioned.How many containers are you planning to use?I plan on using Tomcat or GlassFish. I'm not sure which yet.
    2) MaxThreads limits across the entire container instead of on a per-servlet basis.The maximum is per application. You can put each servlet in separate applications (in the same container)
    What is the motivation for having a purely per servlet limit?Some servlets are very computationally expensive and/or are liable to cause frequent database update collisions (using optimistic locking) if I allow too many simultaneous connections.

  • Java NIO question...

    Hey, I've just start reading and programming a client/server using NIO and got to a point where i need to keep a list of connected clients to send them messages as the server processes information it received... How could i handle this selection of clients that i want to send a specific message and how should I store those clients after they connect (I mean, when a client connects, a channel is started, but how can i identify later that a channel is related to THAT client?)
    Thanks in advance
    Message was edited by:
    Lemmerich

    Generally you will associate some kind of client session object with the channel via the attachment. This will also contain the input buffer and whatever you need in your application to identify the client.

  • Very simple question on J NIO

    This will be the easiest to answer i guess.
    I have a SocketChannel and i want to read two characters from it and store then in two char variables.
    I dont wanna use the socket().getInputStream().read() function as it is blocking
    and also i want to avail the non-blocking read option using configureBlocking Function.
    thanks for reading the post

    NIO is a lot of work, but there are examples, take a look at Taming the NIO Circus for an example and some pointers about problems.
    The simplest approach is to use threads and streams. It all depends on what your application structure is, whether blocking is a problem. You can also limit the time the read blocks for by using Socket.setSoTimeout.
    You will need to eventually block before you can use the data that you have read. If your application uses Swing, you can use SwingUtilities.invokeLater to post an event to the GUI when the data arrives.

  • Question on use of NIO with je

    I saw the FAQ entry about NIO: http://www.oracle.com/technology/products/berkeley-db/faq/je_faq.html#24
    Could you clarify the problems with NIO and situations it shouldn't be used? Currently we are using it with 3.2 and haven't run into any issues but I don't know if we are just getting lucky.
    -Nick

    What I recall is that the NIO JVM bugs were related to GC. Direct memory is not managed like ordinary heap memory, so a separate (and buggy) mechanism is used in the JVM. The symptoms were very slow GC and OOME. It's possible that Sun has fixed these problems since then, but as Charles said there is no measurable performance benefit for direct memory in JE, so it is no longer used in JE. As Charles says, ByteBuffers are still used, but this is not a use of direct memory.
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Java.nio selector non-blocking IO question

    Hi,
    I am designing an interactive server where multiple clients can log on and communicate with the server. I designed a protocol that the client/server use to talk to each other. My server runs a Selector to monitor a ServerSocket, accepting connections and reading continuously from clients.
    Now my question is, since read() on ServerChannel are non-blocking using selector, how can I be sure that my entire protocol message will be read each time selector wakes up? For example, a slow client sends me a 5kb message, in one write() command, can I be sure that I will be able to read the entire message in one non-blocking read() command as well? If not, then the design becomes much more complicated, as I have to pipe each client's input into a handler thread that performs blocking i/o to read a protocol message one at a time. If I do that, then I might as well not use select() at all.
    I did some preliminary tests, and it seems that for my purpose (message of size <= 50kb), a read() command will always be able to read the entire message. But I can't find any documentation on this subject. My guess is that I cannot trust non-blocking I/O as well, which means it does not fit my purpose.
    Any help will be much appreciated.
    Thanks,
    Frank

    You can't be sure a read() will read in all the data from a client in one call.
    For example, say your message from the client to the server is of the following format. <start>message here<end>, where <start> indicates the start of a message and <end> the end of the message. In one read() call you might get "<start>message he". Your server would recognize this is partially correct but it needs the rest of the message. The server would store this and on the second read() you might get "re<end>" for the complete message.
    The purpose of non-blocking I/O is so you don't have to wait around for the second part of the message, you can process other client messages while the first client finishes sending their message. This way other clients aren't waiting around while you(the server) sit and wait for client 1 to finish sending it's data.
    So basically there is no gaurantee you will get a whole message intact. Your protocol will have to deal with partial messages, recognize them, store the partial message in a buffer, and on subsequent reads get the rest of the message.
    Nick

  • Questions about jdk nio charset

    During load test, work threads all wait for reading charset classes from Classloader. Why not keep the stable result in cache? Thanks a lot!
    "ExecuteThread: '25' for queue: 'weblogic.kernel.Default'" daemon prio=1 tid=0x5c941800 nid=0x38ee waiting for monitor entry [5d697000..5d6988c8]
         at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:49)
         - waiting to lock <0x5113ebe0> (a sun.net.www.protocol.jar.JarFileFactory)
         at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:85)
         at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:107)
         at java.net.URL.openStream(URL.java:913)
         at sun.misc.Service.parse(Service.java:203)
         at sun.misc.Service.access$100(Service.java:111)
         at sun.misc.Service$LazyIterator.hasNext(Service.java:257)
         at java.nio.charset.Charset$1.getNext(Charset.java:303)
         at java.nio.charset.Charset$1.hasNext(Charset.java:318)
         at java.nio.charset.Charset$2.run(Charset.java:361)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.nio.charset.Charset.lookupViaProviders(Charset.java:358)
         at java.nio.charset.Charset.lookup(Charset.java:385)
         at java.nio.charset.Charset.isSupported(Charset.java:407)
         at java.lang.StringCoding.lookupCharset(StringCoding.java:82)
         at java.lang.StringCoding.encode(StringCoding.java:363)
         at java.lang.StringCoding.encode(StringCoding.java:380)
         at java.lang.String.getBytes(String.java:590)

    who can tell me how to avoid this? Thanks a lot!

  • A NIO question

    I want to use NIO to improve our servlet program,
    but I found that there are only File and socket operation source in the WWW
    I had to use a Channels.newChannel(OutputStream o ) to convert a outputStream to a WritableByteChannel ,
    however this mehtod only redirect the IO operation to stream. It does not improve the performence after I writing the code below:
    // this one use IO method
         public void doGet(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException{
              byte[] bt = new byte[4096*512];
              for(int i = 0; i < 4096*512; i++)
              bt[i] = (byte)i;
              long startTime2 = System.currentTimeMillis();     
              ServletOutputStream sos = res.getOutputStream();
              sos.write(bt);          
              sos.flush();
              sos.close();
              long endTime2 = System.currentTimeMillis();
              System.out.println("IO outPut spend " + (endTime2 - startTime2));     
    // this one use NIO method
         protected void doGet(HttpServletRequest req, HttpServletResponse res)
              throws ServletException, IOException {
    byte[] nb = new byte[4096 * 512];
              for (int i = 0; i < 4096 * 512; i++)
                   nb[i] = (byte) i;
         long startTime2 = System.currentTimeMillis();
              WritableByteChannel wbc = Channels.newChannel(res.getOutputStream());
              wbc.write(ByteBuffer.wrap(nb));
              wbc.close();
              long endTime2 = System.currentTimeMillis();
              System.out.println("NIO outPut use " + (endTime2 - startTime2));
    how could I use NIO to improve the HttpServlet writing speed?
    Thanks u.

    If you have only a single connection, and especially if you have multiple threads, NIO will not help. Its most effective when multiple connections can be processed by a single thread.

  • Two NIO questions

    Hi,
    I'm very intrigued by some of the possible performance enchancements my application can use with NIO. I've got 2 situations that I want to ask the NIO experts about:
    1. In one scenario, I do my own "broadcasts" over TCP (instead of UDP) by populating an OutputStream that is backed by a ByteArrayOutputStream. Then I convert my ByteArrayOutputSTream to a byte[] using toByteArray, and for each one of my OutputStream's I need to broadcast to (these are OutputStreams going thru Sockets), I write the entire byte[] into it. Is there a more efficient way to handle this with NIO? It seems like there is lots of extra bytes being copied in the JVM since the same byte[] is being sent to multiple OutputStreams. Would using a direct ByteBuffer help? Is there a performance penalty for using a ByteBuffer instead of a byte[] if I'm only sending it to one OutputStream?
    2. In another scenario, I'm reading an InputStream. After a certain point of reading the stream, I need to redirect the remaining bytes in the stream to an OutputStream. Currently I'm populating the remaining bytes of the InputStream into a ByteArrayOutputStream, convert it to a byte[] usinng toByteArray(), and then send that out to my designated OutputStream. Once again, it seems like I'm doing unnecessary copying of bytes and the NIO package should have something to directly transfer the bytes from my InputStream to my OutputStream.
    Thank you in advance for any help.
    Mark

    Well here is some pseudo-code for my first scenario.. It's basically restating what I explained earlier but I hope this helps:
    import java.io.*;
    public class Asdf{
    private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    public ObjectOutputStream getOutputStream(){
         this.byteArrayOutputStream.reset();
         return new ObjectOutputStream(this.byteArrayOutputStream);
    public void broadcastMessage(ObjectOutputStream oos){
         oos.close();
         Iterator broadcastOutputStreamIter = getBroadcastOutputStreamIter();
         while (broadcastOutputStreamIter.hasNext()){
         ObjectOutputStream curOos = (ObjectOutputStream)broadcastOutputStreamIter.next();
         this.byteArrayOutputStream.writeTo(curOos);
         curOos.flush();
    private Iterator getBroadcastOutputStreamIter(){
         //left out for clarity
    public static void main(String[] args){
         Asdf asdf = new Asdf();
         ObjectOutputStream oos = asdf.getOutputStream();
         oos.writeObject("asdflkj");
         oos.writeBoolean(true);
         broadcastMessage();
    To use NIO, I assume instead of using a ByteArrayOutputStream, I need to use a ByteBuffer, right? How would I tie in my usage of my ObjectOutputStream with the ByteBuffer? Or would I have to just create a new ByteBuffer on my ByteArrayOutputStream within broadcastMessage() and send that out over my Socket's Channel?
    I actually haven't written code for #2 yet b/c I'm trying to figure this out. Basically I want to create an ObjectInputStream from a socket, and after reading an undetermined number of objects from the stream, I want to send the remaining bytes of that stream to another Socket's OutputStream.
    Thanks,
    Mark

  • A few general java.nio.* questions

    Hi,
    I've been reading up on selectors and channels for networking programming. During my reading I came across this website, http://www.javaworld.com/javaworld/jw-09-2001/jw-0907-merlin.html
    It's kind of dated (3 years ago) but the example they provide on non-blocking I/O still compiles.
    Further in the article (http://www.javaworld.com/javaworld/jw-09-2001/jw-0907-merlin-p4.html) they mention a few "gotcha's" with using selectors. One in particular that caught my attention was "a selector can have only 63 channels registered, which is probably not a big deal."
    So only 63 channels max can be registered to a selector ? I searched around the web and the java API looking to confirm this. Does anyone know this to be true or if the max 63 registered channels was dropped before java 1.4 beta became the stable release of java 1.4? The article was written when java 1.4 was in beta.
    Lastly, do selectors weed out dropped socket connections? For example. Say I have 500 socket (TCP/IP) connections to a server of mine. Of those 500 , 100 are now closed by the client by the time the server comes around to send them data. As the server loops through the 500 connections using a selector, will there be a delay because 100 connections are dropped and the server must wait for a timeout before proceeding to send the data to the next connection or does the selector detect these 100 dropped connections and only use the 400 still connected?
    I hope this last question makes sense.
    Thanks in advance,
    Nick

    Hi,
    I've been reading up on selectors and channels for
    networking programming. During my reading I came
    across this website,
    http://www.javaworld.com/javaworld/jw-09-2001/jw-0907-m
    rlin.html
    It's kind of dated (3 years ago) but the example they
    provide on non-blocking I/O still compiles.
    Further in the article
    (http://www.javaworld.com/javaworld/jw-09-2001/jw-0907-
    erlin-p4.html) they mention a few "gotcha's" with
    using selectors. One in particular that caught my
    attention was "a selector can have only 63 channels
    registered, which is probably not a big deal."
    So only 63 channels max can be registered to a
    selector ? I searched around the web and the java API
    looking to confirm this. Does anyone know this to be
    true or if the max 63 registered channels was dropped
    before java 1.4 beta became the stable release of java
    1.4? The article was written when java 1.4 was in
    beta.I don't know if that limit of 63 is still there, but it probably makes sense to use a pool of threads each supporting a Selector if the number of connections gets that high.
    Lastly, do selectors weed out dropped socket
    connections? For example. Say I have 500 socket
    (TCP/IP) connections to a server of mine. Of those 500
    , 100 are now closed by the client by the time the
    server comes around to send them data. As the server
    loops through the 500 connections using a selector,
    will there be a delay because 100 connections are
    dropped and the server must wait for a timeout before
    proceeding to send the data to the next connection or
    does the selector detect these 100 dropped connections
    and only use the 400 still connected?closed channels are removed by the select statement. When using non-blocking IO, nothing waits anyway.
    I hope this last question makes sense.
    Thanks in advance,
    Nick

  • NIO question...

    saw this sample code on a webpage...
    while (it.hasNext()) {
          SelectionKey sk = (SelectionKey)it.next();
          it.remove();
          int readyOps = sk.readyOps();
          // Disable the interest for the operation
          // that is ready. This prevents the same
          // event from being raised multiple times.
          sk.interestOps(sk.interestOps() & ~readyOps);
    ...is the setting of interestOps for the selection key necessary? i've not done this in my program and it seems to be working fine.

    is the setting of interestOps for the selection key
    necessary? i've not done this in my program and it
    seems to be working fine.That is something which you should do if e.g the read is handled by another (service) thread. You don't want to get readiness notifications while you are processing.
    Kaj

Maybe you are looking for

  • Trouble Updating Songs on iPod

    Ive never had a problem with my mini iPod until this week. I plugged it into my PC on night (like normal) and when I came back an hour later, all the songs were gone even though the songs were still in the library. I reset the iPod and downloaded the

  • Unable to log into websites since Safari 7.1 upgrades

    Ever since I have upgraded to Safari 7.1, I am no longer able to log into my US Airways frequent flier account.  Clicking on the login button won't do anything.  It worked flawlessly with the previous version.  Also, I use TitanTV to look at TV listi

  • Sudden white screen on my macbook pro resulting in distorted image

    After a sudden white screen, I tried to boot my macbook into recovery mode and the screen started to have some purple straps all over the place. Also, I noticed random distorted startup screen (the grey background and apple logo) and then would end u

  • ALE/IDOC/EDI

    Hi All Please send me some reading material for brief idea regarding ALE/IDOC/EDI. Thanks Goutam

  • Accessing remote table from another R/3 system

    Hi, We are running our production on ECC 5.0 but we ABAP web dynpro environment on ECC7.0 portal box. We want use ABAP web dynpro for publishing some application through portal. I have read many document regarding accessing remote systems through RFC