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(Channel
channel) { channel.pipeline().addLast("myHandler", new MyHandler()); } }ServerBootstrap
bootstrap = ...; ... bootstrap.childHandler(new MyChannelInitializer()); ...
ChannelHandler.Sharable
and so the implementation must be safe to be re-used.ChannelHandler.Sharable
Constructor and Description |
---|
ChannelInitializer() |
Modifier and Type | Method and Description |
---|---|
void |
channelRegistered(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelRegistered() to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
void |
exceptionCaught(ChannelHandlerContext ctx,
Throwable cause)
|
void |
handlerAdded(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, userEventTriggered
ensureNotSharable, handlerRemoved, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handlerRemoved
protected abstract void initChannel(C ch) throws 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.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 Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelRegistered()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelRegistered
in interface ChannelInboundHandler
channelRegistered
in class ChannelInboundHandlerAdapter
Exception
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
exceptionCaught
in interface ChannelHandler
exceptionCaught
in interface ChannelInboundHandler
exceptionCaught
in class ChannelInboundHandlerAdapter
Exception
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
handlerAdded
in interface ChannelHandler
handlerAdded
in class ChannelHandlerAdapter
Exception
Copyright © 2008–2017 The Netty Project. All rights reserved.