public interface ChannelHandlerContext extends io.netty.util.AttributeMap, ChannelFutureFactory, ChannelInboundInvoker, ChannelOutboundInvoker
ChannelHandler to interact with its ChannelPipeline
and other handlers. A handler can send a ChannelEvent upstream or
downstream, modify the ChannelPipeline it belongs to dynamically.
ChannelEvent to the closest handler in the
same ChannelPipeline by calling #sendUpstream(ChannelEvent)
or #sendDownstream(ChannelEvent). Please refer to
ChannelPipeline to understand how an event flows.
ChannelPipeline your handler belongs to by calling
#getPipeline(). A non-trivial application could insert, remove, or
replace handlers in the pipeline dynamically in runtime.
ChannelHandlerContext for later use, such as
triggering an event outside the handler methods, even from a different thread.
public class MyHandler extendsSimpleChannelHandlerimplementsLifeCycleAwareChannelHandler{ 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)); } ... }
#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
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 extendsSimpleChannelHandler{ // 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);
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.
| Modifier and Type | Method and Description |
|---|---|
Channel |
channel()
Return the
Channel which is bound to the ChannelHandlerContext. |
EventExecutor |
executor()
The
EventExecutor that is used to dispatch the events. |
ChannelHandler |
handler()
The
ChannelHandler that is bound this ChannelHandlerContext. |
boolean |
hasInboundByteBuffer()
|
boolean |
hasInboundMessageBuffer()
|
boolean |
hasNextInboundByteBuffer()
|
boolean |
hasNextInboundMessageBuffer()
|
boolean |
hasNextOutboundByteBuffer()
|
boolean |
hasNextOutboundMessageBuffer()
|
boolean |
hasOutboundByteBuffer()
|
boolean |
hasOutboundMessageBuffer()
|
io.netty.buffer.ByteBuf |
inboundByteBuffer()
|
<T> io.netty.buffer.MessageBuf<T> |
inboundMessageBuffer()
|
boolean |
isReadable()
Return
true if the ChannelHandlerContext was marked as readable. |
String |
name()
The unique name of the
ChannelHandlerContext.The name was used when then ChannelHandler
was added to the ChannelPipeline. |
io.netty.buffer.ByteBuf |
nextInboundByteBuffer()
Return the
ByteBuf of the next ChannelHandlerContext if hasNextInboundByteBuffer()
returned true, otherwise a UnsupportedOperationException is thrown. |
io.netty.buffer.MessageBuf<Object> |
nextInboundMessageBuffer()
Return the
MessageBuf of the next ChannelHandlerContext if
hasNextInboundMessageBuffer() returned true, otherwise a
UnsupportedOperationException is thrown. |
io.netty.buffer.ByteBuf |
nextOutboundByteBuffer()
Return the
ByteBuf of the next ChannelHandlerContext if hasNextOutboundByteBuffer()
returned true, otherwise a UnsupportedOperationException is thrown. |
io.netty.buffer.MessageBuf<Object> |
nextOutboundMessageBuffer()
Return the
MessageBuf of the next ChannelHandlerContext if
hasNextOutboundMessageBuffer() returned true, otherwise a
UnsupportedOperationException is thrown. |
io.netty.buffer.ByteBuf |
outboundByteBuffer()
|
<T> io.netty.buffer.MessageBuf<T> |
outboundMessageBuffer()
|
ChannelPipeline |
pipeline()
Return the
ChannelPipeline which belongs this ChannelHandlerContext. |
void |
readable(boolean readable)
Mark the
ChannelHandlerContext as readable or suspend it. |
Set<ChannelHandlerType> |
types() |
newFailedFuture, newFuture, newSucceededFuturefireChannelActive, fireChannelInactive, fireChannelRegistered, fireChannelUnregistered, fireExceptionCaught, fireInboundBufferUpdated, fireUserEventTriggeredbind, bind, close, close, connect, connect, connect, connect, deregister, deregister, disconnect, disconnect, flush, flush, write, writeChannel channel()
Channel which is bound to the ChannelHandlerContext.ChannelPipeline pipeline()
ChannelPipeline which belongs this ChannelHandlerContext.EventExecutor executor()
EventExecutor that is used to dispatch the events. This can also be used to directly
submit tasks that get executed in the event loop. For more informations please refer to the
EventExecutor javadocs.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.Set<ChannelHandlerType> types()
boolean hasInboundByteBuffer()
boolean hasInboundMessageBuffer()
io.netty.buffer.ByteBuf inboundByteBuffer()
ByteBuf for inbound data if hasInboundByteBuffer() returned
true. If hasInboundByteBuffer() returned false it will throw a
UnsupportedOperationException<T> io.netty.buffer.MessageBuf<T> inboundMessageBuffer()
MessageBuf for inbound data if hasInboundMessageBuffer() returned
true. If hasInboundMessageBuffer() returned false it will throw a
UnsupportedOperationException.boolean hasOutboundByteBuffer()
boolean hasOutboundMessageBuffer()
io.netty.buffer.ByteBuf outboundByteBuffer()
ByteBuf for outbound data if hasOutboundByteBuffer() returned
true. If hasOutboundByteBuffer() returned false it will throw
a UnsupportedOperationException.<T> io.netty.buffer.MessageBuf<T> outboundMessageBuffer()
MessageBuf for outbound data if hasOutboundMessageBuffer() returned
true. If hasOutboundMessageBuffer() returned false it will throw a
UnsupportedOperationExceptionboolean hasNextInboundByteBuffer()
boolean hasNextInboundMessageBuffer()
io.netty.buffer.ByteBuf nextInboundByteBuffer()
ByteBuf of the next ChannelHandlerContext if hasNextInboundByteBuffer()
returned true, otherwise a UnsupportedOperationException is thrown.io.netty.buffer.MessageBuf<Object> nextInboundMessageBuffer()
MessageBuf of the next ChannelHandlerContext if
hasNextInboundMessageBuffer() returned true, otherwise a
UnsupportedOperationException is thrown.boolean hasNextOutboundByteBuffer()
boolean hasNextOutboundMessageBuffer()
io.netty.buffer.ByteBuf nextOutboundByteBuffer()
ByteBuf of the next ChannelHandlerContext if hasNextOutboundByteBuffer()
returned true, otherwise a UnsupportedOperationException is thrown.io.netty.buffer.MessageBuf<Object> nextOutboundMessageBuffer()
MessageBuf of the next ChannelHandlerContext if
hasNextOutboundMessageBuffer() returned true, otherwise a
UnsupportedOperationException is thrown.boolean isReadable()
true if the ChannelHandlerContext was marked as readable. This basically means
that once its not readable anymore no new data will be read from the transport and passed down the
ChannelPipeline.
Only if all ChannelHandlerContext's isReadable() return true, the data is
passed again down the ChannelPipeline.void readable(boolean readable)
ChannelHandlerContext as readable or suspend it. See isReadable()Copyright © 2008-2012 The Netty Project. All Rights Reserved.