users@grizzly.java.net

gateway nio migration guide to grizzly part2

From: Survivant 00 <survivant00_at_gmail.com>
Date: Sun, 15 Jun 2008 10:13:38 -0400

salut tout le monde,

I just finish to change my demo nio to v2. Instead of using 1 thread by
connection, I used Selector.

changes :
- selector instead of 1 thread by connection
- fix asynchronizedCloseException (selector fixed that)
- add producer/consomer queue to fastest the read input.

There is still a question that I need to answer.

How to use OP.WRITE correctly. I check the blog of Jean-Francois, but there
is only scriptlet and not the full source, so I'm stuck here. Don't know
how to implement it correctly.

and do you have other comments that can help me to produce a better demo ?


(code taken from my QuoteResponseHandler class)
try {

getClientConnectionHandler().getSocketChannel().write(writeBuffer);
            /*
            int len = socketChannel.write(byteBuffer);
            if (len == 0) {
            if (writeSelector == null){
                writeSelector = SelectorFactory.getSelector();
            }
            key = socketChannel.register
            (writeSelector, key.OP_WRITE);
                if (writeSelector.select(30 * 1000) == 0) {
                    throw new IOException("Client disconnected");
                }
            }
            */


        } catch (IOException e) {
            e.printStackTrace();
        }




(here the code in my SocketConnectionListener class that accept the input
connection)

while(!isShutdown()){
            try {

                f_selector.select();

                // Get list of selection keys with pending events
                Iterator<SelectionKey> it =
f_selector.selectedKeys().iterator();

                // Process each key
                while (it.hasNext()) {
                    // Get the selection key
                    SelectionKey selKey = it.next();

                    // Remove it from the list to indicate that it is being
processed
                    it.remove();

                    // Check if it's a connection request
                    if (selKey.isValid() && selKey.isAcceptable()) {
                        ConnectionAcceptor connectionAcceptor =
(ConnectionAcceptor)selKey.attachment();
                        connectionAcceptor.accept();
                    }

                    // Check if a message has been sent
                    if (selKey.isValid() && selKey.isReadable()) {
                        ClientConnectionHandler connectionReader =
(ClientConnectionHandler)selKey.attachment();
                        connectionReader.read();
                    }
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


the next step will be to migrate to grizzly.. I think that I'll be ready :)