Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to register Channel in Netty component

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report