Package io.netty.channel
Class ChannelInitializer<C extends Channel>
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelInitializer<C>
- Type Parameters:
C
- A sub-type ofChannel
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
- Direct Known Subclasses:
OcspClient.Initializer
@Sharable
public abstract class ChannelInitializer<C extends Channel>
extends ChannelInboundHandlerAdapter
A special
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.-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Set
<ChannelHandlerContext> private static final InternalLogger
Fields inherited from class io.netty.channel.ChannelHandlerAdapter
added
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal void
CallsChannelHandlerContext.fireChannelRegistered()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
exceptionCaught
(ChannelHandlerContext ctx, Throwable cause) void
Do nothing by default, sub-classes may override this method.void
Do nothing by default, sub-classes may override this method.protected abstract void
initChannel
(C ch) This method will be called once theChannel
was registered.private boolean
private void
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelRead, channelReadComplete, channelUnregistered, channelWritabilityChanged, userEventTriggered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Field Details
-
logger
-
initMap
-
-
Constructor Details
-
ChannelInitializer
public ChannelInitializer()
-
-
Method Details
-
initChannel
This method will be called once theChannel
was registered. After the method returns this instance will be removed from theChannelPipeline
of theChannel
.- Parameters:
ch
- theChannel
which was registered.- Throws:
Exception
- is thrown if an error occurs. In that case it will be handled byexceptionCaught(ChannelHandlerContext, Throwable)
which will by default close theChannel
.
-
channelRegistered
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRegistered()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRegistered
in interfaceChannelInboundHandler
- Overrides:
channelRegistered
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
exceptionCaught
- Specified by:
exceptionCaught
in interfaceChannelHandler
- Specified by:
exceptionCaught
in interfaceChannelInboundHandler
- Overrides:
exceptionCaught
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
handlerAdded
Do nothing by default, sub-classes may override this method. If override this method ensure you call super!- Specified by:
handlerAdded
in interfaceChannelHandler
- Overrides:
handlerAdded
in classChannelHandlerAdapter
- Throws:
Exception
-
handlerRemoved
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
Exception
-
initChannel
- Throws:
Exception
-
removeState
-