NOTES ON
NETTY (BASICS)
RICK HIGHTOWER’S
ABOUT RICK HIGHTOWER
ABOUT RICK
• Implemented Microservices, Vert.x/Netty at massive scale
• Author of QBit, microservices lib and Boon, Json parser and utility lib
• Founder of Mammatus Technology
• Rick’s Twitter, Rick’s LinkedIn, Rick’s Blog, Rick’s Slideshare
Great book on Netty
http://www.infoq.com/presentations/apple-netty
Great talk about Netty
BASIC
CONCEPTS
NETTY BASIC IO
CHANNEL, EVENTLOOP, CHANNELFUTURE
• Channel - Sockets
• EventLoop - Control Flow
• ChannelFuture - async notification
BASIC I/O OPERATIONS
INTERFACE CHANNEL
• bind(), connect(), read(), write()
• Channel reduces complexity
• EmbeddedChannel
• LocalServerChannel
• NioDatagramChannel
• NioSctpChannel
• NioSocketChannel
EVENTLOOP
INTERFACE EVENTLOOP
• Core abstraction for handling connection events
• EventLoopGroup contains one or more EventLoops
• EventLoop is bound to a single thread
• IO Events handles by single thread
• Channel is registered to a single EventLoop
• eliminates most needs for synchronization
• EventLoop can handle many channels
CHANNELFUTURE
CHANNEL FUTURE
• ChannelFuture to handle async operations
• Determine results at a later time
• addListener() to register callback for completion
• When it gets executed depends but they are executed in order of
receipt
CHANNEL HANDLER
CHANNEL HANDLER
• ChannelHandler used to implement application logic
• Handles inbound and outbound data
• methods are triggered by network events
• ChannelInboundHandler sub-interface handles incoming events
• gets implemented often for application logic
• allows you to flush data going to client
• your business logic will often rely here
CHANNELPIPELINE
CHANNEL PIPELINE
• ChannelPipeline
• forms chain of ChannelHandlers
• Channel is assigned a ChannelPipeline
• ChannelHandlers are registered with a ChannelIntializer
• ChannelHandlers operate on events by processing them and then
passing the event to the next handler in the chain
INBOUND MESSAGES GET DECODED
OUTBOUND MESSAGES GET ENCODED
ENCODERS AND DECODERS
• Inbound messages get DECODED
• Outbound messages get ENCODED
• Converting from bytes to Java objects or serializing Java objects to bytes
• Examples: ByteToMessageDecoder, MessageToByteEncoder,
ProtobufEncoder, ProtobufDecoder
• encode(), decode()
• Encoded messages get passed to next ChannelInboundHandler
• Decode messages get passed to next ChannelOutboundHandler
STARTING UP A CLIENT OR A SERVER
BOOTSTRAPPING
• Bootstrap - client bootstrap
• uses one thread
• ServerBootstrap - server bootstrap
• binds to a port
• uses two threads
• needs two channels one for listening and a second channel for handling
clients connections
• first channel creates Channels for incoming connections which get
assigned to an EventLoop
ECHO EXAMPLE
ECHO BACK MESSAGES
ECHO SERVER CHANNEL HANDLER
BINDS TO PORT REGISTERS CHANNEL HANDLER
SERVER BOOTSTRAP
SEND MESSAGES ON STARTUP AND THEN RECEIVE
ECHO CLIENT CHANNEL HANDLER
CONNECT TO SERVER GIVEN HOST AND PORT, REGISTER
HANDLER
ECHO CLIENT BOOTSTRAP
Great book on Netty
ABOUT RICK HIGHTOWER
ABOUT RICK
• Implemented Microservices, Vert.x/Netty at massive scale
• Author of QBit, microservices lib and Boon, Json parser and utility lib
• Founder of Mammatus Technology
• Rick’s Twitter, Rick’s LinkedIn, Rick’s Blog, Rick’s Slideshare

Notes on Netty baics

  • 1.
  • 2.
    ABOUT RICK HIGHTOWER ABOUTRICK • Implemented Microservices, Vert.x/Netty at massive scale • Author of QBit, microservices lib and Boon, Json parser and utility lib • Founder of Mammatus Technology • Rick’s Twitter, Rick’s LinkedIn, Rick’s Blog, Rick’s Slideshare
  • 3.
  • 4.
  • 5.
  • 6.
    NETTY BASIC IO CHANNEL,EVENTLOOP, CHANNELFUTURE • Channel - Sockets • EventLoop - Control Flow • ChannelFuture - async notification
  • 7.
    BASIC I/O OPERATIONS INTERFACECHANNEL • bind(), connect(), read(), write() • Channel reduces complexity • EmbeddedChannel • LocalServerChannel • NioDatagramChannel • NioSctpChannel • NioSocketChannel
  • 8.
    EVENTLOOP INTERFACE EVENTLOOP • Coreabstraction for handling connection events • EventLoopGroup contains one or more EventLoops • EventLoop is bound to a single thread • IO Events handles by single thread • Channel is registered to a single EventLoop • eliminates most needs for synchronization • EventLoop can handle many channels
  • 9.
    CHANNELFUTURE CHANNEL FUTURE • ChannelFutureto handle async operations • Determine results at a later time • addListener() to register callback for completion • When it gets executed depends but they are executed in order of receipt
  • 10.
    CHANNEL HANDLER CHANNEL HANDLER •ChannelHandler used to implement application logic • Handles inbound and outbound data • methods are triggered by network events • ChannelInboundHandler sub-interface handles incoming events • gets implemented often for application logic • allows you to flush data going to client • your business logic will often rely here
  • 11.
    CHANNELPIPELINE CHANNEL PIPELINE • ChannelPipeline •forms chain of ChannelHandlers • Channel is assigned a ChannelPipeline • ChannelHandlers are registered with a ChannelIntializer • ChannelHandlers operate on events by processing them and then passing the event to the next handler in the chain
  • 12.
    INBOUND MESSAGES GETDECODED OUTBOUND MESSAGES GET ENCODED ENCODERS AND DECODERS • Inbound messages get DECODED • Outbound messages get ENCODED • Converting from bytes to Java objects or serializing Java objects to bytes • Examples: ByteToMessageDecoder, MessageToByteEncoder, ProtobufEncoder, ProtobufDecoder • encode(), decode() • Encoded messages get passed to next ChannelInboundHandler • Decode messages get passed to next ChannelOutboundHandler
  • 13.
    STARTING UP ACLIENT OR A SERVER BOOTSTRAPPING • Bootstrap - client bootstrap • uses one thread • ServerBootstrap - server bootstrap • binds to a port • uses two threads • needs two channels one for listening and a second channel for handling clients connections • first channel creates Channels for incoming connections which get assigned to an EventLoop
  • 14.
  • 15.
    ECHO BACK MESSAGES ECHOSERVER CHANNEL HANDLER
  • 16.
    BINDS TO PORTREGISTERS CHANNEL HANDLER SERVER BOOTSTRAP
  • 17.
    SEND MESSAGES ONSTARTUP AND THEN RECEIVE ECHO CLIENT CHANNEL HANDLER
  • 18.
    CONNECT TO SERVERGIVEN HOST AND PORT, REGISTER HANDLER ECHO CLIENT BOOTSTRAP
  • 19.
  • 20.
    ABOUT RICK HIGHTOWER ABOUTRICK • Implemented Microservices, Vert.x/Netty at massive scale • Author of QBit, microservices lib and Boon, Json parser and utility lib • Founder of Mammatus Technology • Rick’s Twitter, Rick’s LinkedIn, Rick’s Blog, Rick’s Slideshare