Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Netty distributed Monitoring read event

Shulou Source: shulou.com Published: 2022-06-01 03:12:44 10月05日 Update

Editor to share with you the example analysis of Netty distributed monitoring read events, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

Let's go back to AbstractUnsafe's register0 () method:

Private void register0 (ChannelPromise promise) {try {/ / omit the code / / do the actual registration doRegister (); neverRegistered = false; registered = true; / / trigger event pipeline.invokeHandlerAddedIfNeeded (); safeSetSuccess (promise); / / trigger registration success event pipeline.fireChannelRegistered () If (isActive ()) {if (firstRegistration) {/ / propagate active events (4) pipeline.fireChannelActive ();} else if (config (). IsAutoRead ()) {beginRead ();} catch (Throwable t) {/ / omit code}}

After doRegister () completes the actual registration, it will go to the judgment of if (isActive ()). Because the link has been completed at this time, this is true, and the default judgment condition if (firstRegistration) is also true, so here it will go to pipeline.fireChannelActive ().

We will analyze pipeline in detail in the next chapter. All we need to know here is that it will eventually flow to the beginRead () method of AbstractUnsafe.

Follow to the beginRead () method:

Public final void beginRead () {assertEventLoop (); if (! isActive ()) {return;} try {doBeginRead ();} catch (final Exception e) {/ / Code ellipsis}}

We are also familiar with this piece of code, because we have also analyzed this step by analyzing NioServerSocketChannel.

Let's continue to follow doBeginRead ():

Protected void doBeginRead () throws Exception {/ / get selectionKey final SelectionKey selectionKey = this.selectionKey; if (! selectionKey.isValid ()) {return;} readPending = true; / / get the event final int interestOps = selectionKey.interestOps () / / determine whether if ((interestOps & readInterestOp) = = 0) is not monitored for any event. {/ / this condition holds / / registers the previous accept event. ReadInterest represents the meaning of a new connection selectionKey.interestOps (interestOps | readInterestOp);}}

I believe you will be familiar with this code, because we have come here after the registration of channel on our server.

Because we initialize the read event when we create the NioSocketChannel, and selectionKey is the key returned by channel when registering, the selectionKey.interestOps (interestOps | readInterestOp) step registers the read event of the current channel into the selector

After registration, NioEventLoop can poll the current channel for read events

The above is all the contents of the article "sample Analysis of Netty distributed Monitoring read events". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Events analysis code monitoring methods times articles distributed examples content reality conditions strangers success not so much representatives interests most meaning more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi vpn Huawei Shulou Information Shulou Technology