public interface ChannelHandlerContext extends AttributeMap
ChannelHandler to interact with its ChannelPipeline
and other handlers. A handler can notify the next ChannelHandler in the ChannelPipeline,
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{ privateChannelHandlerContextctx; public void beforeAdd(ChannelHandlerContextctx) { this.ctx = ctx; } public void login(String username, password) { ctx.write(new LoginMessage(username, password)); } ... }
AttributeMap.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
ChannelHandlerContexts if it is added to one or more
ChannelPipelines more than once.
For example, the following handler will have as many independent AttributeKeys
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<Integer> { private finalAttributeKey<Integer> counter = newAttributeKey<Integer>("counter"); // This handler will receive a sequence of increasing integers starting // from 1.@Overridepublic void channelRead(ChannelHandlerContextctx, Object msg) { Integer a = ctx.attr(counter).get(); if (a == null) { a = 1; } attr.set(a * integer)); } } // 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 (using anAttributeKey), 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, 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.
| Modifier and Type | Method and Description |
|---|---|
ByteBufAllocator |
alloc()
Return the assigned
ByteBufAllocator which will be used to allocate ByteBufs. |
ChannelFuture |
bind(java.net.SocketAddress localAddress)
Request to bind to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
ChannelFuture |
bind(java.net.SocketAddress localAddress,
ChannelPromise promise)
Request to bind to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
Channel |
channel()
Return the
Channel which is bound to the ChannelHandlerContext. |
ChannelFuture |
close()
Request to close the
Channel and notify the ChannelFuture once the operation completes,
either because the operation was successful or because of
an error. |
ChannelFuture |
close(ChannelPromise promise)
Request to close the
Channel and notify the ChannelFuture once the operation completes,
either because the operation was successful or because of
an error. |
ChannelFuture |
connect(java.net.SocketAddress remoteAddress)
Request to connect to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
ChannelFuture |
connect(java.net.SocketAddress remoteAddress,
ChannelPromise promise)
Request to connect to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
ChannelFuture |
connect(java.net.SocketAddress remoteAddress,
java.net.SocketAddress localAddress)
Request to connect to the given
SocketAddress while bind to the localAddress and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
connect(java.net.SocketAddress remoteAddress,
java.net.SocketAddress localAddress,
ChannelPromise promise)
Request to connect to the given
SocketAddress while bind to the localAddress and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
deregister()
Request to deregister from the previous assigned
EventExecutor and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
deregister(ChannelPromise promise)
Request to deregister from the previous assigned
EventExecutor and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
disconnect()
Request to disconnect from the remote peer and notify the
ChannelFuture once the operation completes,
either because the operation was successful or because of an error. |
ChannelFuture |
disconnect(ChannelPromise promise)
Request to disconnect from the remote peer and notify the
ChannelFuture once the operation completes,
either because the operation was successful or because of an error. |
EventExecutor |
executor()
The
EventExecutor that is used to dispatch the events. |
ChannelHandlerContext |
fireChannelActive()
A
Channel is active now, which means it is connected. |
ChannelHandlerContext |
fireChannelInactive()
A
Channel is inactive now, which means it is closed. |
ChannelHandlerContext |
fireChannelRead(java.lang.Object msg)
A
Channel received a message. |
ChannelHandlerContext |
fireChannelReadComplete()
Triggers an
ChannelInboundHandler.channelReadComplete(ChannelHandlerContext)
event to the next ChannelInboundHandler in the ChannelPipeline. |
ChannelHandlerContext |
fireChannelRegistered()
|
ChannelHandlerContext |
fireChannelUnregistered()
|
ChannelHandlerContext |
fireChannelWritabilityChanged()
Triggers an
ChannelInboundHandler.channelWritabilityChanged(ChannelHandlerContext)
event to the next ChannelInboundHandler in the ChannelPipeline. |
ChannelHandlerContext |
fireExceptionCaught(java.lang.Throwable cause)
A
Channel received an Throwable in one of its inbound operations. |
ChannelHandlerContext |
fireUserEventTriggered(java.lang.Object evt)
A
Channel received an user defined event. |
ChannelHandlerContext |
flush()
Request to flush all pending messages via this ChannelOutboundInvoker.
|
ChannelHandler |
handler()
The
ChannelHandler that is bound this ChannelHandlerContext. |
boolean |
isRemoved()
Return
true if the ChannelHandler which belongs to this context was removed
from the ChannelPipeline. |
java.lang.String |
name()
The unique name of the
ChannelHandlerContext.The name was used when then ChannelHandler
was added to the ChannelPipeline. |
ChannelFuture |
newFailedFuture(java.lang.Throwable cause)
Create a new
ChannelFuture which is marked as failed already. |
ChannelProgressivePromise |
newProgressivePromise()
Return an new
ChannelProgressivePromise |
ChannelPromise |
newPromise()
Return a new
ChannelPromise. |
ChannelFuture |
newSucceededFuture()
Create a new
ChannelFuture which is marked as succeeded already. |
ChannelPipeline |
pipeline()
Return the assigned
ChannelPipeline |
ChannelHandlerContext |
read()
Request to Read data from the
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. |
ChannelPromise |
voidPromise()
Return a special ChannelPromise which can be reused for different operations.
|
ChannelFuture |
write(java.lang.Object msg)
Request to write a message via this
ChannelHandlerContext through the ChannelPipeline. |
ChannelFuture |
write(java.lang.Object msg,
ChannelPromise promise)
Request to write a message via this
ChannelHandlerContext through the ChannelPipeline. |
ChannelFuture |
writeAndFlush(java.lang.Object msg)
Shortcut for call
write(Object) and flush(). |
ChannelFuture |
writeAndFlush(java.lang.Object msg,
ChannelPromise promise)
Shortcut for call
write(Object, ChannelPromise) and flush(). |
attrChannel channel()
Channel which is bound to the 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 information please refer to the
EventExecutor javadoc.java.lang.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()
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.ChannelHandlerContext fireChannelUnregistered()
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.ChannelHandlerContext fireChannelActive()
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.ChannelHandlerContext fireChannelInactive()
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.ChannelHandlerContext fireExceptionCaught(java.lang.Throwable cause)
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.ChannelHandlerContext fireUserEventTriggered(java.lang.Object evt)
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.ChannelHandlerContext fireChannelRead(java.lang.Object msg)
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.ChannelHandlerContext fireChannelReadComplete()
ChannelInboundHandler.channelReadComplete(ChannelHandlerContext)
event to the next ChannelInboundHandler in the ChannelPipeline.ChannelHandlerContext fireChannelWritabilityChanged()
ChannelInboundHandler.channelWritabilityChanged(ChannelHandlerContext)
event to the next ChannelInboundHandler in the ChannelPipeline.ChannelFuture bind(java.net.SocketAddress localAddress)
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error.
This will result in having the
ChannelOutboundHandler.bind(ChannelHandlerContext, SocketAddress, ChannelPromise) method
called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelFuture connect(java.net.SocketAddress remoteAddress)
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error.
If the connection fails because of a connection timeout, the ChannelFuture will get failed with
a ConnectTimeoutException. If it fails because of connection refused a ConnectException
will be used.
This will result in having the
ChannelOutboundHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelFuture connect(java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress)
SocketAddress while bind to the localAddress and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error.
This will result in having the
ChannelOutboundHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelFuture disconnect()
ChannelFuture once the operation completes,
either because the operation was successful or because of an error.
This will result in having the
ChannelOutboundHandler.disconnect(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelFuture close()
Channel and notify the ChannelFuture once the operation completes,
either because the operation was successful or because of
an error.
After it is closed it is not possible to reuse it again.
This will result in having the
ChannelOutboundHandler.close(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelFuture deregister()
EventExecutor and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error.
This will result in having the
ChannelOutboundHandler.deregister(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelFuture bind(java.net.SocketAddress localAddress, ChannelPromise promise)
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error.
The given ChannelPromise will be notified.
This will result in having the
ChannelOutboundHandler.bind(ChannelHandlerContext, SocketAddress, ChannelPromise) method
called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelFuture connect(java.net.SocketAddress remoteAddress, ChannelPromise promise)
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error.
The given ChannelFuture will be notified.
If the connection fails because of a connection timeout, the ChannelFuture will get failed with
a ConnectTimeoutException. If it fails because of connection refused a ConnectException
will be used.
This will result in having the
ChannelOutboundHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelFuture connect(java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress, ChannelPromise promise)
SocketAddress while bind to the localAddress and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error.
The given ChannelPromise will be notified and also returned.
This will result in having the
ChannelOutboundHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelFuture disconnect(ChannelPromise promise)
ChannelFuture once the operation completes,
either because the operation was successful or because of an error.
The given ChannelPromise will be notified.
This will result in having the
ChannelOutboundHandler.disconnect(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelFuture close(ChannelPromise promise)
Channel and notify the ChannelFuture once the operation completes,
either because the operation was successful or because of
an error.
After it is closed it is not possible to reuse it again.
The given ChannelPromise will be notified.
This will result in having the
ChannelOutboundHandler.close(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelFuture deregister(ChannelPromise promise)
EventExecutor and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error.
The given ChannelPromise will be notified.
This will result in having the
ChannelOutboundHandler.deregister(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOutboundHandler contained in the ChannelPipeline of the
Channel.
ChannelHandlerContext read()
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.
ChannelFuture write(java.lang.Object msg)
ChannelHandlerContext through the ChannelPipeline.
This method will not request to actual flush, so be sure to call flush()
once you want to request to flush all pending data to the actual transport.ChannelFuture write(java.lang.Object msg, ChannelPromise promise)
ChannelHandlerContext through the ChannelPipeline.
This method will not request to actual flush, so be sure to call flush()
once you want to request to flush all pending data to the actual transport.ChannelHandlerContext flush()
ChannelFuture writeAndFlush(java.lang.Object msg, ChannelPromise promise)
write(Object, ChannelPromise) and flush().ChannelFuture writeAndFlush(java.lang.Object msg)
write(Object) and flush().ChannelPipeline pipeline()
ChannelPipelineByteBufAllocator alloc()
ByteBufAllocator which will be used to allocate ByteBufs.ChannelPromise newPromise()
ChannelPromise.ChannelProgressivePromise newProgressivePromise()
ChannelProgressivePromiseChannelFuture newSucceededFuture()
ChannelFuture which is marked as succeeded already. So Future.isSuccess()
will return true. All FutureListener added to it will be notified directly. Also
every call of blocking methods will just return without blocking.ChannelFuture newFailedFuture(java.lang.Throwable cause)
ChannelFuture which is marked as failed already. So Future.isSuccess()
will return false. All FutureListener added to it will be notified directly. Also
every call of blocking methods will just return without blocking.ChannelPromise voidPromise()
It's only supported to use
it for write(Object, ChannelPromise).
Be aware that the returned ChannelPromise will not support most operations and should only be used
if you want to save an object allocation for every write operation. You will not be able to detect if the
operation was complete, only if it failed as the implementation will call
ChannelPipeline.fireExceptionCaught(Throwable) in this case.
Copyright © 2008–2018 The Netty Project. All rights reserved.