Home / Class/ ChannelInitializer Class — netty Architecture

ChannelInitializer Class — netty Architecture

Architecture documentation for the ChannelInitializer class in ChannelInitializer.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  e8b02344_55ca_b96d_1e76_3f1acf3d80c6["ChannelInitializer"]
  c00a14a0_ec82_9c9d_0966_c7844fc0f23d["ChannelInitializer.java"]
  e8b02344_55ca_b96d_1e76_3f1acf3d80c6 -->|defined in| c00a14a0_ec82_9c9d_0966_c7844fc0f23d
  5a672898_8ee2_8a02_cb4a_41c51216bcba["initChannel()"]
  e8b02344_55ca_b96d_1e76_3f1acf3d80c6 -->|method| 5a672898_8ee2_8a02_cb4a_41c51216bcba
  b1427cb1_eeae_cdf2_246b_5209d53f13ed["channelRegistered()"]
  e8b02344_55ca_b96d_1e76_3f1acf3d80c6 -->|method| b1427cb1_eeae_cdf2_246b_5209d53f13ed
  86816443_aade_eece_19a5_02b33154fb7a["exceptionCaught()"]
  e8b02344_55ca_b96d_1e76_3f1acf3d80c6 -->|method| 86816443_aade_eece_19a5_02b33154fb7a
  8660b724_b032_c48f_e8c9_33c1b4542ffe["handlerAdded()"]
  e8b02344_55ca_b96d_1e76_3f1acf3d80c6 -->|method| 8660b724_b032_c48f_e8c9_33c1b4542ffe
  d0c35d7a_0f58_f00c_cfe6_bf0e3f1818e0["handlerRemoved()"]
  e8b02344_55ca_b96d_1e76_3f1acf3d80c6 -->|method| d0c35d7a_0f58_f00c_cfe6_bf0e3f1818e0
  981476f2_0013_63bb_3526_de2c469ebf83["removeState()"]
  e8b02344_55ca_b96d_1e76_3f1acf3d80c6 -->|method| 981476f2_0013_63bb_3526_de2c469ebf83

Relationship Graph

Source Code

transport/src/main/java/io/netty/channel/ChannelInitializer.java lines 53–158

@Sharable
public abstract class ChannelInitializer<C extends Channel> extends ChannelInboundHandlerAdapter {

    private static final InternalLogger logger = InternalLoggerFactory.getInstance(ChannelInitializer.class);
    // We use a Set as a ChannelInitializer is usually shared between all Channels in a Bootstrap /
    // ServerBootstrap. This way we can reduce the memory usage compared to use Attributes.
    private final Set<ChannelHandlerContext> initMap = ConcurrentHashMap.newKeySet();

    /**
     * This method will be called once the {@link Channel} was registered. After the method returns this instance
     * will be removed from the {@link ChannelPipeline} of the {@link Channel}.
     *
     * @param ch            the {@link Channel} which was registered.
     * @throws Exception    is thrown if an error occurs. In that case it will be handled by
     *                      {@link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default close
     *                      the {@link Channel}.
     */
    protected abstract void initChannel(C ch) throws Exception;

    @Override
    @SuppressWarnings("unchecked")
    public final void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        // Normally this method will never be called as handlerAdded(...) should call initChannel(...) and remove
        // the handler.
        if (initChannel(ctx)) {
            // we called initChannel(...) so we need to call now pipeline.fireChannelRegistered() to ensure we not
            // miss an event.
            ctx.pipeline().fireChannelRegistered();

            // We are done with init the Channel, removing all the state for the Channel now.
            removeState(ctx);
        } else {
            // Called initChannel(...) before which is the expected behavior, so just forward the event.
            ctx.fireChannelRegistered();
        }
    }

    /**
     * Handle the {@link Throwable} by logging and closing the {@link Channel}. Sub-classes may override this.
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        if (logger.isWarnEnabled()) {
            logger.warn("Failed to initialize a channel. Closing: " + ctx.channel(), cause);
        }
        ctx.close();
    }

    /**
     * {@inheritDoc} If override this method ensure you call super!
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        if (ctx.channel().isRegistered()) {
            // This should always be true with our current DefaultChannelPipeline implementation.
            // The good thing about calling initChannel(...) in handlerAdded(...) is that there will be no ordering
            // surprises if a ChannelInitializer will add another ChannelInitializer. This is as all handlers
            // will be added in the expected order.
            if (initChannel(ctx)) {

                // We are done with init the Channel, removing the initializer now.
                removeState(ctx);
            }
        }
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        initMap.remove(ctx);
    }

    @SuppressWarnings("unchecked")
    private boolean initChannel(ChannelHandlerContext ctx) throws Exception {
        if (initMap.add(ctx)) { // Guard against re-entrance.
            try {
                initChannel((C) ctx.channel());
            } catch (Throwable cause) {
                // Explicitly call exceptionCaught(...) as we removed the handler before calling initChannel(...).
                // We do so to prevent multiple calls to initChannel(...).
                exceptionCaught(ctx, cause);
            } finally {

Frequently Asked Questions

What is the ChannelInitializer class?
ChannelInitializer is a class in the netty codebase, defined in transport/src/main/java/io/netty/channel/ChannelInitializer.java.
Where is ChannelInitializer defined?
ChannelInitializer is defined in transport/src/main/java/io/netty/channel/ChannelInitializer.java at line 53.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free