1. Bootstrap and ServerBootstrap

Bootstrap is used to boot Netty. Bootstrap is the client bootstrap class, and ServerBootstrap is the ServerBootstrap class. Class inheritance:

Future vs. ChannelFuture

Operations in Netty are asynchronous, waiting to complete or registering listeners. Such as:

//b is ServerBootstrap instance ChannelFuture f = b.bind().sync();Copy the code

3, the Channel

  • Netty A component of network communication used for NETWORK I/O operations.
  • Through Channel, you can obtain the status and network configuration parameters of the Channel currently connected to Wanglue.
  • A Channel provides asynchronous network IO operations, and immediately returns to the ChannelFuture. The result can be obtained by registering listening or synchronous waiting.

Channels are classified into different types according to different protocols and blocking types:

The name can also be roughly guessed the role of the respective.

4, the Selector

Netty based on Java nio. Channels. The Selector object implementation IO multiplexing, through the Selector Channel events of a thread can monitor multiple connections. When a Channel is registered with a Selector, the mechanism inside the Selector automatically and continuously selects whether the registered Channel has IO events (readable, writable, network connection completed, etc.) in place.

5, ChannelHandler

ChannelHandler is a core service interface used to process I/o events or intercept I/o operations and forward them to the next handler in the ChannelPipeline. Post an implementation class diagram:

6. Pipeline and ChannelPipeline

  • A ChannelPipeline is a collection of handlers that handle and intercept outbound and inbound events and operations.
  • ChannelPipeline implements an interception filter pattern that allows the user to control how events are handled.
  • In Netty, each Channel has only one Channel pipeline corresponding to it.

A Channel contains a ChannelPipeline, which maintains a two-way list of ChannelHandlerContext, And each ChannelHandlerContext is associated with a ChannelHandler.

7, ChannelHandlerContext

Saves all context information associated with a Channel, along with a ChannelHandler.

8 ChannelOption.

After Netty creates a Channel instance, you can set parameters using ChannelOption.

NioEventLoop and NioEventLoopGroup

NioEventLoopGroup can be understood as a thread pool, and NioEventLoop can be understood as a thread. Each EventLoop corresponds to a Selector, which is responsible for handling events on multiple channels.

The first boss EventLoopGroup assigns an EventLoop that creates the connection request that Channels pass in. Once the connection is accepted, the second Work EventLoopGroup assigns an EventLoop to its Channel.