public interface ChannelHandlerContext extends AttributeMap, ChannelInboundInvoker, ChannelOutboundInvoker
ChannelHandler
to interact with its ChannelPipeline
and other handlers. Among other things a handler can notify the next ChannelHandler
in the
ChannelPipeline
as well as modify the ChannelPipeline
it belongs to dynamically.
ChannelPipeline
by calling one of the various methods
provided here.
Please refer to ChannelPipeline
to understand how an event flows.
ChannelPipeline
your handler belongs to by calling
pipeline()
. A non-trivial application could insert, remove, or
replace handlers in the pipeline dynamically at runtime.
ChannelHandlerContext
for later use, such as
triggering an event outside the handler methods, even from a different thread.
public class MyHandler extendsChannelDuplexHandler
{ privateChannelHandlerContext
ctx; public void beforeAdd(ChannelHandlerContext
ctx) { this.ctx = ctx; } public void login(String username, password) { ctx.write(new LoginMessage(username, password)); } ... }
attr(AttributeKey)
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 AttributeKey
s
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 extendsChannelInboundHandlerAdapter
{ private finalAttributeKey
<Integer
> counter =AttributeKey
.valueOf("counter"); // This handler will receive a sequence of increasing integers starting // from 1.@Override
public void channelRead(ChannelHandlerContext
ctx, Object msg) { Integer a = ctx.attr(counter).get(); if (a == null) { a = 1; } attr.set(a * (Integer) msg); } } // 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 (using anAttributeKey
), 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
, and
ChannelPipeline
to find out more about inbound and outbound operations,
what fundamental differences they have, how they flow in a pipeline, and how to handle
the operation in your application.
bind, bind, close, close, connect, connect, connect, connect, deregister, deregister, disconnect, disconnect, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, voidPromise, write, write, writeAndFlush, writeAndFlush
Channel channel()
Channel
which is bound to the ChannelHandlerContext
.EventExecutor executor()
EventExecutor
which is used to execute an arbitrary task.String name()
ChannelHandlerContext
.The name was used when then ChannelHandler
was added to the ChannelPipeline
. This name can also be used to access the registered
ChannelHandler
from the ChannelPipeline
.ChannelHandler handler()
ChannelHandler
that is bound this ChannelHandlerContext
.boolean isRemoved()
true
if the ChannelHandler
which belongs to this context was removed
from the ChannelPipeline
. Note that this method is only meant to be called from with in the
EventLoop
.ChannelHandlerContext fireChannelRegistered()
ChannelInboundInvoker
Channel
was registered to its EventLoop
.
This will result in having the ChannelInboundHandler.channelRegistered(ChannelHandlerContext)
method
called of the next ChannelInboundHandler
contained in the ChannelPipeline
of the
Channel
.fireChannelRegistered
in interface ChannelInboundInvoker
ChannelHandlerContext fireChannelUnregistered()
ChannelInboundInvoker
Channel
was unregistered from its EventLoop
.
This will result in having the ChannelInboundHandler.channelUnregistered(ChannelHandlerContext)
method
called of the next ChannelInboundHandler
contained in the ChannelPipeline
of the
Channel
.fireChannelUnregistered
in interface ChannelInboundInvoker
ChannelHandlerContext fireChannelActive()
ChannelInboundInvoker
Channel
is active now, which means it is connected.
This will result in having the ChannelInboundHandler.channelActive(ChannelHandlerContext)
method
called of the next ChannelInboundHandler
contained in the ChannelPipeline
of the
Channel
.fireChannelActive
in interface ChannelInboundInvoker
ChannelHandlerContext fireChannelInactive()
ChannelInboundInvoker
Channel
is inactive now, which means it is closed.
This will result in having the ChannelInboundHandler.channelInactive(ChannelHandlerContext)
method
called of the next ChannelInboundHandler
contained in the ChannelPipeline
of the
Channel
.fireChannelInactive
in interface ChannelInboundInvoker
ChannelHandlerContext fireExceptionCaught(Throwable cause)
ChannelInboundInvoker
Channel
received an Throwable
in one of its inbound operations.
This will result in having the ChannelInboundHandler.exceptionCaught(ChannelHandlerContext, Throwable)
method called of the next ChannelInboundHandler
contained in the ChannelPipeline
of the
Channel
.fireExceptionCaught
in interface ChannelInboundInvoker
ChannelHandlerContext fireUserEventTriggered(Object evt)
ChannelInboundInvoker
Channel
received an user defined event.
This will result in having the ChannelInboundHandler.userEventTriggered(ChannelHandlerContext, Object)
method called of the next ChannelInboundHandler
contained in the ChannelPipeline
of the
Channel
.fireUserEventTriggered
in interface ChannelInboundInvoker
ChannelHandlerContext fireChannelRead(Object msg)
ChannelInboundInvoker
Channel
received a message.
This will result in having the ChannelInboundHandler.channelRead(ChannelHandlerContext, Object)
method called of the next ChannelInboundHandler
contained in the ChannelPipeline
of the
Channel
.fireChannelRead
in interface ChannelInboundInvoker
ChannelHandlerContext fireChannelReadComplete()
ChannelInboundInvoker
ChannelInboundHandler.channelReadComplete(ChannelHandlerContext)
event to the next ChannelInboundHandler
in the ChannelPipeline
.fireChannelReadComplete
in interface ChannelInboundInvoker
ChannelHandlerContext fireChannelWritabilityChanged()
ChannelInboundInvoker
ChannelInboundHandler.channelWritabilityChanged(ChannelHandlerContext)
event to the next ChannelInboundHandler
in the ChannelPipeline
.fireChannelWritabilityChanged
in interface ChannelInboundInvoker
ChannelHandlerContext read()
ChannelOutboundInvoker
Channel
into the first inbound buffer, triggers an
ChannelInboundHandler.channelRead(ChannelHandlerContext, Object)
event if data was
read, and triggers a
channelReadComplete
event so the
handler can decide to continue reading. If there's a pending read operation already, this method does nothing.
This will result in having the
ChannelOutboundHandler.read(ChannelHandlerContext)
method called of the next ChannelOutboundHandler
contained in the ChannelPipeline
of the
Channel
.
read
in interface ChannelOutboundInvoker
ChannelHandlerContext flush()
ChannelOutboundInvoker
flush
in interface ChannelOutboundInvoker
ChannelPipeline pipeline()
ChannelPipeline
ByteBufAllocator alloc()
ByteBufAllocator
which will be used to allocate ByteBuf
s.@Deprecated <T> Attribute<T> attr(AttributeKey<T> key)
AttributeMap.attr(AttributeKey)
AttributeMap
Attribute
for the given AttributeKey
. This method will never return null, but may return
an Attribute
which does not have a value set yet.attr
in interface AttributeMap
@Deprecated <T> boolean hasAttr(AttributeKey<T> key)
AttributeMap.hasAttr(AttributeKey)
AttributeMap
Attribute
exists in this AttributeMap
.hasAttr
in interface AttributeMap
Copyright © 2008–2017 The Netty Project. All rights reserved.