notes on netty baics

Post on 25-Jan-2017

1.537 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

NOTES ON NETTY (BASICS)

RICK HIGHTOWER’S

ABOUT RICK HIGHTOWERABOUT 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 OPERATIONSINTERFACE CHANNEL

• bind(), connect(), read(), write()

• Channel reduces complexity

• EmbeddedChannel

• LocalServerChannel

• NioDatagramChannel

• NioSctpChannel

• NioSocketChannel

EVENTLOOPINTERFACE 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

CHANNELFUTURECHANNEL 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 HANDLERCHANNEL 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

CHANNELPIPELINECHANNEL 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 DECODEDOUTBOUND 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 SERVERBOOTSTRAPPING

• 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 HANDLERSERVER BOOTSTRAP

SEND MESSAGES ON STARTUP AND THEN RECEIVEECHO CLIENT CHANNEL HANDLER

CONNECT TO SERVER GIVEN HOST AND PORT, REGISTER HANDLER

ECHO CLIENT BOOTSTRAP

Great book on Netty

ABOUT RICK HIGHTOWERABOUT 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

top related