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.ChannelInboundHandlerAdapterA specialChannelInboundHandlerwhich offers an easy way to initialize aChannelonce it was registered to itsEventLoop. Implementations are most often used in the context ofAbstractBootstrap.handler(io.netty.channel.ChannelHandler),AbstractBootstrap.handler(io.netty.channel.ChannelHandler)andServerBootstrap.childHandler(io.netty.channel.ChannelHandler)to setup theChannelPipelineof aChannel.public class MyChannelInitializer extends
Be aware that this class is marked asChannelInitializer{ public void initChannel(Channelchannel) { channel.pipeline().addLast("myHandler", new MyHandler()); } }ServerBootstrapbootstrap = ...; ... bootstrap.childHandler(new MyChannelInitializer()); ...ChannelHandler.Sharableand so the implementation must be safe to be re-used.Note that this contains a workaround for https://github.com/netty/netty/issues/8616
-
-
Constructor Summary
Constructors Constructor Description ChannelInitializer()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidchannelRegistered(io.netty.channel.ChannelHandlerContext ctx)protected io.netty.channel.ChannelHandlerContextcurrentContext()voidexceptionCaught(io.netty.channel.ChannelHandlerContext ctx, java.lang.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.
-
-
-
Method Detail
-
initChannel
protected abstract void initChannel(C ch) throws java.lang.Exception
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:
java.lang.Exception- is thrown if an error occurs. In that case it will be handled byexceptionCaught(ChannelHandlerContext, Throwable)which will by default close theChannel.
-
channelRegistered
public final void channelRegistered(io.netty.channel.ChannelHandlerContext ctx) throws java.lang.Exception- Specified by:
channelRegisteredin interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelRegisteredin classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
exceptionCaught
public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.ExceptionHandle 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:
java.lang.Exception
-
handlerAdded
public void handlerAdded(io.netty.channel.ChannelHandlerContext ctx) throws java.lang.ExceptionIf override this method ensure you call super!- Specified by:
handlerAddedin interfaceio.netty.channel.ChannelHandler- Overrides:
handlerAddedin classio.netty.channel.ChannelHandlerAdapter- Throws:
java.lang.Exception
-
handlerRemoved
public void handlerRemoved(io.netty.channel.ChannelHandlerContext ctx) throws java.lang.ExceptionIf override this method ensure you call super!- Specified by:
handlerRemovedin interfaceio.netty.channel.ChannelHandler- Overrides:
handlerRemovedin classio.netty.channel.ChannelHandlerAdapter- Throws:
java.lang.Exception
-
currentContext
protected io.netty.channel.ChannelHandlerContext currentContext()
-
-