Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to register Channel in Netty component

Shulou Source: shulou.com Published: 2022-06-02 05:31:35 10月02日 Update

This article is to share with you about how to register Channel in Netty components, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1. Overview of EventLoopGroup class diagram

NioEventLoopGroup is instantiated in the client sample code, and then the instantiation process is analyzed.

EventLoopGroup workerGroup = new NioEventLoopGroup ()

Bootstrap b = new Bootstrap ()

B.group (workerGroup)

From the following class diagram structure, the io.netty.util.concurrent.AbstractEventExecutorGroup branch is mainly responsible for the processing of multithreaded tasks; the io.netty.channel.EventLoopGroup branch is mainly responsible for Channel-related registration. MultithreadEventExecutorGroup and MultithreadEventLoopGroup inherit and implement the above AbstractEventExecutorGroup and EventLoopGroup respectively, merging the functions they are responsible for.

Second, the interpretation of constructor

Constructor function

Number of nThreads:eventLoopThreads threads. The default value is 0, which is twice the number of CPU cores, which can be specified by parameter io.netty.eventLoopThreads.

Executor: default ThreadPerTaskExecutor

SelectorProvider default SelectorProvider.provider (), which is used to enable Selector and Channel

SelectStrategyFactory:SelectStrategy factory class, default DefaultSelectStrategyFactory

EventExecutorChooserFactory:EventExecutor selector, default is DefaultEventExecutorChooserFactory

Initialize the EventExecutor array

Code interpretation

EventExecutor [] children: the array size is nThreads, and the default is the number of CPU cores multiplied by 2.

EventExecutor inherits EventExecutorGroup as the thread framework class Executor in essence

Children [I]: the data element is EventLoop, which in this case is NioEventLoop.

NioEventLoop class diagram

NioEventLoop inherits SingleThreadEventLoop,SingleThreadEventLoop and implements both EventExecutor and EventLoop. That is, NioEventLoop has the ability of the thread class framework to handle multithreaded tasks and the ability to handle Channel.

Note: the element of the EventExecutor array children in this article is that NioEventLoop,NioEventLoop has both threading framework capabilities and Channel registration capabilities.

4. EventExecutor selector

The third part analyzes the initialization of EventExecutor [] children, but how to select one of the elements when using it?

The following line of code is used to initialize the EventExecutorChooser during the initialization process.

Chooser = chooserFactory.newChooser (children)

EventExecutorChooser class diagram structure

Select strategy

@ 1 if the array length is to the power of 2, select PowerOfTwoEventExecutorChooser and use executors [idx.getAndIncrement () & executors.length-1] when selecting EventExecutor.

@ 2 if the array length is not to the power of 2, select GenericEventExecutorChooser,executors [Math.abs (idx.getAndIncrement ()% executors.length)].

V. Channel registration

Channel Registration Portal

Select EventLoop

This article is NioEventLoop

Bind Channel to EventExecutor

Bind Channel to EventExecutor (NioEventLoop) through DefaultChannelPromise

Register Channel with Selector

The above is how to register Channel in Netty components. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Choices threads arrays capabilities processing code elements frameworks components tasks functions branches and at the same time instances more kernels knowledge examples articles structures Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology Apple NVidia Redmi