Top Description Methods
io.netty.channel

public Interface ChannelHandlerContext

extends AttributeMap, ChannelInboundInvoker, ChannelOutboundInvoker
Known Direct Implementers
io.netty.channel.CombinedChannelDuplexHandler.DelegatingChannelHandlerContext, io.netty.channel.AbstractChannelHandlerContext
Imports
io.netty.buffer.ByteBuf, .ByteBufAllocator, io.netty.util.Attribute, .AttributeKey, .AttributeMap, io.netty.util.concurrent.EventExecutor

Enables a 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.

Notify

You can notify the closest handler in the same ChannelPipeline by calling one of the various methods provided here. Please refer to ChannelPipeline to understand how an event flows.

Modifying a pipeline

You can get the ChannelPipeline your handler belongs to by calling pipeline(). A non-trivial application could insert, remove, or replace handlers in the pipeline dynamically at runtime.

Retrieving for later use

You can keep the ChannelHandlerContext for later use, such as triggering an event outside the handler methods, even from a different thread.
public class MyHandler extends ChannelDuplexHandler {

    private ChannelHandlerContext ctx;

    public void beforeAdd(ChannelHandlerContext ctx) {
        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 a ChannelHandler / Channel and its context. Please refer to ChannelHandler to learn various recommended ways to manage stateful information.

A handler can have more than one ChannelHandlerContext

Please note that a 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. Also note that a ChannelHandler that is supposed to be added to multiple ChannelPipelines should be marked as io.netty.channel.ChannelHandler.Sharable.

Additional resources worth reading

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.

Method Summary

Modifier and TypeMethod and Description
public ByteBufAllocator
alloc()

Return the assigned ByteBufAllocator which will be used to allocate ByteBufs.

public <T> Attribute<T>
attr(AttributeKey<T> key)

Redeclares io.netty.util.AttributeMap.attr.

Get the Attribute for the given AttributeKey.
public Channel
channel()

Return the Channel which is bound to the ChannelHandlerContext.

public EventExecutor
executor()

Returns the EventExecutor which is used to execute an arbitrary task.

public ChannelHandlerContext
fireChannelActive()

Redeclares io.netty.channel.ChannelInboundInvoker.fireChannelActive.

A Channel is active now, which means it is connected.
public ChannelHandlerContext
fireChannelInactive()

Redeclares io.netty.channel.ChannelInboundInvoker.fireChannelInactive.

A Channel is inactive now, which means it is closed.
public ChannelHandlerContext
public ChannelHandlerContext
public ChannelHandlerContext
public ChannelHandlerContext
public ChannelHandlerContext
public ChannelHandlerContext
fireExceptionCaught(Throwable cause)

Redeclares io.netty.channel.ChannelInboundInvoker.fireExceptionCaught.

A Channel received an Throwable in one of its inbound operations.
public ChannelHandlerContext
public ChannelHandlerContext
flush()

Redeclares io.netty.channel.ChannelOutboundInvoker.flush.

Request to flush all pending messages via this ChannelOutboundInvoker.
public ChannelHandler
handler()

The ChannelHandler that is bound this ChannelHandlerContext.

public <T> boolean
hasAttr(AttributeKey<T> key)

Redeclares io.netty.util.AttributeMap.hasAttr.

Returns true if and only if the given Attribute exists in this AttributeMap.
public boolean
isRemoved()

Return true if the ChannelHandler which belongs to this context was removed from the ChannelPipeline.

public String
name()

The unique name of the ChannelHandlerContext.The name was used when then ChannelHandler was added to the ChannelPipeline.

public ChannelPipeline
pipeline()

Return the assigned ChannelPipeline

public ChannelHandlerContext
read()

Redeclares io.netty.channel.ChannelOutboundInvoker.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.
Inherited from io.netty.channel.ChannelOutboundInvoker:
bindbindclosecloseconnectconnectconnectconnectderegisterderegisterdisconnectdisconnectnewFailedFuturenewProgressivePromisenewPromisenewSucceededFuturevoidPromisewritewritewriteAndFlushwriteAndFlush

Method Detail

allocback to summary
public ByteBufAllocator alloc()

Return the assigned ByteBufAllocator which will be used to allocate ByteBufs.

attrback to summary
public <T> Attribute<T> attr(AttributeKey<T> key)

Redeclares io.netty.util.AttributeMap.attr.

Doc from io.netty.util.AttributeMap.attr.

Get the Attribute for the given AttributeKey. This method will never return null, but may return an Attribute which does not have a value set yet.

Annotations
@Deprecated
@Override
channelback to summary
public Channel channel()

Return the Channel which is bound to the ChannelHandlerContext.

executorback to summary
public EventExecutor executor()

Returns the EventExecutor which is used to execute an arbitrary task.

fireChannelActiveback to summary
public ChannelHandlerContext fireChannelActive()

Redeclares io.netty.channel.ChannelInboundInvoker.fireChannelActive.

Doc from io.netty.channel.ChannelInboundInvoker.fireChannelActive.

A 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.

Annotations
@Override
fireChannelInactiveback to summary
public ChannelHandlerContext fireChannelInactive()

Redeclares io.netty.channel.ChannelInboundInvoker.fireChannelInactive.

Doc from io.netty.channel.ChannelInboundInvoker.fireChannelInactive.

A 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.

Annotations
@Override
fireChannelReadback to summary
public ChannelHandlerContext fireChannelRead(Object msg)

Redeclares io.netty.channel.ChannelInboundInvoker.fireChannelRead.

Doc from io.netty.channel.ChannelInboundInvoker.fireChannelRead.

A 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.

Annotations
@Override
fireChannelReadCompleteback to summary
public ChannelHandlerContext fireChannelReadComplete()

Redeclares io.netty.channel.ChannelInboundInvoker.fireChannelReadComplete.

Doc from io.netty.channel.ChannelInboundInvoker.fireChannelReadComplete.

Triggers an ChannelInboundHandler#channelReadComplete(ChannelHandlerContext) event to the next ChannelInboundHandler in the ChannelPipeline.

Annotations
@Override
fireChannelRegisteredback to summary
public ChannelHandlerContext fireChannelRegistered()

Redeclares io.netty.channel.ChannelInboundInvoker.fireChannelRegistered.

Doc from io.netty.channel.ChannelInboundInvoker.fireChannelRegistered.

A 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.

Annotations
@Override
fireChannelUnregisteredback to summary
public ChannelHandlerContext fireChannelUnregistered()

Redeclares io.netty.channel.ChannelInboundInvoker.fireChannelUnregistered.

Doc from io.netty.channel.ChannelInboundInvoker.fireChannelUnregistered.

A 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.

Annotations
@Override
fireChannelWritabilityChangedback to summary
public ChannelHandlerContext fireChannelWritabilityChanged()

Redeclares io.netty.channel.ChannelInboundInvoker.fireChannelWritabilityChanged.

Doc from io.netty.channel.ChannelInboundInvoker.fireChannelWritabilityChanged.

Triggers an ChannelInboundHandler#channelWritabilityChanged(ChannelHandlerContext) event to the next ChannelInboundHandler in the ChannelPipeline.

Annotations
@Override
fireExceptionCaughtback to summary
public ChannelHandlerContext fireExceptionCaught(Throwable cause)

Redeclares io.netty.channel.ChannelInboundInvoker.fireExceptionCaught.

Doc from io.netty.channel.ChannelInboundInvoker.fireExceptionCaught.

A 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.

Annotations
@Override
fireUserEventTriggeredback to summary
public ChannelHandlerContext fireUserEventTriggered(Object evt)

Redeclares io.netty.channel.ChannelInboundInvoker.fireUserEventTriggered.

Doc from io.netty.channel.ChannelInboundInvoker.fireUserEventTriggered.

A 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.

Annotations
@Override
flushback to summary
public ChannelHandlerContext flush()

Redeclares io.netty.channel.ChannelOutboundInvoker.flush.

Doc from io.netty.channel.ChannelOutboundInvoker.flush.

Request to flush all pending messages via this ChannelOutboundInvoker.

Annotations
@Override
handlerback to summary
public ChannelHandler handler()

The ChannelHandler that is bound this ChannelHandlerContext.

hasAttrback to summary
public <T> boolean hasAttr(AttributeKey<T> key)

Redeclares io.netty.util.AttributeMap.hasAttr.

Doc from io.netty.util.AttributeMap.hasAttr.

Returns true if and only if the given Attribute exists in this AttributeMap.

Annotations
@Deprecated
@Override
isRemovedback to summary
public boolean isRemoved()

Return 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.

nameback to summary
public String name()

The unique name of the 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.

pipelineback to summary
public ChannelPipeline pipeline()

Return the assigned ChannelPipeline

readback to summary
public ChannelHandlerContext read()

Redeclares io.netty.channel.ChannelOutboundInvoker.read.

Doc from io.netty.channel.ChannelOutboundInvoker.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. 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.

Annotations
@Override