In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what is the execution order of handler". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the order of handler execution?"
In order to illustrate this problem, we have added two encoder, namely MavlinkMsg2BytesEncoder and FixLengthEncoder, which are used to convert a protocol object into an byte array, and the other is to add a fixed length setting for client parsing.
Still follow the old method, set breakpoints, and then check the order in which they are called.
NettyServerHandler.messageReceived is the end point where we receive the downstream message, and we perform the return of the message directly here.
First go to the NioSocketChannel.write method, and here is the code
@ Override
Public ChannelFuture write (Object msg) {
Return pipeline.write (msg)
}
The write method is defined in AbstractChannel and is the ancestor of all channel classes in netty.
In this method, execution starts to execute each handler in the pipeline in turn
Here is the DefaultChannelPipeline.write method
@ Override
Public ChannelFuture write (Object msg) {
Return tail.write (msg)
}
If you remember correctly, the downlink process starts from head.fireChannelRead, while in the uplink process, it starts with tail. In fact, head and tail are the head and tail nodes of the two-way linked list generated by pipeline by default during initialization, and they do not complete any actual work. Here from tail is an uplink process, it should be said to be very logical.
Next, let's look at the DefaultChannelHandlerContext.write method.
@ Override
Public ChannelFuture write (Object msg, ChannelPromise promise) {
DefaultChannelHandlerContext next = findContextOutbound (MASK_WRITE)
Next.invoker.invokeWrite (next, msg, promise)
Return promise
}
This is also the code we analyzed earlier to find the prev node in the two-way linked list.
Then there is the ChannelHandlerInvokerUtil.invokeWriteNow method.
Public static void invokeWriteNow (ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
Try {
Ctx.handler () .write (ctx, msg, promise)
} catch (Throwable t) {
NotifyOutboundHandlerException (t, promise)
}
}
Then there is the MessageToByteEncoder.write method.
Public void write (ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
ByteBuf buf = null
Try {
If (acceptOutboundMessage (msg)) {
@ SuppressWarnings ("unchecked")
I cast = (I) msg
If (preferDirect) {
Buf = ctx.alloc () .ioBuffer ()
} else {
Buf = ctx.alloc () .heapBuffer ()
}
Try {
Encode (ctx, cast, buf)
} finally {
ReferenceCountUtil.release (cast)
}
If (buf.isReadable ()) {
Ctx.write (buf, promise)
} else {
Buf.release ()
Ctx.write (Unpooled.EMPTY_BUFFER, promise)
}
Buf = null
} else {
Ctx.write (msg, promise)
}
} catch (EncoderException e) {
Throw e
} catch (Throwable e) {
Throw new EncoderException (e)
} finally {
If (buf! = null) {
Buf.release ()
}
}
}
A dynamic buffer is allocated here, and then the encoder method is called.
If (preferDirect) {
Buf = ctx.alloc () .ioBuffer ()
} else {
Buf = ctx.alloc () .heapBuffer ()
}
Please note this code, the concept here refers to whether the current buffer allocation is allocated from the system io or from the jvm heap, each has its own advantages and disadvantages, which we will discuss specifically in the future.
In the end, it's our encode method.
Protected void encode (ChannelHandlerContext ctx, MAVLinkMessage msg, ByteBuf out) throws Exception {
MAVLinkPacket packet = msg.pack ()
Byte [] buf = packet.encodePacket ()
Out.writeBytes (buf)
}
However, the procedure for calling encoder is basically the same as before. I will not repeat it here.
At this point, I believe you have a deeper understanding of "what is the order of handler execution?". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.