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,BasicServerChannelInitializer,SimpleChannelInitializer,SslInitializer
@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(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.
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidchannelRegistered(io.netty.channel.ChannelHandlerContext ctx) protected io.netty.channel.ChannelHandlerContextvoidexceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) Handle theThrowableby logging and closing theChannel.voidhandlerAdded(io.netty.channel.ChannelHandlerContext ctx) If override this method ensure you call super!voidhandlerRemoved(io.netty.channel.ChannelHandlerContext ctx) If override this method ensure you call super!protected abstract voidinitChannel(C ch) This method will be called once theChannelwas registered.Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelRead, channelReadComplete, channelUnregistered, channelWritabilityChanged, userEventTriggeredMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Constructor Details
-
ChannelInitializer
public ChannelInitializer()
-
-
Method Details
-
initChannel
This method will be called once theChannelwas registered. After the method returns this instance will be removed from theChannelPipelineof theChannel.- Parameters:
ch- theChannelwhich 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:
channelRegisteredin interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelRegisteredin classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
exceptionCaught
public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) throws Exception Handle theThrowableby logging and closing theChannel. Sub-classes may override this.- Specified by:
exceptionCaughtin interfaceio.netty.channel.ChannelHandler- Specified by:
exceptionCaughtin interfaceio.netty.channel.ChannelInboundHandler- Overrides:
exceptionCaughtin classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
handlerAdded
If override this method ensure you call super!- Specified by:
handlerAddedin interfaceio.netty.channel.ChannelHandler- Overrides:
handlerAddedin classio.netty.channel.ChannelHandlerAdapter- Throws:
Exception
-
handlerRemoved
If override this method ensure you call super!- Specified by:
handlerRemovedin interfaceio.netty.channel.ChannelHandler- Overrides:
handlerRemovedin classio.netty.channel.ChannelHandlerAdapter- Throws:
Exception
-
currentContext
protected io.netty.channel.ChannelHandlerContext currentContext()
-