Upload
jaap-ter-woerds
View
5.318
Download
2
Tags:
Embed Size (px)
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.com
Citation preview
Building scalable network applications with Netty
Jaap ter Woerds@jaaptw
XMS pin: EBDDFF9A
The trouble with synchronous IO
The trouble with synchronous IO
The trouble with synchronous IO
Creating your custom server
Creating your custom server
Netty?
Unified, asynchronous programming model. All IO operations return immediately.
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 } }});
Netty?
Networking application framework with a modular architecture and quite some ready reusable
components.
When should I care about this?
Custom servers / networking applications / proxies
High concurrency
Netty concepts
Netty concepts
ByteBuf random accessable sequence of bytes
Abstraction over plain byte arrays and NIO buffers
Netty concepts
ByteBuf Use: encoding and decoding data
ByteBuf b = .b.writeInt(3).writeBoolean(true);
Netty concepts
Channel Link ('connection') to a resource that provides IO operations like read /
write
Netty concepts
ChannelChannel c = ..;
c.write(myMsg).addListener(CLOSE);
Netty concepts
ChannelHandler IO event listener,handles outbound and / or inbound events.
Netty concepts
ChannelInboundHandler Defines listener methods for incoming IO events
Netty concepts
ChannelInboundHandler
channelActive(ChannelHandlerContext ctx)channelRead(ChannelHandlerContext ctx, Object msg)
channelReadComplete(ChannelHandlerContext ctx)channelInactive(ChannelHandlerContext ctx)
....
Netty concepts
ChannelOutboundHandler Defines listener methods for outgoing IO events
Netty concepts
ChannelOutboundHandler
write(ChannelHandlerContext ctx, Object msg, ChannelPromise..)flush(ChannelHandlerContext ctx)
Netty concepts
ChannelPipelineA List of ChannelHandlers that process incoming and outgoing events
in a specific order
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.
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.
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); }
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();
Netty architecture
Chain of responsibilityIncoming data flow through the ChannelPipeline
- Framing ByteBuf- Decode frames
- Decode frames in DTOs/POJOs
Netty architecture
Netty components
Out of the box support for:Streaming compression Zlib
SSL stream encryptionHTTP
WebSocketsProtocolBuffer
XMS server protocol layer
Example SSE / CORS
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}
Example SSE / CORS
Thank you!
Example code:https://github.com/jaapterwoerds/jfall-netty4
Further reading:
https://nettio.io
http://xms.mehttp://tech.ebuddy.com