C - A sub-type of Channel@ChannelHandler.Sharable public abstract class ChannelInitializer<C extends Channel> extends ChannelInboundHandlerAdapter
ChannelInboundHandler which offers an easy way to initialize a Channel once it was
registered to its EventLoop.
Implementations are most often used in the context of AbstractBootstrap.handler(ChannelHandler) ,
AbstractBootstrap.handler(ChannelHandler) and ServerBootstrap.childHandler(ChannelHandler) to
setup the ChannelPipeline of a Channel.
public class MyChannelInitializer extendsBe aware that this class is marked asChannelInitializer{ public void initChannel(Channelchannel) { channel.pipeline().addLast("myHandler", new MyHandler()); } }ServerBootstrapbootstrap = ...; ... bootstrap.childHandler(new MyChannelInitializer()); ...
ChannelHandler.Sharable and so the implementation must be safe to be re-used.ChannelHandler.Sharable| 构造器和说明 |
|---|
ChannelInitializer() |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
channelRegistered(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelRegistered() to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
void |
exceptionCaught(ChannelHandlerContext ctx,
java.lang.Throwable cause)
Handle the
Throwable by logging and closing the Channel. |
void |
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
void |
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
protected abstract void |
initChannel(C ch)
This method will be called once the
Channel was registered. |
channelActive, channelInactive, channelRead, channelReadComplete, channelUnregistered, channelWritabilityChanged, userEventTriggeredensureNotSharable, isSharableprotected abstract void initChannel(C ch) throws java.lang.Exception
Channel was registered. After the method returns this instance
will be removed from the ChannelPipeline of the Channel.ch - the Channel which was registered.java.lang.Exception - is thrown if an error occurs. In that case it will be handled by
exceptionCaught(ChannelHandlerContext, Throwable) which will by default close
the Channel.public final void channelRegistered(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelRegistered() to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelRegistered 在接口中 ChannelInboundHandlerchannelRegistered 在类中 ChannelInboundHandlerAdapterjava.lang.Exceptionpublic void exceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.Exception
Throwable by logging and closing the Channel. Sub-classes may override this.exceptionCaught 在接口中 ChannelHandlerexceptionCaught 在接口中 ChannelInboundHandlerexceptionCaught 在类中 ChannelInboundHandlerAdapterjava.lang.Exceptionpublic void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
handlerAdded 在接口中 ChannelHandlerhandlerAdded 在类中 ChannelHandlerAdapterjava.lang.Exceptionpublic void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapterhandlerRemoved 在接口中 ChannelHandlerhandlerRemoved 在类中 ChannelHandlerAdapterjava.lang.Exception