Storing stateful information

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.

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 extends ChannelInboundHandlerAdapter&ltInteger&gt {
private final AttributeKey<Integer> counter =
new AttributeKey<Integer>("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));
}
}
// 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 an AttributeKey), 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);

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.

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.

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.

Method Detail

channel

executor

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

connect

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.

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.
After it is closed it is not possible to reuse it again.

bind

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.
The given ChannelPromise will be notified.

connect

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

connect

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.
The given ChannelPromise will be notified and also returned.

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.
The given ChannelPromise will be notified.

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.
After it is closed it is not possible to reuse it again.
The given ChannelPromise will be notified.

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.
The given ChannelPromise will be notified.

write

Request to write a message via this 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.

write

Request to write a message via this 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.

newProgressivePromise

newSucceededFuture

Create a new 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.

newFailedFuture

Create a new 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.

voidPromise

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.