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 specialChannelInboundHandler
which offers an easy way to initialize aChannel
once 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 theChannelPipeline
of aChannel
.public class MyChannelInitializer extends
Be 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
-
-
Constructor Summary
Constructors Constructor Description ChannelInitializer()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
channelRegistered(io.netty.channel.ChannelHandlerContext ctx)
protected io.netty.channel.ChannelHandlerContext
currentContext()
void
exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, java.lang.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.
-
-
-
Method Detail
-
initChannel
protected abstract void initChannel(C ch) throws java.lang.Exception
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:
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:
channelRegistered
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelRegistered
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
java.lang.Exception
-
exceptionCaught
public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.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:
java.lang.Exception
-
handlerAdded
public void handlerAdded(io.netty.channel.ChannelHandlerContext ctx) throws java.lang.Exception
If override this method ensure you call super!- Specified by:
handlerAdded
in interfaceio.netty.channel.ChannelHandler
- Overrides:
handlerAdded
in classio.netty.channel.ChannelHandlerAdapter
- Throws:
java.lang.Exception
-
handlerRemoved
public void handlerRemoved(io.netty.channel.ChannelHandlerContext ctx) throws java.lang.Exception
If override this method ensure you call super!- Specified by:
handlerRemoved
in interfaceio.netty.channel.ChannelHandler
- Overrides:
handlerRemoved
in classio.netty.channel.ChannelHandlerAdapter
- Throws:
java.lang.Exception
-
currentContext
protected io.netty.channel.ChannelHandlerContext currentContext()
-
-