In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to store handler in pipeline". In daily operation, I believe many people have doubts about how to store handler in pipeline. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how to store handler in pipeline". Next, please follow the editor to study!
Solve two problems today
1. How exactly is handler stored in pipeline
2. In the process of traversing handler, different handler will be called according to different event, and this is how to achieve this.
Let's start with the first question:
TailHandler tailHandler = new TailHandler ()
Tail = new DefaultChannelHandlerContext (this, null, generateName (tailHandler), tailHandler)
HeadHandler headHandler = new HeadHandler (channel.unsafe ())
Head = new DefaultChannelHandlerContext (this, null, generateName (headHandler), headHandler)
Head.next = tail
Tail.prev = head
This is a piece of code from the DefaultChannelPipeline constructor. As we mentioned earlier, there is a bi-directional linked list in pipeline. Here, the head and tail of the bi-directional linked list are initialized.
Then we add handler to the pipeline through the addlast method
DefaultChannelHandlerContext prev = tail.prev
NewCtx.prev = prev
NewCtx.next = tail
Prev.next = newCtx
Tail.prev = newCtx
Here is to add a new handler to the two-way linked list.
Another question is, what are the elements in the two-way linked list?
DefaultChannelHandlerContext newCtx =
New DefaultChannelHandlerContext (this, invoker, name, handler)
Need to see this, simple understanding, put handler into a new DefaultChannelHandlerContext. So what exactly is its function?
Through the study of the code, DefaultChannelHandlerContext is actually a dynamic proxy class, and our handler is the class being proxied. There are two points that need to be explained. Let's take a look at the following code
@ Override
Public ChannelFuture write (Object msg, ChannelPromise promise) {
DefaultChannelHandlerContext next = findContextOutbound (MASK_WRITE)
Next.invoker.invokeWrite (next, msg, promise)
Return promise
}
First of all, don't be confused by the write method, which does perform the action of write and the action of encode contained in write. Note, however, that this write action is not the write of the current element, but the write of handler in the next element in the linked list. At the end of the write method of handler, next.write is executed, and here the write of handler in the next element is executed. By analogy, the traversal of the linked list is realized.
Second: don't be fooled by invoke, the reflection mechanism of java is not used here, because no matter what kind of handler you pass in, it comes from ChannelHandler, the unified ancestor. You can call different methods as needed. It has nothing to do with reflection.
Then there is the second question:
First of all, let's make it clear that all handler come from the same ancestor ChannelHandler, and the different methods in ChannelHandler correspond to different events in netty.
When initializing bootstrap, we added a lot of handler, some of which are responsible for downlink decode and some of which are responsible for uplink encode. Of course, we hope that when downlink, we only enter downlink-related handler and uplink-related handler. So how exactly did you do it? Look at the following code. In view of the space problem, only one of the paragraphs is excerpted:
If (handlerType.getMethod (
"handlerAdded", ChannelHandlerContext.class) .isAnnotationPresent (Skip.class)) {
Flags | = MASK_HANDLER_ADDED
}
When constructing a DefaultChannelHandlerContext, there will be a piece of code that can be interpreted as follows: according to the comments of the method in the handler, we can determine whether the method is executed when an event occurs.
ChannelHandlerAdapter is one of the most basic implementations of ChannelHandler. If we look at the code of ChannelHandlerAdapter, we will find that every method header has an @ Skip annotation. That is, by default, each method is not executed. Among the descendants of ChannelHandlerAdapter, if you Override a certain method, @ Skip is gone. Then this method of handler will be implemented.
As for the specific decision process, you can refer to another article, android edittext.setInputType, in which the idea of setting by bit.
At this point, the study of "how to store handler in pipeline" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.