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 operationC...
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

Channel

Channel 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, Objec...
Netty concepts

ChannelOutboundHandler

Defines listener methods for outgoing IO events
Netty concepts
ChannelOutboundHandler
write(ChannelHandlerContext ctx, Object msg, ChannelPromise..)
flush(ChannelHandlerC...
Netty concepts

ChannelPipeline

A List of ChannelHandlers that process incoming and outgoing events
in a specific order
Netty concepts

ChannelInitializer

Configures the pipeline for a Channel. In fact, a special case of
ChannelInboundHandle...
Netty concepts

ChannelInitializer

Configures the pipeline for a Channel. In fact, a special case of
ChannelInboundHandle...
Netty concepts
ChannelInitializer
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p...
Netty concepts
Bootstrap
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoop...
Netty architecture
Chain of responsibility
Incoming data flow through the ChannelPipeline
- Framing ByteBuf
- Decode frame...
Netty architecture
Netty components
Out of the box support for:
Streaming compression Zlib
SSL stream encryption
HTTP
WebSockets
ProtocolBuff...
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 ab...
Example SSE / CORS
Thank you!

Example code:
https://github.com/jaapterwoerds/jfall-netty4
Further reading:
https://nettio.io
http://xms.me
h...
Upcoming SlideShare
Loading in …5
×

Building scalable network applications with Netty

1,181 views
897 views

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,181
On SlideShare
0
From Embeds
0
Number of Embeds
63
Actions
Shares
0
Downloads
13
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Building scalable network applications with Netty

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

×