public interface LifeCycleAwareChannelHandler extends ChannelHandler
ChannelHandler
that is notified when it is added to or removed
from a ChannelPipeline
.
Please note that the methods of this handler is called only when the
ChannelPipeline
it belongs to has been
attached.
That is, if you add a LifeCycleAwareChannelHandler
to the unattached
ChannelPipeline
, the life cycle handler methods in this handler will
not be invoked because there's no associated ChannelHandlerContext
:
// Create a new pipeline which is unattached initially. ChannelPipeline pipeline = Channels.pipeline(); // beforeAdd() and afterAdd() will not be called. pipeline.addLast("handler", new MyLifeCycleAwareChannelHandler()); // beforeRemove() and afterRemove() will not be called. pipeline.remove("handler");However, once the
ChannelPipeline
is attached and the
channelOpen
event is fired, the life cycle handler methods will
be invoked on addition or removal:
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent evt) { // channelOpen event has been triggered, which means the pipeline has // been attached to the channel. ChannelPipeline pipeline = ctx.getChannel().getPipeline(); // beforeAdd() and afterAdd() will be called. pipeline.addLast("handler", new MyLifeCycleAwareChannelHandler()); } public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent evt) { // Once attached, the pipeline is never detached. ChannelPipeline pipeline = ctx.getChannel().getPipeline(); // beforeRemove() and afterRemove() will be called. pipeline.remove("handler"); }
Modifier and Type | Method and Description |
---|---|
void |
afterAdd(ChannelHandlerContext ctx) |
void |
afterRemove(ChannelHandlerContext ctx) |
void |
beforeAdd(ChannelHandlerContext ctx) |
void |
beforeRemove(ChannelHandlerContext ctx) |
void beforeAdd(ChannelHandlerContext ctx) throws Exception
Exception
void afterAdd(ChannelHandlerContext ctx) throws Exception
Exception
void beforeRemove(ChannelHandlerContext ctx) throws Exception
Exception
void afterRemove(ChannelHandlerContext ctx) throws Exception
Exception
Copyright © 2008–2013 JBoss, by Red Hat. All rights reserved.