Top Description Fields Constructors Methods
io.netty.channel

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

extends ChannelInboundHandlerAdapter
Class Inheritance
Annotations
@Sharable
Type Parameters
<C>
A sub-type of Channel
Imports
io.netty.bootstrap.Bootstrap, .ServerBootstrap, io.netty.channel.ChannelHandler.Sharable, io.netty.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.util.Collections, .Set, java.util.concurrent.ConcurrentHashMap

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 Bootstrap#handler(ChannelHandler) , ServerBootstrap#handler(ChannelHandler) and ServerBootstrap#childHandler(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 Sharable and so the implementation must be safe to be re-used.

Field Summary

Modifier and TypeField and Description
private final Set<ChannelHandlerContext>
private static final InternalLogger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final void
public void
public void
handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
public void
handlerRemoved(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.
protected abstract void
initChannel(C
the Channel which was registered.
ch
)

This method will be called once the Channel was registered.

private boolean
private void
Inherited from io.netty.channel.ChannelInboundHandlerAdapter:
channelActivechannelInactivechannelReadchannelReadCompletechannelUnregisteredchannelWritabilityChangeduserEventTriggered

Field Detail

initMapback to summary
private final Set<ChannelHandlerContext> initMap
loggerback to summary
private static final InternalLogger logger

Constructor Detail

ChannelInitializerback to summary
public ChannelInitializer()

Method Detail

channelRegisteredback to summary
public final void channelRegistered(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelRegistered.

Implements io.netty.channel.ChannelInboundHandler.channelRegistered.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelRegistered.

Calls ChannelHandlerContext#fireChannelRegistered() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
@SuppressWarnings:unchecked
exceptionCaughtback to summary
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.exceptionCaught.

Implements io.netty.channel.ChannelInboundHandler.exceptionCaught, io.netty.channel.ChannelHandler.exceptionCaught.

Handle the Throwable by logging and closing the Channel. Sub-classes may override this.

Annotations
@Override
handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Doc from io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events. If override this method ensure you call super!

Annotations
@Override
handlerRemovedback to summary
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Doc from io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.

Annotations
@Override
initChannelback to summary
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:C

the Channel which was registered.

Exceptions
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.
initChannelback to summary
private boolean initChannel(ChannelHandlerContext ctx) throws Exception
Annotations
@SuppressWarnings:unchecked
removeStateback to summary
private void removeState(final ChannelHandlerContext ctx)