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

Asynchronous Notification Mechanism of Netty

2024-05-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "asynchronous notification mechanism of Netty". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Preface

The previous article analyzed the instantiation, initialization and registration mechanism of Channel. This article analyzes the notification of asynchronous results, that is, callback, and combs the relationship among Future, Promise, ChannelFuture and ChannelPromise.

First, asynchronous notification code walk-through

After Channel registers with Selector, ChannelFuture is returned. If the registration is not completed, the registration result will be notified asynchronously by adding Listener. Next, let's see how the callback is made.

Code block

Note: the above code block returns ChannelFuture after registering Channel, registers ChannelFutureListener in ChannelFuture, and obtains the registration result by asynchronous notification.

Code block

Note: the build DefaultChannelPromise binds EventLoop and Channel, and the ChannelFutureListener registered above is actually registered with DefaultChannelPromise.

Code block

Note: successfully registered through the ChannelPromise tag Channel.

Code block

Note: set the Channel registration result through cas in DefaultPromise, and call back the Listener added to it.

Note: all Listener that will be registered will be notified by calling back the operationComplete method of GenericFutureListener.

2. Flow chart of asynchronous notification

Take channel registration as an example to outline the flow chart of asynchronous callback. Future/Promise acts as the result carrier and the executive body of the executive Listener.

3. Future/Promise relationship map

The class diagram structure of Future/Promise is shown in the following class diagram. Future in Netty inherits Future in Java and `adds an asynchronous notification mechanism based on Listener.

Promise allows you to call back Listener after marking the result of an operation (for example, calling Promise#trySuccess after successful registration marks the successful result in Promise and callback Listener).

ChannelFuture is bound to a specific Channel. ChannelPromise inherits ChannelFuture and Promise, that is, it has the ability to bind a specific Channel and mark the result of the callback Listener.

This is the end of the content of "Asynchronous Notification Mechanism of Netty". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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