Building scalable network applications with Netty

Embed Size (px)

DESCRIPTION

 

Text of Building scalable network applications with Netty

  • 1. Building scalable network applications with Netty Jaap ter Woerds @jaaptw XMS pin: EBDDFF9A
  • 2. The trouble with synchronous IO
  • 3. The trouble with synchronous IO
  • 4. The trouble with synchronous IO
  • 5. Creating your custom server
  • 6. Creating your custom server
  • 7. Netty? Unified, asynchronous programming model. All IO operations return immediately.
  • 8. Netty? Channel channel =.. channel.write(myMsg).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if(future.isSuccess()){ // Business as usual } else{ // Handle failure } } });
  • 9. Netty? Networking application framework with a modular architecture and quite some ready reusable components.
  • 10. When should I care about this? Custom servers / networking applications / proxies High concurrency
  • 11. Netty concepts
  • 12. Netty concepts ByteBuf random accessable sequence of bytes Abstraction over plain byte arrays and NIO buffers
  • 13. Netty concepts ByteBuf Use: encoding and decoding data ByteBuf b = . b.writeInt(3).writeBoolean(true);
  • 14. Netty concepts Channel Link ('connection') to a resource that provides IO operations like read / write
  • 15. Netty concepts Channel Channel c = ..; c.write(myMsg).addListener(CLOSE);
  • 16. Netty concepts ChannelHandler IO event listener,handles outbound and / or inbound events.
  • 17. Netty concepts ChannelInboundHandler Defines listener methods for incoming IO events
  • 18. Netty concepts ChannelInboundHandler channelActive(ChannelHandlerContext ctx) channelRead(ChannelHandlerContext ctx, Object msg) channelReadComplete(ChannelHandlerContext ctx) channelInactive(ChannelHandlerContext ctx) ....
  • 19. Netty concepts ChannelOutboundHandler Defines listener methods for outgoing IO events
  • 20. Netty concepts ChannelOutboundHandler write(ChannelHandlerContext ctx, Object msg, ChannelPromise..) flush(ChannelHandlerContext ctx)
  • 21. Netty concepts ChannelPipeline A List of ChannelHandlers that process incoming and outgoing events in a specific order
  • 22. Netty concepts ChannelInitializer Configures the pipeline for a Channel. In fact, a special case of ChannelInboundHandler which creates the handlers for a channel whenerver a new channel is registered.
  • 23. Netty concepts ChannelInitializer Configures the pipeline for a Channel. In fact, a special case of ChannelInboundHandler which creates the handlers for a channel whenerver a new channel is registered.
  • 24. Netty concepts ChannelInitializer @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Decoders pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(MAX_LENGTH)); pipeline.addLast("stringDecoder", STRING_DECODER); pipeline.addLast("moderatorHandler", moderatorHandler); pipeline.addLast("chatServerHandler", chatServerHandler); // Encoder pipeline.addLast("stringEncoder", STRING_ENCODER); }
  • 25. Netty concepts Bootstrap EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, BACKLOG) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(channelInitializer); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync();
  • 26. Netty architecture Chain of responsibility Incoming data flow through the ChannelPipeline - Framing ByteBuf - Decode frames - Decode frames in DTOs/POJOs
  • 27. Netty architecture
  • 28. Netty components Out of the box support for: Streaming compression Zlib SSL stream encryption HTTP WebSockets ProtocolBuffer
  • 29. XMS server protocol layer
  • 30. Example SSE / CORS
  • 31. Example SSE / CORS In the last handler we deal with normal POJO and handle an incoming request without actually caring about the connection layer! @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { IncomingMessagePojo m = (IncomingMessagePojo) msg; handle(m);// Login / Send a text message/ Handle create group }
  • 32. Example SSE / CORS
  • 33. Thank you! Example code: https://github.com/jaapterwoerds/jfall-netty4 Further reading: https://nettio.io http://xms.me http://tech.ebuddy.com