Network - Application development the easy way

http://netty.io

https://github.com/netty/netty

@netty_project

Norman Maurer 

  • Red Hat (JBoss) - EAP Core Team
  • Former contractor for Apple Inc
  • Author - Netty in Action
  • Apache Software Foundation Member
  • Netty  / Vert.x Core-Developer and all things NIO
  • Java and Scala
  • Twitter: @normanmaurer
  • Github: https://github.com/normanmaurer

What is NETTY ?

Netty is a NIO client server framework which enable quick and easy development of network applications such as protocol servers and clients...

YET ANOTHER Network FrameWORK ?


YES!

@!# WTF, why should I care ?

Features

Features

  • Asynchronous
  • Simple and unified API
  • High-Performing
  • Listeners and Callbacks supported
  • Zero-file-copy
  • Buffer Pooling
  • Gathering / Scattering

... and many more

Asynchronous, what does this mean ?

Asynchronous...

  • I/O Operations don't block at all!
  • Get notified once the operation completes
  • Be able to share one Thread across many connections

© http://www.flickr.com/photos/56223083@N06/5514161603/

As waiting is b0ring....
But synchronous / blocking IO  served me well! Again, why should I care ?

Blocking-IO may not SCALE!


Usually a Thread takes memory from 256kb to 1mb for the stack space!

Good luck with serving 1 million concurrent connections

Ouch....

© http://www.flickr.com/photos/djjasoncook/8608405432/

Netty to the rescue

© http://www.flickr.com/photos/brizzlebornandbred/6136653327/

Asynchronous in action

    Channel channel = ...
    ChannelFuture cf = channel.writeAndFlush(data);
    cf.addListener(
        new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future)
                throws Exception {
                if(!future.isSuccess() {
                    future.cause().printStacktrace();
                    ...
                }
                ...
            }
        });
    ...
    // or block and wait for completion if you really need!
    cf.sync();

Transports

  • NIO
  • OIO
  • Local
  • Embedded

And more to come ....

Supported protocols

  • TCP
  • UDP
  • SCTP*
  • UDT
  • Serial

* Only supported on linux

Simple state Model

State Flow 

Data Container

ByteBuf == ByteBuffer on steroids!

© http://www.flickr.com/photos/lightmash/3183278318/

ByteBuf

  • Separate index for reader/writer
  • CompositeByteBuf 
  • Direct and Heap implementations
  • Resizable with max capacity
  • Support for reference-counting
  • Method-Chaining

    ByteBuf buf = ...;
    buf.writeInt(1).writeBytes(data).writeBoolean(true)...

ByteBufAllocator

© https://blog.twitter.com/2013/netty-4-at-twitter-reduced-gc-overhead

Pooling or not pooling....

Channelhandler


Users building blocks!

© http://www.flickr.com/photos/bdesham/2432400623/

ChannelHandler


Your business logic needs a place, and this is where it belongs too

ChannelHandler Facts

  • Inbound vs. Outbound
  • Always called by the assigned EventExecutor
  • Type-safe*

Example use cases

  • Convert data a.k.a Encoder / Decoder
  • Business-Logic
  • Handle state changes

Provided codecs

We have a lot of them!

Provided Codecs

  • HTTP
  • Websockets
  • SPDY
  • Serialization
  • JBoss Marshalling
  • Protobufs
  • SSL
  • Zlib

Provided Codecs - The future

  • DNS
  • Memcached Binary
  • SockJS

Adapters

Makes your life easier

  • ChannelInboundHandlerAdapter
  • ChannelOutboundHandlerAdapter
  • SimpleChannelInboundHandler

Adapter in Action

    @Sharable
    public class  Echo4ServerHandler extends 
            ChannelInboundHandlerAdapter {        
        @Override 
        public void channelRead(ChannelHandlerContext ctx,
                Object msg) {
            ctx.writeAndFlush(msg);
        }
        
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx,
                Throwable cause) {
            cause.printStacktrace();
            ctx.close();
        }
    }

Decoder

  • ByteToMessageDecoder
  • MessageToMessageDecoder

Encoder

  • MessageToByteEncoder
  • MessageToMessageEncoder

Hybrids

  • ByteToByteCodec
  • ByteToMessageCodec
  • MessageToMessageCodec

Other Often useful Decoders

  • ReplayDecoder
  • FixedLengthFrameDecoder
  • LengthFieldBasedFrameDecoder
  • LineBasedFrameDecoder
  • DelimiterBasedFrameDecoder

ChannelPipeline

The home for all your ChannelHandlers

© http://www.flickr.com/photos/rickz/2113212191/

ChannelPipeline

  • Holds the ChannelHandlers for a Channel
  • All events will get passed through it
  • On the fly modification
  • One-to-one relation between Channel and ChannelPipeline

ChannelPipeline


Kind of a unix-pipe-like thing...

$ echo "Netty is shit...." | sed -e 's/is /is the /'
                    Netty is the shit....
                        
                    

You see, everything is adjustable!

Adding ChannelHandlers

    Channel ch = ...;
    ChannelPipeline p = ch.pipeline();
    EventExecutor e1 = new DefaultEventExecutor(16);
    EventExecutor e2 = new DefaultEventExecutor(8);

    // Executed in EventLoop Thread
    p.addLast(new MyProtocolCodec());

    // Executed in one of the Threads of e1
    p.addLast(e1, new MyDatabaseAccessingHandler());

    // Executed in one of the Threads of e1
    p.addLast(e2, new MyHardDiskAccessingHandler());

Event-Loop

Someone needs to do the heavy work!

© http://www.flickr.com/photos/stavos52093/9645884201/

Event-loop

  • One-to-Many relation between EventLoop and Channel
  • Process "events" and hand over work to the ChannelPipeline


All Tasks executed in Event-Loop!

No need to worry about synchronization

© http://www.flickr.com/photos/vegaseddie/5700609302/

© http://www.flickr.com/photos/disowned/1159135004/

Repeat with me:
I promise to never block the Event-Loop!

EventLoop Hierarchy




All the ScheduleExecutorService goodies for free!

EventLoop - How does it work



Run a Task in the EventLoop

    public class MyHandler extends 
            ChannelOutboundHandlerAdapter {
        @Override
        public void write(ChannelHandlerContext ctx,
                Object msg, ChannelPromise promise) {
            ctx.write(msg, promise);
        
            if (!promise.isDone() {
                // schedule task for in 30 seconds
                ctx.executor().schedule(new MyWriteTimeoutTask(
                    promise), 30, TimeUnit.SECONDS);
            }
            // Run an arbitrary task from an I/O thread.
            ctx.executor().execute(new Runnable() { ... });
        }
    }

Bootstrap your application

It's time to accept some real traffic

© http://www.flickr.com/photos/postbear/4500894615/

Bootstrap


  • Fluent-API a.k.a DSL
  • Graceful shutdown
  • Lightweight

Bootstrap a Server

    ServerBootstrap b = new ServerBootstrap();
    EventLoopGroup group = new NioEventLoopGroup(numThreads);
    try {
        b.group(group, group))
            .channel(NioServerSocketChannel.class)
            .localAddress(new InetSocketAddress(port))
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) {
                    ch.pipeline().addLast(new YourChannelHandler());
                }
            });
        ChannelFuture f = b.bind().sync();
        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }

Bootstrap a Client

    Bootstrap b = new Bootstrap();
    EventLoopGroup group = new NioEventLoopGroup(numThreads);
    try { 
        b.group(group)
            .channel(NioSocketChannel.class)
            .remoteAddress(new InetSocketAddress("10.0.0.1", 25))
            .handler(new ChannelInitializer<SocketChannel>() {            
                @Override
                public void initChannel(SocketChannel ch) {  
                    ch.pipeline().addLast(new YourChannelHandler());
                }
            });
        ChannelFuture f = b.connect().sync();
        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }

General

  • ASL2 license
  • Hosted at Github
  • Community-driven

Companies

  • Red Hat
  • Twitter
  • Facebook
  • Firebase
  • Urban Airship
  • LinkedIn
  • Samsung
  • Boundary

And many more... 

(Opensource) Projects

  • Vert.x
  • HornetQ
  • Infinispan
  • Play Framework*
  • Finangle
  • Apache Cassandra
  • Minecraft

And again many more....

Things to come

  • Chained ChannelPipelines
  • Native edge-triggered Transport*
  • Lambda support

Other NIO Frameworks

  • Apache MINA
  • Grizzly
  • Akka IO
  • HawtDispatch*
  • JBoss XNIO*

It's OpenSource - Contribute now!

© http://www.flickr.com/photos/legofenris/4499417549/

Get Involved

Mailinglist
https://groups.google.com/forum/#!forum/netty

IRC
#netty irc.freenode.org

Website
http://netty.io

GitHub
https://github.com/netty/netty/

Too Low Level ?

You may like Vert.x which is based on Netty!

Not enough info?


You may want to buy my book...



http://www.manning.com/maurer/


Questions please!

© http://www.flickr.com/photos/photomequickbooth/3531951972/

Thanks!