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 of Channel
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 extends ChannelInitializer {
     public void initChannel(Channel channel) {
         channel.pipeline().addLast("myHandler", new MyHandler());
     }
 }

 ServerBootstrap bootstrap = ...;
 ...
 bootstrap.childHandler(new MyChannelInitializer());
 ...
 
Be aware that this class is marked as 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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    channelRegistered(io.netty.channel.ChannelHandlerContext ctx)
     
    protected io.netty.channel.ChannelHandlerContext
     
    void
    exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause)
    Handle the Throwable by logging and closing the Channel.
    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
    This method will be called once the Channel 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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ChannelInitializer

      public ChannelInitializer()
  • Method Details

    • initChannel

      protected abstract void initChannel(C ch) throws Exception
      This method will be called once the Channel was registered. After the method returns this instance will be removed from the ChannelPipeline of the Channel.
      Parameters:
      ch - the Channel which was registered.
      Throws:
      Exception - is thrown if an error occurs. In that case it will be handled by exceptionCaught(ChannelHandlerContext, Throwable) which will by default close the Channel.
    • channelRegistered

      public final void channelRegistered(io.netty.channel.ChannelHandlerContext ctx) throws Exception
      Specified by:
      channelRegistered in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelRegistered in class io.netty.channel.ChannelInboundHandlerAdapter
      Throws:
      Exception
    • exceptionCaught

      public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) throws Exception
      Handle the Throwable by logging and closing the Channel. Sub-classes may override this.
      Specified by:
      exceptionCaught in interface io.netty.channel.ChannelHandler
      Specified by:
      exceptionCaught in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      exceptionCaught in class io.netty.channel.ChannelInboundHandlerAdapter
      Throws:
      Exception
    • handlerAdded

      public void handlerAdded(io.netty.channel.ChannelHandlerContext ctx) throws Exception
      If override this method ensure you call super!
      Specified by:
      handlerAdded in interface io.netty.channel.ChannelHandler
      Overrides:
      handlerAdded in class io.netty.channel.ChannelHandlerAdapter
      Throws:
      Exception
    • handlerRemoved

      public void handlerRemoved(io.netty.channel.ChannelHandlerContext ctx) throws Exception
      If override this method ensure you call super!
      Specified by:
      handlerRemoved in interface io.netty.channel.ChannelHandler
      Overrides:
      handlerRemoved in class io.netty.channel.ChannelHandlerAdapter
      Throws:
      Exception
    • currentContext

      protected io.netty.channel.ChannelHandlerContext currentContext()