Download presentation
Presentation is loading. Please wait.
1
Netty
2
Agenda Intro to Netty and NIO Construct Use Summary
3
What and Why NIO Client-server framework Asynchronous Event-driven
Easy To Use API High Performance Quick Development
4
What do people use it for
Web services Instant messaging / push server Multiplayer game Storage database …
6
I/O Read Request Decode Request Process Encode Reply Send Reply
OIO NIO AIO Client Server
7
OIO InputStream OutputStream block on read/write
1 Thread: 1 Connection
8
Sample Code
9
Scalable Thread thread num, switch overhead
Divide and Conquer is usually the best approach any scalability goal
10
NIO Non-Blocking IO ByteBuffer byte container
Channel nexus which can do IO operation Selector event monitor which can watch multiple channels
11
Selector SelectableChannel::register(Selector, ops, attachment): SelectionKey selectedKeys: Set<SelectionKey> SelectionKey
12
ByteBuffer get, put position limit capacity flip
13
Reactor Pattern Reactor responds to IO events by dispatching the appropriate handle Handlers perform non-blocking actions Manage by binding handlers to events
14
Sample Code
15
Sample Code
16
Java NIO2 AsynchronousServerSocketChannel AsynchronousSocketChannel
read(ByteBuffer): Future<Integer> read(ByteBuffer, A, CompletionHandler<Int, ? super A> )
17
Sample Code
18
why not use jdk NIO directly
low level, hard, tricky concurrency is too hard bugs
19
netty structure ChannelPipeline ChannelHandler ByteBuf Bootstrap
Decoder/Encoder Thread Model
20
ChannelPipeline List of ChannelHandlers Chain Of Responsibility
Can modify on the fly
21
ChannelHandler Separation of concern Inbound Outbound
22
ByteBuf Capacity is expanded ReaderIndex WriterIndex ReferenceCounted
Pooled Unpooled
23
Codec Decoder ByteToMessageDecoder MessageToMessageDecoder
Encoder MessageToByteEncoder MessageToMessageEncoder Provided Codecs
24
Thread Model
25
Sample Code
26
Sample Code
27
use case WebSocket nifty
28
Push Message websocket metrics, auth handle, heartbeat
29
Nifty Nifty is an implementation of Thrift clients and servers on Netty. y hengyang/repos/nifty/browse
30
github.com/liuzhengyang
Questions github.com/liuzhengyang
31
Merci !
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.