• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Building scalable network applications with Netty
 

Building scalable network applications with Netty

on

  • 385 views

 

Statistics

Views

Total Views
385
Views on SlideShare
366
Embed Views
19

Actions

Likes
1
Downloads
5
Comments
0

1 Embed 19

http://www.nljug.org 19

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Building scalable network applications with Netty Building scalable network applications with Netty Presentation Transcript

    • 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 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, 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 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 ChannelInboundHandler which creates the handlers for a channel whenerver a new channel is registered.
    • 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.
    • 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 responsibility Incoming 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 encryption HTTP WebSockets ProtocolBuffer
    • 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! @Override public 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.me http://tech.ebuddy.com