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

fifty-seven。 Netty source code analysis-server-side startup ServerBootstrap initialization

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

one。 Start 1.1 previous article

The instantiation analysis of the previous NioEventLoopGroup continues.

Https://blog.51cto.com/483181/2118817

The purpose of this blog is to analyze "2. ServerBootstrap initialization" as follows:

EventLoopGroup bossGroup = new NioEventLoopGroup (); EventLoopGroup workerGroup = new NioEventLoopGroup (); try {ServerBootstrap b = new ServerBootstrap () / / 2. ServerBootstrap initializes b.group (bossGroup, workerGroup) / / 2. ServerBootstrap initializes .channel (NioServerSocketChannel.class) .option (ChannelOption.SO_BACKLOG) New LoggingHandler (LogLevel.INFO) .childHandler (new ChannelInitializer () {@ Override protected void initChannel (SocketChannel ch) throws Exception {}}) ChannelFuture f = b.bind (port). Sync (); / 3. Bind f.channel (). CloseFuture (). Sync ();} catch (Exception e) {e.printStackTrace ();} finally {bossGroup.shutdownGracefully (); workerGroup.shutdownGracefully ();} two. ServerBootstrap2.1 ServerBootstrap inheritance diagram

2.2 ServerBootstrap constructor public ServerBootstrap () {}

ServerBootstrap provides a no-parameter constructor, which is actually a bit strange, because a network service like this must adapt to different scenarios, so there must be constructors with a lot of parameters.

At this point, it is precisely because there are so many parameters to adapt that ServerBootstrap provides a no-parameter constructor and then uses the constructor pattern to solve this problem.

As follows:

Public ServerBootstrap childHandler (ChannelHandler childHandler) {if (childHandler = = null) {throw new NullPointerException ("childHandler");} this.childHandler = childHandler; return this;} 2.3 ServerBootstrap.grouppublic ServerBootstrap group (EventLoopGroup parentGroup, EventLoopGroup childGroup) {super.group (parentGroup); if (childGroup = = null) {throw new NullPointerException ("childGroup") } if (this.childGroup! = null) {throw new IllegalStateException ("childGroup set already");} this.childGroup = childGroup; return this;} parent group (xx) public B group (EventLoopGroup group) {this.group = group; return self ();}

Two EventLoopGroup are passed in, that is, the NioEventLoopGroup mentioned in the previous article, one is bossGroup, the other is workerGroup.

Where bossGroup has its parent class group attribute

WorkerGroup is stored in the childGroup attribute of ServerBootstrap, but the difference between them is not known for the time being.

Continue to watch b.channel (NioServerSocketChannel.class)

2.4Setting channel

The code is in AbstractBootstrap, the parent class of ServerBootstrap, as follows:

Public B channel (Class

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