building scalable network applications with netty (as presented on nljug jfall 2013)
DESCRIPTION
The presentation I gave on creating server application with Netty, including an example of how it is used to power XMS the mobile messaging platform of eBuddy. Example code is on github: https://github.com/jaapterwoerds/jfall-netty4 More information on eBuddy: xms.me and tech.ebuddy.comTRANSCRIPT
![Page 1: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/1.jpg)
Building scalable network applications with Netty
Jaap ter Woerds@jaaptw
XMS pin: EBDDFF9A
![Page 2: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/2.jpg)
The trouble with synchronous IO
![Page 3: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/3.jpg)
The trouble with synchronous IO
![Page 4: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/4.jpg)
The trouble with synchronous IO
![Page 5: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/5.jpg)
Creating your custom server
![Page 6: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/6.jpg)
Creating your custom server
![Page 7: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/7.jpg)
Netty?
Unified, asynchronous programming model. All IO operations return immediately.
![Page 8: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/8.jpg)
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 } }});
![Page 9: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/9.jpg)
Netty?
Networking application framework with a modular architecture and quite some ready reusable
components.
![Page 10: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/10.jpg)
When should I care about this?
Custom servers / networking applications / proxies
High concurrency
![Page 11: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/11.jpg)
Netty concepts
![Page 12: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/12.jpg)
Netty concepts
ByteBuf random accessable sequence of bytes
Abstraction over plain byte arrays and NIO buffers
![Page 13: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/13.jpg)
Netty concepts
ByteBuf Use: encoding and decoding data
ByteBuf b = .b.writeInt(3).writeBoolean(true);
![Page 14: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/14.jpg)
Netty concepts
Channel Link ('connection') to a resource that provides IO operations like read /
write
![Page 15: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/15.jpg)
Netty concepts
ChannelChannel c = ..;
c.write(myMsg).addListener(CLOSE);
![Page 16: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/16.jpg)
Netty concepts
ChannelHandler IO event listener,handles outbound and / or inbound events.
![Page 17: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/17.jpg)
Netty concepts
ChannelInboundHandler Defines listener methods for incoming IO events
![Page 18: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/18.jpg)
Netty concepts
ChannelInboundHandler
channelActive(ChannelHandlerContext ctx)channelRead(ChannelHandlerContext ctx, Object msg)
channelReadComplete(ChannelHandlerContext ctx)channelInactive(ChannelHandlerContext ctx)
....
![Page 19: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/19.jpg)
Netty concepts
ChannelOutboundHandler Defines listener methods for outgoing IO events
![Page 20: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/20.jpg)
Netty concepts
ChannelOutboundHandler
write(ChannelHandlerContext ctx, Object msg, ChannelPromise..)flush(ChannelHandlerContext ctx)
![Page 21: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/21.jpg)
Netty concepts
ChannelPipelineA List of ChannelHandlers that process incoming and outgoing events
in a specific order
![Page 22: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/22.jpg)
Netty concepts
ChannelInitializerConfigures 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.
![Page 23: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/23.jpg)
Netty concepts
ChannelInitializerConfigures 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.
![Page 24: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/24.jpg)
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); }
![Page 25: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/25.jpg)
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();
![Page 26: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/26.jpg)
Netty architecture
Chain of responsibilityIncoming data flow through the ChannelPipeline
- Framing ByteBuf- Decode frames
- Decode frames in DTOs/POJOs
![Page 27: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/27.jpg)
Netty architecture
![Page 28: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/28.jpg)
Netty components
Out of the box support for:Streaming compression Zlib
SSL stream encryptionHTTP
WebSocketsProtocolBuffer
![Page 29: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/29.jpg)
XMS server protocol layer
![Page 30: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/30.jpg)
Example SSE / CORS
![Page 31: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/31.jpg)
Example SSE / CORS
In the last handler we deal with normal POJO and handle an incoming request without actually caring about the connection layer!
@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) { IncomingMessagePojo m = (IncomingMessagePojo) msg; handle(m);// Login / Send a text message/ Handle create group}
![Page 32: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/32.jpg)
Example SSE / CORS
![Page 33: Building scalable network applications with Netty (as presented on NLJUG JFall 2013)](https://reader035.vdocuments.site/reader035/viewer/2022081401/5562c19fd8b42a595e8b50ee/html5/thumbnails/33.jpg)
Thank you!
Example code:https://github.com/jaapterwoerds/jfall-netty4
Further reading:
https://nettio.io
http://xms.mehttp://tech.ebuddy.com