Netty's inbound and outbound, and Inboundhandler's channelinactive and Outboundhandler's close

Source: Internet
Author: User

Let's look at an example first.

There is a simple Server

 Public classSimpleserver { Public Static voidMain (string[] args)throwsException {eventloopgroup bossgroup=NewNioeventloopgroup (1); Eventloopgroup Workergroup=NewNioeventloopgroup (); Serverbootstrap b=NewServerbootstrap (); B.group (Bossgroup, Workergroup). Channel (Nioserversocketchannel.class). Option (CHANNELOPTION.SO_REUSEADDR,true). Childoption (Channeloption.so_sndbuf,1024 * 1024). Childhandler (NewChannelinitializer<niosocketchannel>() {@Overrideprotected voidInitchannel (Niosocketchannel ch)throwsException {ch.pipeline (). AddLast (NewSimpleDuplex1 ()); Ch.pipeline (). AddLast (NewSimpleDuplex2 ()); Ch.pipeline (). AddLast (NewSimpleserverhandler ());        }                }); B.bind (8090). Sync (). Channel (). Closefuture (). sync (); }}

Handler details are as follows

 Public classSimpleDuplex1extendsChannelduplexhandler {@Override Public voidWrite (Channelhandlercontext ctx, Object msg, Channelpromise Promise)throwsException {System.out.println ("----Write 1----"); Super. Write (CTX, MSG, promise); } @Override Public voidChannelread (Channelhandlercontext ctx, Object msg)throwsException {System.out.println ("----Read 1----"); Super. Channelread (CTX, msg); }} Public classSimpleDuplex2extendsChannelduplexhandler {@Override Public voidWrite (Channelhandlercontext ctx, Object msg, Channelpromise Promise)throwsException {System.out.println ("----Write 2----"); Super. Write (CTX, MSG, promise); } @Override Public voidChannelread (Channelhandlercontext ctx, Object msg)throwsException {System.out.println ("----Read 2----"); Super. Channelread (CTX, msg); }}
 Public classSimpleserverhandlerextendsChannelduplexhandler {@Override Public voidChannelread (Channelhandlercontext CTX,FinalObject msg)throwsException {Ctx.channel (). Writeandflush (ByteBufAllocator.DEFAULT.buffer (). Writebytes ("OK". GetBytes ())).    AddListener (Channelfuturelistener.close); } @Override Public voidChannelinactive (Channelhandlercontext ctx)throwsException {System.out.println ("-----INACTIVE-----"); Super. channelinactive (CTX); } @Override Public voidClose (Channelhandlercontext ctx, channelpromise Future)throwsException {System.out.println ("-----CLOSE-----"); Super. Close (CTX, future); }}

After starting the Server, use Telnet to send data to view the results of the execution

----Read 1--------Read 2---- successfully ----Write 2--------write 1---------CLOSE----------INACTIVE-----

1. First look at the execution order, visible, inbound order is consistent with the add sequence, and outbound order is the opposite of the Add order

Also, the IO trigger order for Read is "Socketchannel.read (), order handler, Tailcontext.channelread (). Releasemsg"

The IO trigger sequence for write is "reverse handler, HeadContext.socketChannel.write ()"

That is, read is to trigger the socket's read IO time before entering handler, and if our last handler fails to fully process the message, the Super.channelread is called, then Tailcontext will be entered. At this point Tailcontext will make a debug message telling you that the message entered the last Handler and was not processed. Because in general should be in their own handler to dispose of the message. Instead of getting him into the default handler.

For write, it is the first to enter the custom handler, and finally to enter the Headcontext trigger IO time

2. Say Close and channelinactive again

I said it earlier. The order of Outbound is finally performed to Headcontext to perform the actual IO operation, and Close is the same, when you call Channle.close, you will go through your handler. Finally call HeadContext.socketChannel.close (). So, in our Handler, we'll print "----CLOSE----" Before invoking the actual socketchannel.close. Finally, when close succeeds, the channelinactive time is triggered.

So the relationship between close and Channelinactive is close is the action of actively closing the channel, and Channelinactive is the event that is notified when the closure is successful.

Netty's inbound and outbound, and Inboundhandler's channelinactive and Outboundhandler's close

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.