Bootstrap、ServerBootstrap
Bootstrap guide means, usually by the application of a Netty Bootstrap a start, the main role is to configure the entire Netty program, various components in series, the Bootstrap Netty boot class is the class of the client program, the service end of ServerBootstrap boot class.
Common methods:
Method name | description |
---|---|
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) | A server, provided two EventLoopGroup |
public B group(EventLoopGroup group) | A client, provided a EventLoopGroup |
public B channel(Class<? extends C> channelClass) | Set the service end of the tunnel to achieve |
public B option(ChannelOption option, T value) | To add the configuration to ServerChannel |
public ServerBootstrap childOption(ChannelOption childOption, T value) | To add to the received channel configuration |
public ServerBootstrap childHandler(ChannelHandler childHandler) | Processing to set the business class |
public ChannelFuture bind(int inetPort) | To set service port number |
public ChannelFuture connect(String inetHost, int inetPort) | A client connected to the server |
Future、ChannelFuture
Netty all IO operations are asynchronous, can not know immediately whether the message is processed correctly. But after a while and so it can be completed or executed directly register a listener specific implementation is through the Future and ChannelFutures, they can register a listener, when an operation success or failure of the monitoring will be triggered automatically registered to listen for events.
Common methods:
Method name | description |
---|---|
Channel channel() | Returns the current channel being IO operations |
ChannelFuture sync() | Waiting for asynchronous operations are completed |
Channel
Channel assembly is Netty network communication, the network can be used to perform I / O operations.
State of the channel current through Channel network connection available.
Configuration parameters (e.g., the receive buffer size) by Channel network connection available, Channel network providing asynchronous I / O operation (e.g., to establish a connection, reading and writing, bound port), an asynchronous call means that any I / O calls will be returns immediately, and is not guaranteed at the end of the invocation of the requested I / O operation has been completed.
Callback notification immediately return a call to the caller ChannelFuture example, by registering listeners to ChannelFuture, you can be successful I / O operations, cancel or fail. Support channel associated I / O operation corresponding to the processing procedure, different protocols, different types of connection blocking Channel types have different corresponding, common Channel Type:
- NioSocketChannel: asynchronous client TCP Socket connection
- NioServerSocketChannel: asynchronous server side TCP Socket connection
- NioDatagramChannel: Asynchronous UDP connection
- NioSctpChannel: asynchronous client Sctp connection
- NioSctpServerChannel: asynchronous Sctp server connection, the channels cover the UDP and TCP network file IO and IO.
Selector
Selector object Netty Based on I / O multiplexer, a thread may listen via Channel Selector plurality of connection events.
When a Selector to register the Channel, the internal mechanism Selector can automatically continue to query (Select) whether these have been registered with Channel-ready I / O event (such as read, write, network connection is completed, etc.), so the program can simply use a thread to efficiently manage multiple Channel.
ChannelHandler
Is an interface to process I / O events or intercepting I / O operations, and forwards it to the next processing program which the ChannelPipeline (service processing chain) was added.
It does not in itself provide many ways, because there are many ways this interface need to implement, easy to use period, can inherit its subclasses.
Common Interface:
- ChannelInboundHandler: Inbound I / O events for processing.
- ChannelOutboundHandler: a station for processing I / O operations.
Common sub-categories:
- ChannelInboundHandlerAdapter: Inbound I / O events for processing.
- ChannelOutboundHandlerAdapter: a station for processing I / O operations.
- ChannelDuplexHandler: inbound and outbound events for processing.
Pipeline、ChannelPipeline
ChannelPipeline is a collection of Handler, which is responsible for processing and intercept inbound or outbound events and operations, equivalent to a Netty throughout the chain. (Can be understood: ChannelPipeline ChannelHandler is stored in List, for processing or intercepting inbound and outbound events of operation Channel).
ChannelPipeline implements an advanced form of interception filter mode, the user has full control event handling, and how Channel in each of ChannelHandler interact with each other.
Each Channel has one and only one corresponding ChannelPipeline in Netty, the relation on the compositions thereof as shown below:
Process analysis:
-
A Channel contains a ChannelPipeline, and ChannelPipeline he also maintains a doubly linked list composed by the ChannelHandlerContext, and each in turn is associated with a ChannelHandlerContext ChannelHandler
-
Inbound and outbound events events in a doubly linked list, the inbound event from the list will be passed back to the last head of the handler inbound, outbound events from the list passed to the handler before the tail forward the most out of a station, two without disturbing each type of handler
Common methods:
Method name | description |
---|---|
ChannelPipeline addFirst(ChannelHandler… handlers) | The service processing a class (Handler) was added to the first position in the chain |
ChannelPipeline addLast(ChannelHandler… handlers) | The service processing a class (Handler) was added to a final position in the chain |
ChannelHandlerContext
All relevant context information stored Channel, while a ChannelHandler associated objects, namely ChannelHandlerContext contains a specific event handler ChannelHandler, while ChannelHandlerContext also bound pipeline and corresponding information Channel, easy to ChannelHandler call.
Common methods:
Method name | description |
---|---|
ChannelFuture close() | Close the channel |
ChannelOutboundInvoker flush() | Refresh |
ChannelFuture writeAndFlush(Object msg) | Writing data to a next currently ChannelPipeline ChannelHandler ChannelHandler start process (Outbound) |
ChannelOption
After creating Netty Channel instance, generally need to set parameters ChannelOption, common parameters are as follows:
parameter name | description |
---|---|
ChannelOption.SO_BACKLOG | Corresponding to the TCP / IP protocol listen function of backlog parameter, the server can be used to initialize the connection queue size. Processing server client connection requests are processed sequentially, so that at the same time can handle a client connection. When a plurality of client, the server will not handle the client connection request in a queue waiting to be processed, backlog parameter specifies the size of the queue |
ChannelOption.SO_KEEPALIVE | Always connected active |
EventLoopGroup、NioEventLoopGroup
EventLoopGroup is an interface, NioEventLoopGroup is a subclass of EventLoopGroup, EventLoopGroup EventLoop abstract is a group, in order to better utilize Netty multicore CPU resources, generally have a plurality EventLoop simultaneously, each instance EventLoop maintains a Selector.
EventLoopGroup next provides an interface, which may be obtained from a group which EventLoop according to certain rules to process the task. On the server side programming Netty, we are generally required to provide two EventLoopGroup, such as BossEventLoopGroup and WorkerEventLoopGroup.
Usually a service port that is a ServerSocketChannel corresponds to a Selector and a EventLoop thread. BossEventLoop responsible for receiving client is connected to and SocketChannel WorkerEventLoopGroup IO processing is performed, as shown below:
BossEventLoopGroup usually EventLoop a single-threaded, EventLoop maintains a register of constantly polling Selector Selector instance BossEventLoop ServerSocketChannel will separate the connection event
Usually OP_ACCEPT event, then received SocketChannel to WorkerEventLoopGroup
WorkerEventLoopGroup will be chosen by the next of which will be registered to a EventLoop this SocketChannel to its maintenance Selector and its subsequent processing IO event
Common methods:
parameter name | description |
---|---|
public NioEventLoopGroup() | Construction method |
public Future<?> shutdownGracefully() | Disconnected, close the thread |
Unpooled class
Unpooled Netty is designed to provide a buffer operation (i.e. Netty data container) tools.
Common methods:
parameter name | description |
---|---|
public static ByteBuf copiedBuffer(CharSequence string, Charset charset) | ByteBuf by returning a given data object is a character encoding and similar but differentiated in NIO ByteBuffer |