Class ChannelInitializer<C extends io.netty.channel.Channel>

  • 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
    • 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 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 initChannel​(C ch)
      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 Detail

      • ChannelInitializer

        public ChannelInitializer()
    • Method Detail

      • initChannel

        protected abstract void initChannel​(C ch)
                                     throws java.lang.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:
        java.lang.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 java.lang.Exception
        Specified by:
        channelRegistered in interface io.netty.channel.ChannelInboundHandler
        Overrides:
        channelRegistered in class io.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 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:
        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 interface io.netty.channel.ChannelHandler
        Overrides:
        handlerAdded in class io.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 interface io.netty.channel.ChannelHandler
        Overrides:
        handlerRemoved in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        java.lang.Exception
      • currentContext

        protected io.netty.channel.ChannelHandlerContext currentContext()