Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

There are many components in Netty. In the previous section, we introduced many important components such as Bytebuf, EventLoopGroup, Channel, and so on. Components are associated with each other, and here’s how. Let’s take a look at Netty’s threading model:

Use the threading model to illustrate the relationships between components.

1. The EventLoop and EventLoopGroup

EventLoopGroup and EventLoop have a one-to-many relationship from the code level:

public interface EventLoop extends OrderedEventExecutor.EventLoopGroup {
    @Override
    EventLoopGroup parent(a);
}
Copy the code

EventLoop is a subclass of EventLoopGroup. Also equivalent to an EventLoopGroup.

Channel, ChannelPipeline, ChannelHandler, ChannelHandlerContext

It can be known from source code research (take NioServerSocketChannel as an example) that Channel will be created at the same time when a Channel is created. Here we can know that Channel and ChannelPipeline are one-to-one corresponding. The ChannelPipeline maintains a bidirectional list of ChannelHandlers that inherits the ChannelHandlerContext and ChannelHandler. When you go

When a ChannelPipeline adds a ChannelHandler, it first wraps the ChannelHandlerContext and then adds it to the context. The diagram is as follows:

The figure above illustrates the relationship between Channel, ChannelPipeline, ChannelHandler, ChannelHandlerContext.

If ChannelHandler is a singleton, ChannelHandler has a 1-to-many relationship with ChannelPipeline

  • A Channel and ChannelPipeline have a one-to-one relationship. When a Channel is created, it is bound to ChannelPipeline
  • ChannelPipeline has multiple ChannelHandlers, including ChannelInboundHandler, ChannelOutboundHandler and duplex handlers.
  • ChannelHandlerContext and ChannelHandler have a one-to-one relationship. ChannelHandler is wrapped as ChannelHandlerContext when it registers with ChannelPipeline.
  • The two-way linked list maintained in the ChannelPipeline is the ChannelHandlerContext.

I am ant back elephant, the article is helpful to you like to pay attention to me, the article has incorrect place please give correct comments ~ thank you