Building scalable network applications with Netty

  • Published on
    10-Nov-2014

  • View
    586

  • Download
    1

DESCRIPTION

 

Transcript

<ul><li> 1. Building scalable network applications with Netty Jaap ter Woerds @jaaptw XMS pin: EBDDFF9A </li> <li> 2. The trouble with synchronous IO </li> <li> 3. The trouble with synchronous IO </li> <li> 4. The trouble with synchronous IO </li> <li> 5. Creating your custom server </li> <li> 6. Creating your custom server </li> <li> 7. Netty? Unified, asynchronous programming model. All IO operations return immediately. </li> <li> 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 } } }); </li> <li> 9. Netty? Networking application framework with a modular architecture and quite some ready reusable components. </li> <li> 10. When should I care about this? Custom servers / networking applications / proxies High concurrency </li> <li> 11. Netty concepts </li> <li> 12. Netty concepts ByteBuf random accessable sequence of bytes Abstraction over plain byte arrays and NIO buffers </li> <li> 13. Netty concepts ByteBuf Use: encoding and decoding data ByteBuf b = . b.writeInt(3).writeBoolean(true); </li> <li> 14. Netty concepts Channel Link ('connection') to a resource that provides IO operations like read / write </li> <li> 15. Netty concepts Channel Channel c = ..; c.write(myMsg).addListener(CLOSE); </li> <li> 16. Netty concepts ChannelHandler IO event listener,handles outbound and / or inbound events. </li> <li> 17. Netty concepts ChannelInboundHandler Defines listener methods for incoming IO events </li> <li> 18. Netty concepts ChannelInboundHandler channelActive(ChannelHandlerContext ctx) channelRead(ChannelHandlerContext ctx, Object msg) channelReadComplete(ChannelHandlerContext ctx) channelInactive(ChannelHandlerContext ctx) .... </li> <li> 19. Netty concepts ChannelOutboundHandler Defines listener methods for outgoing IO events </li> <li> 20. Netty concepts ChannelOutboundHandler write(ChannelHandlerContext ctx, Object msg, ChannelPromise..) flush(ChannelHandlerContext ctx) </li> <li> 21. Netty concepts ChannelPipeline A List of ChannelHandlers that process incoming and outgoing events in a specific order </li> <li> 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. </li> <li> 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. </li> <li> 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); } </li> <li> 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(); </li> <li> 26. Netty architecture Chain of responsibility Incoming data flow through the ChannelPipeline - Framing ByteBuf - Decode frames - Decode frames in DTOs/POJOs </li> <li> 27. Netty architecture </li> <li> 28. Netty components Out of the box support for: Streaming compression Zlib SSL stream encryption HTTP WebSockets ProtocolBuffer </li> <li> 29. XMS server protocol layer </li> <li> 30. Example SSE / CORS </li> <li> 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 } </li> <li> 32. Example SSE / CORS </li> <li> 33. Thank you! Example code: https://github.com/jaapterwoerds/jfall-netty4 Further reading: https://nettio.io http://xms.me http://tech.ebuddy.com </li> </ul>

Recommended

View more >