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