Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Netty 4 Overview (Obsolete)

Netty 4 Overview (Obsolete)

UPDATE: This presentation is out-of-date. We changed a lot of things in the public API since then.

Netty is a Java asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. It powers many large scale services built on top of JVM such as Twitter.

This talk will provide an overview of Netty 4, the major backward-incompatible update against the version 3, from basic constructs to a working example. It's not necessarily for those who are familiar with the previous versions on Netty.

9b123f408258511b201ca1230f260340?s=128

Trustin Lee

February 27, 2013
Tweet

Transcript

  1. Netty 4 Overview by Trustin Lee on 27-Feb-2013

  2. What is Netty? an asynchronous event-driven network application framework for

    rapid development of maintainable high performance protocol servers & clients
  3. Asynchronous Channel channel = ... ChannelFuture f = channel.write(message); f.addListener((f)

    -> { if (f.isSuccess()) { f.channel().write(nextMessage); // Async } else { f.cause().printStackTrace(); f.channel().close(); // Async } });
  4. Synchrony via Asynchrony ChannelFuture f = channel.write(message); try { f.sync()

    .channel().write(nextMessage).sync(); } finally { f.channel().close(); }
  5. Event-driven class MyInboundMessageListener extends ChannelInboundMessageHandlerAdapter <MyInboundMessage> { @Override void messageReceived(

    ChannelHandlerContext ctx, MyInboundMessage msg) { MyOutboundMessage res = ...; ctx.write(res); } }
  6. Available events • channelRegistered/Active • inboundBufferUpdated ◦ Most handlers have

    its own inbound buffer. ◦ Much less GC pressure than generating event objects per message ◦ CIMHAdapter.inboundBufferUpdated() invokes messageReceived() • channelInactive/Unregistered • exceptionCaught (Throwable) • userEventTriggered (Object)
  7. ChannelHandler type hierarchy ChannelHandler ChannelStateHandler ChannelOperationHandler ChannelInboundByteHandler ChannelInboundMessageHandler ChannelOutboundByteHandler ChannelOutboundMessageHandler

    *Adapter
  8. ChannelInboundByteHandlerAdapter abstract class ChannelInboundByteHandlerAdapter ... { ByteBuf newInboundBuffer(ChannelHandlerContext ctx) {

    return ctx.alloc().buffer(); } void freeInboundBuffer(ChannelHandlerContext ctx) { ctx.inboundByteBuffer().release(); } final void inboundBufferUpdated(ChannelHandlerContext ctx) { inboundBufferUpdated(ctx, ctx.inboundByteBuffer()); } abstract void inboundBufferUpdated(ctx, ByteBuf in); ... }
  9. ByteBufAllocator • "ctx.alloc().buffer()" • The default: PooledByteBufAllocator ◦ Pure Java

    jemalloc impl with some twist ◦ Beats JVM by not filling with NUL(0) ◦ Takes advantage of sun.misc.Unsafe to: ▪ Deallocate direct buffers timely ▪ Access direct buffers directly (+10% faster) ◦ Reference-counted ▪ "buf.release()" and "buf.retain()" ▪ Sampled leak detection enabled by '-D' flag
  10. Pipeline: List<ChannelHandler> ChannelPipeline p = ch.pipeline(); p.addLast( new HttpRequestDecoder(), //

    IB new HttpResponseEncoder(), // OM new MyHttpServiceHandler()); // IM p.addFirst(new SslHandler(...)); // IB //+OB
  11. Pipeline: dynamic manipulation class HttpAndHttpsHandler extends ChannelInboundByteHandlerAdapter { void inboundBufferUpdated(ctx,

    inBuf) { if (isHttp(inBuf)) { addHttpHandlers(ctx, inBuf); } else if (isSsl(inBuf)) { addSslHandler(ctx, inBuf); } else { ctx.close(); // Unknown protocol } }
  12. Pipeline: staged event threading EventExecutorGroup group = new DefaultEventExecutorGroup(16); ChannelPipeline

    p = channel.pipeline(); p.addLast( new RedisRequestDecoder(), // IB new RedisResponseEncoder()); // OM p.addLast( group, new RedisServiceHandler()); // IM
  13. EventExecutor and EventLoop • EventLoop ◦ performs actual I/O ◦

    triggers an event to a pipeline ◦ receives an operation from a pipeline ◦ performs arbitrary tasks submitted by user • EventExecutor ◦ EventLoop minus I/O ◦ Usually specified as an arg to ChannelPipeline. add*()
  14. EventLoop type hierarchy ScheduledExecutorService EventExecutorGroup EventLoop EventExecutor EventLoopGroup NioEventLoopGroup AioEventLoopGroup

    DefaultEvtExecGrp
  15. Putting all together NioEventLoopGroup NioSocketChannel ChannelPipeline HttpRequestDecoder EL2 ELn HttpRequestEncoder

    MyHttpServiceHandler DefaultEvtExecGrp EE1 EEm EL1 EE2
  16. Code walk-through via Factorial example

  17. Factorial example • Sends sequential integers to multiply them •

    Codec ◦ NumberEncoder ◦ BigIntegerDecoder • Server ◦ FactorialServerHandler ◦ FactorialServerInitializer ◦ FactorialServer • Client ◦ FactorialClientHandler ◦ FactorialClientInitializer ◦ FactorialClient
  18. @netty_project netty.io Q & A