Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-03 05:24:32 11月24日 Update

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

Tags: Methods objects types functions analysis code properties two factories patterns previous different that is parameters variables just scenarios threads affirmations services Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Linux macOS Huawei