The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


Growing the latest baby of the family: Grizzly 2.0 jars' snapshot now available!

Posted by jfarcand on September 4, 2008 at 10:07 AM PDT

Starting today, we are shipping the new Grizzly 2.0 artifacts/jars as Alexey described (and lead!).

IMG_2389.JPG

Grizzly 2.0 is a complete new re-design of Grizzly based on our experience since the monster poped up in 2005. We learned a lot and we are now ready to do a revolution :-) This is just the beginning and we are discussing among us to come with the best design. So far, Alexey demonstrated how easy it will looks like...many cycles/discussions will be done before we can say this is a stable candidate (but we wanted to share early to get feedback)...BUT.... The server side looks like:

        TCPNIOTransport transport = TransportManager.instance().createTCPTransport();

        // Add TransportFilter, which is responsible
        // for reading and writing data to the connection
        transport.getFilterChain().add(new TransportFilter());
        transport.getFilterChain().add(new EchoFilter());

        try {
            // binding transport to start listen on certain host and port
            transport.bind(HOST, PORT);

            // start the transport
            transport.start();

            System.out.println("Press any key to stop the server...");
            System.in.read();
        } finally {
            // stop the transport
            transport.stop();

            // release TransportManager resources like ThreadPool
            TransportManager.instance().close();
        } 

A simple filter looks like

    /**
     * Handle just read operation, when some message has come and ready to be
     * processed.
     *
     * @param ctx Context of {@link FilterChainContext} processing
     * @param nextAction default {@link NextAction} filter chain will execute
     *                   after processing this {@link Filter}. Could be modified.
     * @return the next action
     * @throws java.io.IOException
     */
    @Override
    public NextAction handleRead(FilterChainContext ctx, NextAction nextAction)
            throws IOException {
        // Get the read message
        Object message = ctx.getMessage();

        /* Send the same message on the connection. The filter chain write
         * will pass each filter on a filter chain (interceptWrite method),
         * before sending the message on a wire. It means each filter can modify
         * the message, before it will be sent to the recipient
         */
        ctx.getFilterChain().write(ctx.getConnection(), message);

        return nextAction;
    }
And the client looks like:

        Connection connection = null;

        // Create the TCP transport
        TCPNIOTransport transport = TransportManager.instance().
                createTCPTransport();

        try {
            // start the transport
            transport.start();

            // perform async. connect to the server
            ConnectFuture future = transport.connectAsync(EchoServer.HOST,
                    EchoServer.PORT);
            // wait for connect operation to complete
            connection = (TCPNIOConnection) future.get(10, TimeUnit.SECONDS);

            assert connection != null;

            // create the message
            ByteBuffer message = ByteBuffer.wrap("Echo test".getBytes());
            // sync. write the complete message using
            // temporary selectors if required
            WriteResult result = connection.write(message);

            assert result.getWrittenSize() == message.capacity();

            // allocate the buffer for receiving bytes
            ByteBuffer receiverBuffer = ByteBuffer.allocate(message.capacity());

            ReadResult readResult = null;
            // read the same number of bytes as we sent before
            while (receiverBuffer.hasRemaining() &&
                    (readResult == null || readResult.getReadSize() > 0)) {
                readResult = connection.read(receiverBuffer);
            }

            // check the result
            assert message.flip().equals(receiverBuffer.flip());
        } finally {
            // close the client connection
            if (connection != null) {
                connection.close();
            }

            // stop the transport
            transport.stop();
            // release TransportManager resources like ThreadPool etc.
            TransportManager.instance().close();
        } 

This is just the beginning, and things will most probably change a lot based on our community (thanks to everybody... it keeps us busy those day!) and discussions. Next of the redesign task? Our http framework. Anybody interested just join the project and checkout the code...and stay tuned for our current official version, Grizzly 1.9.0, which add support for NIO.2!

technorati:

Related Topics >> Glassfish      
Comments
Comments are listed in date ascending order (oldest first)

Hi Jean-Francois -- we're big fans of your work on grizzly here at work. I was wondering if you could offer some advice -- we're having stability issues with the newest glassfish v3tp2 (it sometimes takes up 100% cpu). Is running grizzly on Jetty technically more stable for now? Do your recommend this?

Hum, can you try a more recent v3 build to see if you still get the 100% CPU? Grizzly on Jetty is also an alternative, but I think the current v3 is quite stable. Please follow the discussion on users@grizzly.dev.java.net (if you can :-)). Thanks Jeanfrancois

Not yet. We are planning to re design from zero that module. The goal is to have it end of December, maybe earlier. Thanks!!

Do you have a grizzly-http 2.0 yet? The stuff in svn/2dot0 is still referencing 1.9