Package io.netty.channel
Interface ChannelHandlerContext
-
- All Superinterfaces:
io.netty.util.AttributeMap,ChannelInboundInvoker,ChannelOutboundInvoker
public interface ChannelHandlerContext extends io.netty.util.AttributeMap, ChannelInboundInvoker, ChannelOutboundInvoker
Enables aChannelHandlerto interact with itsChannelPipelineand other handlers. Among other things a handler can notify the nextChannelHandlerin theChannelPipelineas well as modify theChannelPipelineit belongs to dynamically.Notify
You can notify the closest handler in the sameChannelPipelineby calling one of the various methods provided here. Please refer toChannelPipelineto understand how an event flows.Modifying a pipeline
You can get theChannelPipelineyour handler belongs to by callingpipeline(). A non-trivial application could insert, remove, or replace handlers in the pipeline dynamically at runtime.Retrieving for later use
You can keep theChannelHandlerContextfor later use, such as triggering an event outside the handler methods, even from a different thread.public class MyHandler extends
ChannelDuplexHandler{ privateChannelHandlerContextctx; public void beforeAdd(ChannelHandlerContextctx) { this.ctx = ctx; } public void login(String username, password) { ctx.write(new LoginMessage(username, password)); } ... }Storing stateful information
attr(AttributeKey)allow you to store and access stateful information that is related with aChannelHandler/Channeland its context. Please refer toChannelHandlerto learn various recommended ways to manage stateful information.A handler can have more than one
Please note that aChannelHandlerContextChannelHandlerinstance can be added to more than oneChannelPipeline. It means a singleChannelHandlerinstance can have more than oneChannelHandlerContextand therefore the single instance can be invoked with differentChannelHandlerContexts if it is added to one or moreChannelPipelines more than once. Also note that aChannelHandlerthat is supposed to be added to multipleChannelPipelines should be marked asChannelHandler.Sharable.Additional resources worth reading
Please refer to the
ChannelHandler, andChannelPipelineto 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.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description io.netty.buffer.ByteBufAllocatoralloc()Return the assignedByteBufAllocatorwhich will be used to allocateByteBufs.<T> io.netty.util.Attribute<T>attr(io.netty.util.AttributeKey<T> key)Deprecated.UseAttributeMap.attr(AttributeKey)Channelchannel()Return theChannelwhich is bound to theChannelHandlerContext.io.netty.util.concurrent.EventExecutorexecutor()Returns theEventExecutorwhich is used to execute an arbitrary task.ChannelHandlerContextfireChannelActive()AChannelis active now, which means it is connected.ChannelHandlerContextfireChannelInactive()AChannelis inactive now, which means it is closed.ChannelHandlerContextfireChannelRead(Object msg)AChannelreceived a message.ChannelHandlerContextfireChannelReadComplete()Triggers anChannelInboundHandler.channelReadComplete(ChannelHandlerContext)event to the nextChannelInboundHandlerin theChannelPipeline.ChannelHandlerContextfireChannelRegistered()ChannelHandlerContextfireChannelUnregistered()ChannelHandlerContextfireChannelWritabilityChanged()Triggers anChannelInboundHandler.channelWritabilityChanged(ChannelHandlerContext)event to the nextChannelInboundHandlerin theChannelPipeline.ChannelHandlerContextfireExceptionCaught(Throwable cause)ChannelHandlerContextfireUserEventTriggered(Object evt)AChannelreceived an user defined event.ChannelHandlerContextflush()Request to flush all pending messages via this ChannelOutboundInvoker.ChannelHandlerhandler()TheChannelHandlerthat is bound thisChannelHandlerContext.<T> booleanhasAttr(io.netty.util.AttributeKey<T> key)Deprecated.UseAttributeMap.hasAttr(AttributeKey)booleanisRemoved()Returntrueif theChannelHandlerwhich belongs to this context was removed from theChannelPipeline.Stringname()The unique name of theChannelHandlerContext.The name was used when thenChannelHandlerwas added to theChannelPipeline.ChannelPipelinepipeline()Return the assignedChannelPipelineChannelHandlerContextread()Request to Read data from theChannelinto the first inbound buffer, triggers anChannelInboundHandler.channelRead(ChannelHandlerContext, Object)event if data was read, and triggers achannelReadCompleteevent so the handler can decide to continue reading.-
Methods inherited from interface io.netty.channel.ChannelOutboundInvoker
bind, bind, close, close, connect, connect, connect, connect, deregister, deregister, disconnect, disconnect, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, voidPromise, write, write, writeAndFlush, writeAndFlush
-
-
-
-
Method Detail
-
channel
Channel channel()
Return theChannelwhich is bound to theChannelHandlerContext.
-
executor
io.netty.util.concurrent.EventExecutor executor()
Returns theEventExecutorwhich is used to execute an arbitrary task.
-
name
String name()
The unique name of theChannelHandlerContext.The name was used when thenChannelHandlerwas added to theChannelPipeline. This name can also be used to access the registeredChannelHandlerfrom theChannelPipeline.
-
handler
ChannelHandler handler()
TheChannelHandlerthat is bound thisChannelHandlerContext.
-
isRemoved
boolean isRemoved()
Returntrueif theChannelHandlerwhich belongs to this context was removed from theChannelPipeline. Note that this method is only meant to be called from with in theEventLoop.
-
fireChannelRegistered
ChannelHandlerContext fireChannelRegistered()
Description copied from interface:ChannelInboundInvokerAChannelwas registered to itsEventLoop. This will result in having theChannelInboundHandler.channelRegistered(ChannelHandlerContext)method called of the nextChannelInboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
fireChannelRegisteredin interfaceChannelInboundInvoker
-
fireChannelUnregistered
ChannelHandlerContext fireChannelUnregistered()
Description copied from interface:ChannelInboundInvokerAChannelwas unregistered from itsEventLoop. This will result in having theChannelInboundHandler.channelUnregistered(ChannelHandlerContext)method called of the nextChannelInboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
fireChannelUnregisteredin interfaceChannelInboundInvoker
-
fireChannelActive
ChannelHandlerContext fireChannelActive()
Description copied from interface:ChannelInboundInvokerAChannelis active now, which means it is connected. This will result in having theChannelInboundHandler.channelActive(ChannelHandlerContext)method called of the nextChannelInboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
fireChannelActivein interfaceChannelInboundInvoker
-
fireChannelInactive
ChannelHandlerContext fireChannelInactive()
Description copied from interface:ChannelInboundInvokerAChannelis inactive now, which means it is closed. This will result in having theChannelInboundHandler.channelInactive(ChannelHandlerContext)method called of the nextChannelInboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
fireChannelInactivein interfaceChannelInboundInvoker
-
fireExceptionCaught
ChannelHandlerContext fireExceptionCaught(Throwable cause)
Description copied from interface:ChannelInboundInvokerAChannelreceived anThrowablein one of its inbound operations. This will result in having theChannelInboundHandler.exceptionCaught(ChannelHandlerContext, Throwable)method called of the nextChannelInboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
fireExceptionCaughtin interfaceChannelInboundInvoker
-
fireUserEventTriggered
ChannelHandlerContext fireUserEventTriggered(Object evt)
Description copied from interface:ChannelInboundInvokerAChannelreceived an user defined event. This will result in having theChannelInboundHandler.userEventTriggered(ChannelHandlerContext, Object)method called of the nextChannelInboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
fireUserEventTriggeredin interfaceChannelInboundInvoker
-
fireChannelRead
ChannelHandlerContext fireChannelRead(Object msg)
Description copied from interface:ChannelInboundInvokerAChannelreceived a message. This will result in having theChannelInboundHandler.channelRead(ChannelHandlerContext, Object)method called of the nextChannelInboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
fireChannelReadin interfaceChannelInboundInvoker
-
fireChannelReadComplete
ChannelHandlerContext fireChannelReadComplete()
Description copied from interface:ChannelInboundInvokerTriggers anChannelInboundHandler.channelReadComplete(ChannelHandlerContext)event to the nextChannelInboundHandlerin theChannelPipeline.- Specified by:
fireChannelReadCompletein interfaceChannelInboundInvoker
-
fireChannelWritabilityChanged
ChannelHandlerContext fireChannelWritabilityChanged()
Description copied from interface:ChannelInboundInvokerTriggers anChannelInboundHandler.channelWritabilityChanged(ChannelHandlerContext)event to the nextChannelInboundHandlerin theChannelPipeline.- Specified by:
fireChannelWritabilityChangedin interfaceChannelInboundInvoker
-
read
ChannelHandlerContext read()
Description copied from interface:ChannelOutboundInvokerRequest to Read data from theChannelinto the first inbound buffer, triggers anChannelInboundHandler.channelRead(ChannelHandlerContext, Object)event if data was read, and triggers achannelReadCompleteevent 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 nextChannelOutboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
readin interfaceChannelOutboundInvoker
-
flush
ChannelHandlerContext flush()
Description copied from interface:ChannelOutboundInvokerRequest to flush all pending messages via this ChannelOutboundInvoker.- Specified by:
flushin interfaceChannelOutboundInvoker
-
pipeline
ChannelPipeline pipeline()
Return the assignedChannelPipeline
-
alloc
io.netty.buffer.ByteBufAllocator alloc()
Return the assignedByteBufAllocatorwhich will be used to allocateByteBufs.
-
attr
@Deprecated <T> io.netty.util.Attribute<T> attr(io.netty.util.AttributeKey<T> key)
Deprecated.UseAttributeMap.attr(AttributeKey)- Specified by:
attrin interfaceio.netty.util.AttributeMap
-
hasAttr
@Deprecated <T> boolean hasAttr(io.netty.util.AttributeKey<T> key)
Deprecated.UseAttributeMap.hasAttr(AttributeKey)- Specified by:
hasAttrin interfaceio.netty.util.AttributeMap
-
-