Netty3 Source Analysis-Channelhandlercontext

Source: Internet
Author: User

Netty3 Source Analysis-Channelhandlercontext
the meaning of the existence of Channelhandlercontext is that it is possible to interact with pipeline or other handlers of the handler it manages, and Channelhandler's understanding is said earlier.
Send event : can be called Sendupstream (channelevent) or Senddownstream (channelevent) passes an event to the pipeline in this handler that is closest to it.
Modify Pipeline: Call Getpipeline () can get this handler belong to the Channelpipeline object, of course, can also be updated (join, remove) the pipeline in this one. Extract Spare (Retrieving for later use):can hold aChannelhandlercontext, in order to be used later. ( need to be thoroughly understood here )
Storage status information : see Channelhandler. A handler can have multiple context: This means that if the context is added to a different pipeline, these handler instances are executed multiple times. The following example in the API, for each received a number, do accumulate, the result exists in attachment. Public  classFactorialHandler extendsSimplechannelhandler {
      //This handler would receive a sequence of increasing integers starting       //from 1.       @Override       Public  void messagereceived (Channelhandlercontext ctx, messageevent evt) { integer a = (integer) ctx.getattachment ();integer b = (integer) evt.getmessage ();
           if (A = = NULL) {a = 1;          } ctx.setattachment (Integer. valueOf (A * b));      }}FactorialHandler fh = NewFactorialHandler ();channelpipeline p1 = Channels.  Pipeline();P1.addlast ("F1" , FH); P1.addlast ("F2" , FH);
ChannelpipelineP2= Channels. Pipeline (); p2. AddLast ( "F3" , FH); p2. AddLast ( "F4" , FH);if both pipeline are started, the multiplication operation will be performed four times correctly.
Come here, look again.Channelhandlercontext Source code is at a glance. Public  InterfaceChannelhandlercontext {
      //Get the channel to which this pipeline belongs, equivalent to Getpipeline (). Getchannel () Channel Getchannel ();
      //handler belongs to pipeline channelpipeline getpipeline ();
      //handler have a corresponding name String getName ();
      //Return to this context-maintained handler Channelhandler gethandler ();
      ///corresponding handler type, see if it is a Channelupstreamhandler,channeldownstreamhandler instance       Boolean Canhandleupstream ();      Boolean Canhandledownstream ();
      //pass events to the nearest handler       void Sendupstream (channelevent e);      void Senddownstream (channelevent e);

Object getattachment ();      void setattachment (Object attachment);}








Netty3 Source Analysis-Channelhandlercontext

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.