Class ChannelInitializer<C extends io.netty.channel.Channel>
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
com.linkedin.alpini.netty4.handlers.ChannelInitializer<C>
- Type Parameters:
C
- A sub-type ofChannel
- All Implemented Interfaces:
io.netty.channel.ChannelHandler
,io.netty.channel.ChannelInboundHandler
- Direct Known Subclasses:
AsyncFullHttpRequestHandler
,BackgroundChannelHandler
,BasicServerChannelInitializer
,ChunkedResponseHandler
,SimpleChannelInitializer
,SslClientInitializer
,SslInitializer
,VIPRequestHandler
@Sharable
public abstract class ChannelInitializer<C extends io.netty.channel.Channel>
extends io.netty.channel.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(io.netty.channel.ChannelHandler)
,
AbstractBootstrap.handler(io.netty.channel.ChannelHandler)
and
ServerBootstrap.childHandler(io.netty.channel.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.
Note that this contains a workaround for https://github.com/netty/netty/issues/8616
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
io.netty.channel.ChannelHandler.Sharable
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal void
channelRegistered
(io.netty.channel.ChannelHandlerContext ctx) protected io.netty.channel.ChannelHandlerContext
void
exceptionCaught
(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) Handle theThrowable
by logging and closing theChannel
.void
handlerAdded
(io.netty.channel.ChannelHandlerContext ctx) If override this method ensure you call super!void
handlerRemoved
(io.netty.channel.ChannelHandlerContext ctx) If override this method ensure you call super!protected abstract void
initChannel
(C ch) This method will be called once theChannel
was registered.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
-
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
- Specified by:
channelRegistered
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelRegistered
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
Exception
-
exceptionCaught
public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) throws Exception Handle theThrowable
by logging and closing theChannel
. Sub-classes may override this.- Specified by:
exceptionCaught
in interfaceio.netty.channel.ChannelHandler
- Specified by:
exceptionCaught
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
exceptionCaught
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
Exception
-
handlerAdded
If override this method ensure you call super!- Specified by:
handlerAdded
in interfaceio.netty.channel.ChannelHandler
- Overrides:
handlerAdded
in classio.netty.channel.ChannelHandlerAdapter
- Throws:
Exception
-
handlerRemoved
If override this method ensure you call super!- Specified by:
handlerRemoved
in interfaceio.netty.channel.ChannelHandler
- Overrides:
handlerRemoved
in classio.netty.channel.ChannelHandlerAdapter
- Throws:
Exception
-
currentContext
protected io.netty.channel.ChannelHandlerContext currentContext()
-