Archive

Archive for December, 2008

Tricks and Tips with NIO.2/AIO part 0: A new beginning

In this new installment series of tricks in tips, I will start sharing my observations with the upcoming NIO.2 (Asynchronous I/O) support in JDK 7 (jsr 203). The idea is simple: I’ve already added some support inside the Grizzly Framework (upcoming 1.9.0 version), and will share what I’ve learned/measured. Everything is new, from the JDK to upcoming Grizzly support!

IMG_0104.JPG

But first, before I deep dive into what I’ve learned (and that’s why this blog’s title contains “part 0” :-)), I strongly recommend you first install JDK 7, take a look at the new AIO API, and build Grizzly 1.9.0 with AIO support enabled:

% svn checkout https://www.dev.java.net/svn/grizzly/trunk
% export JAVA_HOME=/location_of_b96
% mvn -f pom-jdk7.xml install

The module that contains AIO support are located under modules/grizzly-aio, the equivalent of the Grizzly Framework and modules/http-aio, which is a redesign of the http web server. Two OSGi bundles can can be launched (modules/bundles/grizzly-aio and modules/bundles/http-aio) using any OSGi runtime or directly using java -jar …

OK now I have to admit, the code is far from well designed (that will improve over time…trust me :-)). Why? Because I’ve added support to AIO on top of the NIO.1 framework and without forcing JDK 7 support for all modules. Only the AIO module requires JDK 7, hence I’ve “extended” as much as I can the original framework without adding JDK 7 dependencies.

To write a Grizzly application that use AIO, you just need to:


        final ProtocolFilter aioReadFilter = new AIOReadFilter();
        final ProtocolFilter echoFilter = new AIOEchoFilter();

        final AIOController aioController = new AIOController();

        TCPAIOHandler aioHandler = new TCPAIOHandler(aioConroller);
        aioHandler.setPort(port);

        aioController.addAIOHandler(aioHandler);

        aioConroller.setProtocolChainInstanceHandler(
                new DefaultProtocolChainInstanceHandler() {

                    @Override
                    public ProtocolChain poll() {
                        ProtocolChain protocolChain = protocolChains.poll();
                        if (protocolChain == null) {
                            protocolChain = new DefaultProtocolChain();
                            protocolChain.addFilter(aioReadFilter);
                            protocolChain.addFilter(echoFilter);
                        }
                        return protocolChain;
                    }
                });

        return aioConroller;

As you can see, buiding an AIO application using Grizzly is almost the same as with NIO.1

        final ProtocolFilter readFilter = new ReadFilter();
        final ProtocolFilter echoFilter = new EchoFilter();
        
        TCPSelectorHandler selectorHandler = new TCPSelectorHandler();
        selectorHandler.setPort(port);
        
        final Controller controller = new Controller();
        
        controller.setSelectorHandler(selectorHandler);
        
        controller.setProtocolChainInstanceHandler(
                new DefaultProtocolChainInstanceHandler(){
            @Override
            public ProtocolChain poll() {
                ProtocolChain protocolChain = protocolChains.poll();
                if (protocolChain == null){
                    protocolChain = new DefaultProtocolChain();
                    protocolChain.addFilter(readFilter);
                    protocolChain.addFilter(echoFilter);
                }
                return protocolChain;
            }
        });
        
        return controller;

Of course it would have been better to hide the difference between NIO.1 and NIO.2 (AIOController vs Controler, AIOReadFilter vs ReadFilter), but it would have means adding a dependencies on JDK 7 to the framework, which is unthinkable right now.

OK next time I will start describing what I needed to change, what are the new concepts and starts comparing NIO.1 performance with NIO.2. So far I've pretty good news 🙂

_uacct = "UA-3111670-1";
urchinTracker();

technorati:

Categories: Uncategorized

Grizzly 1.9.0 is out and refuse to hibernate!

The latest release of the monster include tons of enhancements and new features like new Asynchronous I/O support, OSGi improvement, ExecutorServices now supported natively, Asynchronous Http Write, and many more

IMG_2642.JPG

We have worked really hard on that release to deliver what the community was asking. The list is long, but here is a summary…more details every hour on Twitter!

  • Asynchronous I/O support: Module framework and http have been extended and can now take advantages of NIO.2…and the performance is fantastic!
  • Asynchronous HTTP write support: Module http (our WebServer) can now be configured to support asynchronous http write. Until now, the module was using this trick. Now you decide (via GrizzlyWebServer)
  • ExecutorServices now natively supported: You can now take your favorite ExecutorServices and use it as it is (no more Wrapper needed)
  • Our “on the side” Embeddable Servlet Container now supports all flavors of listeners like ServletContextListener etc. It is time to try our Servzly WebServer
  • Comet Framework performance improvement: we have significantly improved the performance, and added some nice features like the ReflectorCometHandler…and a very nice demo called Twitter 😉
  • GrizzlyAdapterChain now supports multiple GrizzlyAdapter and can map requests on the fly. You can now embed Grizzly Http and add several Adapter like ServletAdapter, JRubyAdapter, CometAdapter etc. and get requests dispatched to those Adapter.
  • Improvements here and there
  • A healthy community that helps improving the monster

But we don’t hibernate yet…next release is coming fast

_uacct = “UA-3111670-1”;
urchinTracker();

technorati:

Categories: Uncategorized