|
|||||||||
上一个类 下一个类 | 框架 无框架 | ||||||||
摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 |
public interface ChannelHandlerContext
允许一个ChannelHandler
与它所属的ChannelPipeline
及其他处理器交互.一个处理器可以发送一个上游事件或下游事件,动态修改所属的ChannelPipeline
.
sendUpstream(ChannelEvent)
发送或转发一个ChannelEvent
到同一个
ChannelPipeline
里最接近的处理器.请参阅ChannelPipeline
了解一个事件如何流动.
getPipeline()
获取处理器所属的ChannelPipeline
.特殊的应用程序可以在运行时动态插入,移除,替换管道里的处理器.
ChannelHandlerContext
以备后用,如在处理方法之外甚至来之不同的线程触发事件.
public class MyHandler extendsSimpleChannelHandler
implementsLifeCycleAwareChannelHandler
{ privateChannelHandlerContext
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.
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
ChannelHandlerContext
s if it is added to one or more
ChannelPipeline
s 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 extendsSimpleChannelHandler
{ // 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);
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()
只有当该 ChannelHandler 是ChannelDownstreamHandler 的一个实例才返回true. |
boolean |
canHandleUpstream()
只有当该 ChannelHandler 是ChannelUpstreamHandler 的一个实例才返回true. |
java.lang.Object |
getAttachment()
获取link #setAttachment(Object) 附加}到该上下文的对象. |
Channel |
getChannel()
返回 ChannelPipeline 所属的Channel .该方法是
getPipeline().getChannel()快捷方式. |
ChannelHandler |
getHandler()
获取该上下文对象正在服务的 ChannelHandler . |
java.lang.String |
getName()
返回在 ChannelPipeline 里ChannelHandler 的名称. |
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 的状态信息. |
方法详细信息 |
---|
Channel getChannel()
ChannelPipeline
所属的Channel
.该方法是
getPipeline().getChannel()快捷方式.
ChannelPipeline getPipeline()
ChannelHandler
所属的ChannelPipeline
.
java.lang.String getName()
ChannelPipeline
里ChannelHandler
的名称.
ChannelHandler getHandler()
ChannelHandler
.
boolean canHandleUpstream()
ChannelHandler
是ChannelUpstreamHandler
的一个实例才返回true.
boolean canHandleDownstream()
ChannelHandler
是ChannelDownstreamHandler
的一个实例才返回true.
void sendUpstream(ChannelEvent e)
ChannelEvent
到位于该上下文关联的最接近的上游处理器
ChannelUpstreamHandler
.建议使用 Channels
里的快捷方法胜过直接调用该方法.
void sendDownstream(ChannelEvent e)
ChannelEvent
到位于该上下文关联的最接近的下游处理器
ChannelDownstreamHandler
.建议使用 Channels
里的快捷方法胜过直接调用该方法.
java.lang.Object getAttachment()
null
,则返回null
void setAttachment(java.lang.Object attachment)
ChannelHandler
的状态信息.
|
|||||||||
上一个类 下一个类 | 框架 无框架 | ||||||||
摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 |