Package org.jboss.netty.channel
Interface ChannelHandlerContext
-
- All Known Implementing Classes:
DefaultChannelPipeline.DefaultChannelHandlerContext
public interface ChannelHandlerContextEnables aChannelHandlerto interact with itsChannelPipelineand other handlers. A handler can send aChannelEventupstream or downstream, modify theChannelPipelineit belongs to dynamically.Sending an event
You can send or forward aChannelEventto the closest handler in the sameChannelPipelineby callingsendUpstream(ChannelEvent)orsendDownstream(ChannelEvent). Please refer toChannelPipelineto understand how an event flows.Modifying a pipeline
You can get theChannelPipelineyour handler belongs to by callinggetPipeline(). A non-trivial application could insert, remove, or replace handlers in the pipeline dynamically in runtime.Retrieving for later use
You can keep theChannelHandlerContextfor later use, such as triggering an event outside the handler methods, even from a different thread.public class MyHandler extends
SimpleChannelHandlerimplementsLifeCycleAwareChannelHandler{ privateChannelHandlerContextctx; public void beforeAdd(ChannelHandlerContextctx) { this.ctx = ctx; } public void login(String username, password) {Channels.write( this.ctx,Channels.succeededFuture(this.ctx.getChannel()), new LoginMessage(username, password)); } ... }Storing stateful information
setAttachment(Object)andgetAttachment()allow you to store and access stateful information that is related with a handler and its context. Please refer toChannelHandlerto learn various recommended ways to manage stateful information.A handler can have more than one context
Please note that aChannelHandlerinstance can be added to more than oneChannelPipeline. It means a singleChannelHandlerinstance can have more than oneChannelHandlerContextand therefore the single instance can be invoked with differentChannelHandlerContexts if it is added to one or moreChannelPipelines more than once.For example, the following handler will have as many independent attachments as how many times it is added to pipelines, regardless if it is added to the same pipeline multiple times or added to different pipelines multiple times:
public class FactorialHandler extends
SimpleChannelHandler{ // This handler will receive a sequence of increasing integers starting // from 1.@Overridepublic void messageReceived(ChannelHandlerContextctx,MessageEventevt) { Integer a = (Integer) ctx.getAttachment(); Integer b = (Integer) evt.getMessage(); if (a == null) { a = 1; } ctx.setAttachment(Integer.valueOf(a * b)); } } // Different context objects are given to "f1", "f2", "f3", and "f4" even if // they refer to the same handler instance. Because the FactorialHandler // stores its state in a context object (as an attachment), the factorial is // calculated correctly 4 times once the two pipelines (p1 and p2) are active. FactorialHandler fh = new FactorialHandler();ChannelPipelinep1 =Channels.pipeline(); p1.addLast("f1", fh); p1.addLast("f2", fh);ChannelPipelinep2 =Channels.pipeline(); p2.addLast("f3", fh); p2.addLast("f4", fh);Additional resources worth reading
Please refer to the
ChannelHandler,ChannelEvent, andChannelPipelineto find out what a upstream event and a downstream event are, what fundamental differences they have, how they flow in a pipeline, and how to handle the event in your application.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description booleancanHandleDownstream()booleancanHandleUpstream()java.lang.ObjectgetAttachment()Retrieves an object which isattachedto this context.ChannelgetChannel()Returns theChannelthat theChannelPipelinebelongs to.ChannelHandlergetHandler()Returns theChannelHandlerthat this context object is serving.java.lang.StringgetName()Returns the name of theChannelHandlerin theChannelPipeline.ChannelPipelinegetPipeline()Returns theChannelPipelinethat theChannelHandlerbelongs to.voidsendDownstream(ChannelEvent e)Sends the specifiedChannelEventto theChannelDownstreamHandlerwhich is placed in the closest downstream from the handler associated with this context.voidsendUpstream(ChannelEvent e)Sends the specifiedChannelEventto theChannelUpstreamHandlerwhich is placed in the closest upstream from the handler associated with this context.voidsetAttachment(java.lang.Object attachment)Attaches an object to this context to store a stateful information specific to theChannelHandlerwhich is associated with this context.
-
-
-
Method Detail
-
getChannel
Channel getChannel()
Returns theChannelthat theChannelPipelinebelongs to. This method is a shortcut to getPipeline().getChannel().
-
getPipeline
ChannelPipeline getPipeline()
Returns theChannelPipelinethat theChannelHandlerbelongs to.
-
getName
java.lang.String getName()
Returns the name of theChannelHandlerin theChannelPipeline.
-
getHandler
ChannelHandler getHandler()
Returns theChannelHandlerthat this context object is serving.
-
canHandleUpstream
boolean canHandleUpstream()
-
canHandleDownstream
boolean canHandleDownstream()
-
sendUpstream
void sendUpstream(ChannelEvent e)
Sends the specifiedChannelEventto theChannelUpstreamHandlerwhich is placed in the closest upstream from the handler associated with this context. It is recommended to use the shortcut methods inChannelsrather than calling this method directly.
-
sendDownstream
void sendDownstream(ChannelEvent e)
Sends the specifiedChannelEventto theChannelDownstreamHandlerwhich is placed in the closest downstream from the handler associated with this context. It is recommended to use the shortcut methods inChannelsrather than calling this method directly.
-
getAttachment
java.lang.Object getAttachment()
Retrieves an object which isattachedto this context.- Returns:
nullif no object was attached ornullwas attached
-
setAttachment
void setAttachment(java.lang.Object attachment)
Attaches an object to this context to store a stateful information specific to theChannelHandlerwhich is associated with this context.
-
-