Top Description Fields Constructors Methods
io.netty5.channel

public abstract Class ChannelInitializer<C extends Channel>

extends Object
implements ChannelHandler
Class Inheritance
All Implemented Interfaces
io.netty5.channel.ChannelHandler
Type Parameters
<C>
A sub-type of Channel
Imports
io.netty5.bootstrap.Bootstrap, .ServerBootstrap, io.netty5.util.internal.logging.InternalLogger, .InternalLoggerFactory

A special ChannelHandler 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 isSharable() and so the implementation must be safe to be re-used.

Field Summary

Modifier and TypeField and Description
private static final InternalLogger

Constructor Summary

AccessConstructor and Description
public

Method Summary

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

Overrides default io.netty5.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
protected abstract void
initChannel(C
the Channel which was registered.
ch
)

This method will be called once the Channel was registered.

public boolean
isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

loggerback to summary
private static final InternalLogger logger

Constructor Detail

ChannelInitializerback to summary
public ChannelInitializer()

Method Detail

channelExceptionCaughtback to summary
public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelExceptionCaught.

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 default io.netty5.channel.ChannelHandler.handlerAdded.

Doc from io.netty5.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
@SuppressWarnings:unchecked
@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 channelExceptionCaught(ChannelHandlerContext, Throwable) which will by default close the Channel.
isSharableback to summary
public boolean isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Doc from io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline. By default, this method returns false. If this method returns false, you have to create a new handler instance every time you add it to a pipeline because it has unshared state such as member variables.

Annotations
@Override