NOTE

2. Bootstrap Creation

1. Code to analyze 2. Create ServerBootstrap 3. Set ServerBootstrap properties 4. ReflectiveChannelFactory

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Code to Analyze

ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup,workerGroup)
        .channel(NioServerSocketChannel.class)
        .option(ChannelOption.SO_BACKLOG, 100)
        .handler(new LoggingHandler(LogLevel.INFO))
        .childHandler(new ChannelInitializer<SocketChannel>()
        {
            @Override
            protected void initChannel(SocketChannel channel) throws Exception
            {
                ChannelPipeline pipeline = channel.pipeline();
                pipeline.addLast(new LoggingHandler(LogLevel.INFO));
            }
        });

2. Create ServerBootstrap

There is not much to say about creating ServerBootstrap; it simply calls the constructor.

public ServerBootstrap() { }

3. Set ServerBootstrap Properties

Next, set its properties, including channel, handler, childHandler, option, and so on.

The most important one is the channel setting:

.channel(NioServerSocketChannel.class)

3.1. Set the Factory That Creates Channels

public B channel(Class<? extends C> channelClass) {
    // The channelClass passed by the server is NioServerSocketChannel.
    if (channelClass == null) {
        throw new NullPointerException("channelClass");
    }
    // Follow this call downward.
    return channelFactory(new ReflectiveChannelFactory<C>(channelClass));
}

public B channelFactory(ChannelFactory<? extends C> channelFactory) {
    if (channelFactory == null) {
        throw new NullPointerException("channelFactory");
    }
    if (this.channelFactory != null) {
        throw new IllegalStateException("channelFactory set already");
    }
    // Set channelFactory.
    this.channelFactory = channelFactory;
    return self();
}

3.2. Save NioServerSocketChannel in ReflectiveChannelFactory

As shown above, setting the channel only creates a ReflectiveChannelFactory and stores NioServerSocketChannel in that ReflectiveChannelFactory.

This ReflectiveChannelFactory is the factory used to create channels.

4. ReflectiveChannelFactory

public class ReflectiveChannelFactory<T extends Channel> implements ChannelFactory<T> {

    private final Class<? extends T> clazz;

    public ReflectiveChannelFactory(Class<? extends T> clazz) {
        if (clazz == null) {
            throw new NullPointerException("clazz");
        }
        this.clazz = clazz;
    }

    @Override
    public T newChannel() {
        try {
            return clazz.getConstructor().newInstance();// Use reflection to invoke the Channel class constructor.
        } catch (Throwable t) {
            throw new ChannelException("Unable to create Channel from class " + clazz, t);
        }
    }

The newChannel method invokes the constructor of the Channel class through reflection, thereby creating a channel. This method appears again in the later analysis.

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub