Top Banner
NOTES ON NETTY (BASICS) RICK HIGHTOWER’S
20

Notes on Netty baics

Jan 25, 2017

Download

Technology

Rick Hightower
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Notes on Netty baics

NOTES ON NETTY (BASICS)

RICK HIGHTOWER’S

Page 2: Notes on Netty baics

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

Page 3: Notes on Netty baics

Great book on Netty

Page 4: Notes on Netty baics

http://www.infoq.com/presentations/apple-netty

Great talk about Netty

Page 5: Notes on Netty baics

BASIC CONCEPTS

Page 6: Notes on Netty baics

NETTY BASIC IO CHANNEL, EVENTLOOP, CHANNELFUTURE

• Channel - Sockets

• EventLoop - Control Flow

• ChannelFuture - async notification

Page 7: Notes on Netty baics

BASIC I/O OPERATIONSINTERFACE CHANNEL

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

• Channel reduces complexity

• EmbeddedChannel

• LocalServerChannel

• NioDatagramChannel

• NioSctpChannel

• NioSocketChannel

Page 8: Notes on Netty baics

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

Page 9: Notes on Netty baics

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

Page 10: Notes on Netty baics

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

Page 11: Notes on Netty baics

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

Page 12: Notes on Netty baics

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

Page 13: Notes on Netty baics

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

Page 14: Notes on Netty baics

ECHO EXAMPLE

Page 15: Notes on Netty baics

ECHO BACK MESSAGES ECHO SERVER CHANNEL HANDLER

Page 16: Notes on Netty baics

BINDS TO PORT REGISTERS CHANNEL HANDLERSERVER BOOTSTRAP

Page 17: Notes on Netty baics

SEND MESSAGES ON STARTUP AND THEN RECEIVEECHO CLIENT CHANNEL HANDLER

Page 18: Notes on Netty baics

CONNECT TO SERVER GIVEN HOST AND PORT, REGISTER HANDLER

ECHO CLIENT BOOTSTRAP

Page 19: Notes on Netty baics

Great book on Netty

Page 20: Notes on Netty baics

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