org.jboss.netty.channel
接口 ChannelHandlerContext


public interface ChannelHandlerContext

允许一个ChannelHandler与它所属的ChannelPipeline 及其他处理器交互.一个处理器可以发送一个上游事件或下游事件,动态修改所属的ChannelPipeline.

发送一个事件

你可以调用sendUpstream(ChannelEvent)发送或转发一个ChannelEvent到同一个 ChannelPipeline里最接近的处理器.请参阅ChannelPipeline了解一个事件如何流动.

修改管道

你可以通过调用getPipeline()获取处理器所属的ChannelPipeline .特殊的应用程序可以在运行时动态插入,移除,替换管道里的处理器.

获取以备后用

你可以保留ChannelHandlerContext以备后用,如在处理方法之外甚至来之不同的线程触发事件.
 public class MyHandler extends SimpleChannelHandler
                        implements LifeCycleAwareChannelHandler {
 
     private ChannelHandlerContext ctx;
 
     public void beforeAdd(ChannelHandlerContext ctx) {
         this.ctx = ctx;
     }
 
     public void login(String username, password) {
         Channels.write(
                 this.ctx,
                 Channels.succeededFuture(this.ctx.getChannel()),
                 new LoginMessage(username, password));
     }
     ...
 }
 

存储状态信息

setAttachment(Object) and getAttachment() allow you to store and access stateful information that is related with a handler and its context. Please refer to ChannelHandler to learn various recommended ways to manage stateful information.

A handler can have more than one context

Please note that a ChannelHandler instance can be added to more than one ChannelPipeline. It means a single ChannelHandler instance can have more than one ChannelHandlerContext and therefore the single instance can be invoked with different ChannelHandlerContexts if it is added to one or more ChannelPipelines 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.
   @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));
   }
 }
 
 // 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();
 
 ChannelPipeline p1 = Channels.pipeline();
 p1.addLast("f1", fh);
 p1.addLast("f2", fh);
 
 ChannelPipeline p2 = Channels.pipeline();
 p2.addLast("f3", fh);
 p2.addLast("f4", fh);
 

Additional resources worth reading

Please refer to the ChannelHandler, ChannelEvent, and ChannelPipeline to 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.


方法摘要
 boolean canHandleDownstream()
          只有当该ChannelHandlerChannelDownstreamHandler的一个实例才返回true.
 boolean canHandleUpstream()
          只有当该ChannelHandlerChannelUpstreamHandler的一个实例才返回true.
 java.lang.Object getAttachment()
          获取link #setAttachment(Object) 附加}到该上下文的对象.
 Channel getChannel()
          返回ChannelPipeline所属的Channel.该方法是 getPipeline().getChannel()快捷方式.
 ChannelHandler getHandler()
          获取该上下文对象正在服务的ChannelHandler.
 java.lang.String getName()
          返回在ChannelPipelineChannelHandler的名称.
 ChannelPipeline getPipeline()
          返回ChannelHandler所属的ChannelPipeline.
 void sendDownstream(ChannelEvent e)
          发送一个指定的ChannelEvent到位于该上下文关联的最接近的下游处理器 ChannelDownstreamHandler.建议使用 Channels里的快捷方法胜过直接调用该方法.
 void sendUpstream(ChannelEvent e)
          发送一个指定的ChannelEvent到位于该上下文关联的最接近的上游处理器 ChannelUpstreamHandler.建议使用 Channels里的快捷方法胜过直接调用该方法.
 void setAttachment(java.lang.Object attachment)
          附加一个对象到该上下文用于保存关联该上下文指定的ChannelHandler的状态信息.
 

方法详细信息

getChannel

Channel getChannel()
返回ChannelPipeline所属的Channel.该方法是 getPipeline().getChannel()快捷方式.


getPipeline

ChannelPipeline getPipeline()
返回ChannelHandler所属的ChannelPipeline.


getName

java.lang.String getName()
返回在ChannelPipelineChannelHandler的名称.


getHandler

ChannelHandler getHandler()
获取该上下文对象正在服务的ChannelHandler.


canHandleUpstream

boolean canHandleUpstream()
只有当该ChannelHandlerChannelUpstreamHandler的一个实例才返回true.


canHandleDownstream

boolean canHandleDownstream()
只有当该ChannelHandlerChannelDownstreamHandler的一个实例才返回true.


sendUpstream

void sendUpstream(ChannelEvent e)
发送一个指定的ChannelEvent到位于该上下文关联的最接近的上游处理器 ChannelUpstreamHandler.建议使用 Channels里的快捷方法胜过直接调用该方法.


sendDownstream

void sendDownstream(ChannelEvent e)
发送一个指定的ChannelEvent到位于该上下文关联的最接近的下游处理器 ChannelDownstreamHandler.建议使用 Channels里的快捷方法胜过直接调用该方法.


getAttachment

java.lang.Object getAttachment()
获取link #setAttachment(Object) 附加}到该上下文的对象.

返回:
如果没有对象被附加或附加的为null,则返回null

setAttachment

void setAttachment(java.lang.Object attachment)
附加一个对象到该上下文用于保存关联该上下文指定的ChannelHandler的状态信息.