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

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!


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);


                new DefaultProtocolChainInstanceHandler() {

                    public ProtocolChain poll() {
                        ProtocolChain protocolChain = protocolChains.poll();
                        if (protocolChain == null) {
                            protocolChain = new DefaultProtocolChain();
                        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();
        final Controller controller = new Controller();
                new DefaultProtocolChainInstanceHandler(){
            public ProtocolChain poll() {
                ProtocolChain protocolChain = protocolChains.poll();
                if (protocolChain == null){
                    protocolChain = new DefaultProtocolChain();
                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";


Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s