In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
one。 Take on the last article
Https://blog.51cto.com/483181/2121265
Let's continue to analyze doBind0 (regFuture, channel, localAddress, promise)
Private ChannelFuture doBind (final SocketAddress localAddress) {final ChannelFuture regFuture = initAndRegister (); final Channel channel = regFuture.channel (); if (regFuture.cause ()! = null) {return regFuture;} if (regFuture.isDone ()) {/ / At this point we know that the registration was complete and successful. ChannelPromise promise = channel.newPromise (); doBind0 (regFuture, channel, localAddress, promise); / / 3. What we are going to analyze here is return promise;} else {... Return promise;}}. 4 parameters of doBind02.1
As in the above code, doBind0 has four parameters regFuture, channel, localAddress, and promise, which are of the following types:
RegFuture: DefaultChannelPromise
Channel:NioServerSocketChannel
LocalAddress:SocketAddress
Promise:DefaultChannelPromise
Then go on and look at the code.
Private static void doBind0 (final ChannelFuture regFuture, final Channel channel, final SocketAddress localAddress, final ChannelPromise promise) {/ / This method is invoked before channelRegistered () is triggered. Give user handlers a chance to set up / / the pipeline in its channelRegistered () implementation. Channel.eventLoop () .execute (new Runnable () {@ Override public void run () {if (regFuture.isSuccess ()) {channel.bind (localAddress, promise) .addListener (ChannelFutureListener.CLOSE_ON_FAILURE);} else {promise.setFailure (regFuture.cause () });}
The doBind0 () code is simple and a call to channel.eventloop () executes a Runnable. Channel.eventloop () calls AbstractChannle.eventloop ()
@ Override public EventLoop eventLoop () {EventLoop eventLoop = this.eventLoop; if (eventLoop = = null) {throw new IllegalStateException ("channel not registered to an event loop");} return eventLoop;}
When initializing this.eventLoop, you can refer to the analysis of bind initialization in the previous article.
Https://blog.51cto.com/483181/2121265
@ Override public final void register (EventLoop eventLoop, final ChannelPromise promise) {.... AbstractChannel.this.eventLoop = eventLoop;.}
Its type is a NioEventLoop, so it just drops a Runnable task into its thread pool.
The inheritance diagram of NioEventLoop is as follows:
We can take a look at the NioEventLoop.execute (Runnable) method.
2.2 execute (Runnable) method
The implementation of this method is in SingleThreadEventExecutor.java.
Private final Queue taskQueue;@Override public void execute (Runnable task) {if (task = = null) {throw new NullPointerException ("task");} boolean inEventLoop = inEventLoop (); addTask (task); if (! inEventLoop) {startThread (); if (isShutdown () & & removeTask (task)) {reject () }} if (! addTaskWakesUp & & wakesUpForTask (task)) {wakeup (inEventLoop);}} protected void addTask (Runnable task) {if (task = = null) {throw new NullPointerException ("task");} if (! offerTask (task)) {reject (task) }} final boolean offerTask (Runnable task) {if (isShutdown ()) {reject ();} return taskQueue.offer (task);}
It puts the incoming Runnable object into a taskQueue queue.
Let's move on to the implementation in Runnable, channel.bind (xxxx).
Channel.eventLoop () .execute (new Runnable () {@ Override public void run () {... Channel.bind (localAddress, promise) .addListener (ChannelFutureListener.CLOSE_ON_FAILURE);}}); 2.3 channel.bind
The type of Channel is NioServerSocketChannel, and the implementation class of the bind method is in AbstractChannel.java.
Override public ChannelFuture bind (SocketAddress localAddress, ChannelPromise promise) {return pipeline.bind (localAddress, promise);}
The call is pipeline.bind (xxx), which, as we know, links to ChannelHandler's Context, with head and tail. Head is outbound,tail. It's inbound.
2.4 pipe.bind (xxx)
DefaultChannelPipeline.java
Override public final ChannelFuture bind (SocketAddress localAddress, ChannelPromise promise) {return tail.bind (localAddress, promise);}
The direct call is tail.bind. Move on.
AbstractChannelHandlerContext.java
@ Override public ChannelFuture bind (final SocketAddress localAddress, final ChannelPromise promise) {final AbstractChannelHandlerContext next = findContextOutbound (); EventExecutor executor = next.executor (); if (executor.inEventLoop ()) {next.invokeBind (localAddress, promise) } else {safeExecute (executor, new Runnable () {@ Override public void run () {next.invokeBind (localAddress, promise);}}, promise, null);} return promise;} private AbstractChannelHandlerContext findContextOutbound () {AbstractChannelHandlerContext ctx = this Do {ctx = ctx.prev;} while (! ctx.outbound); return ctx;}
It calls findContextOutbound (), and then calls its bind method, and as you can see from previous analysis, outbound is talking about head. So, we came to the head.bind method
2.5 head.bind
DefaultChannelPipeline.java
Override public void bind (ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {unsafe.bind (localAddress, promise);}
The call is unsafe.bind
2.6 unsafe.bind
AbstractChannel.java
Override public final void bind (final SocketAddress localAddress, final ChannelPromise promise) {assertEventLoop ();... Boolean wasActive = isActive (); try {doBind (localAddress) } catch (Throwable t) {...} if (! wasActive & & isActive ()) {invokeLater (new Runnable ()) {@ Override public void run () {pipeline.fireChannelActive () }});} safeSetSuccess (promise);}
Continue to watch doBind ()
2.7 doBind ()
DoBind is in NioServerSocketChannel.
@ Override protected void doBind (SocketAddress localAddress) throws Exception {if (PlatformDependent.javaVersion () > = 7) {javaChannel () .bind (localAddress, config.getBacklog ());} else {javaChannel () .socket () .bind (localAddress, config.getBacklog ());}}
This calls the interface of Java to bind the port we passed in.
So the whole bind logic is analyzed, and let's analyze the whole process and the relationship between the classes.
three。 Summary 3.1 the relationship between classes EventLoopGroup contains several EventLoop, the specific number we can specify manually, if not specified, the general default is cpu x 2. EventLoop inherits from SingleThreadEventLoop and represents a thread with a queue queue to store dropped Runnable tasks. We generally instantiate two EventLoopGroup, one is bossGroup, the other is workGroup,bossGroup to receive client connections, which can be described by channel. Then the channel is thrown to the workGroup, and workGroup is responsible for the operation behind the channel. 3.2 correspondence one EventLoopGroup contains multiple EventLoop, one EventLoop corresponds to multiple channel, and one channel corresponds to only one EventLoop. EventLoop is generally less than channel, just as there are generally fewer waiters than guests in restaurants. Therefore, it is also conceivable that if the business logic done by EventLoop on a channel is more complex, it may be too late to respond to other channel requests. A NioServerSocketChannel contains a pipeline and a unsafe object
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.