Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ServerBootstrap.option() and ServerBootstrap.childOption() in netty 4.x

Tags:

java

netty

According to the doc New and noteworthy in 4.0, netty4 provided a new bootstrap API, and the doc gives the following code sample:

public static void main(String[] args) throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
         .channel(NioServerSocketChannel.class)
         .option(ChannelOption.SO_BACKLOG, 100)
         .localAddress(8080)
         .childOption(ChannelOption.TCP_NODELAY, true)
         .childHandler(new ChannelInitializer<SocketChannel>() {
             @Override
             public void initChannel(SocketChannel ch) throws Exception {
                 ch.pipeline().addLast(handler1, handler2, ...);
             }
         });

        // Start the server.
        ChannelFuture f = b.bind().sync();

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();

        // Wait until all threads are terminated.
        bossGroup.terminationFuture().sync();
        workerGroup.terminationFuture().sync();
    }
}

The code uses ServerBootStrap.option to set ChannelOption.SO_BACKLOG, and then uses ServerBootStrap.childOption to set ChannelOption.TCP_NODELAY.

What is the difference between ServerBootStrap.option and ServerBootStrap.childOption? How could I know which option should be an option and which should be a childOption ?

like image 419
Weibo Li Avatar asked Feb 19 '16 02:02

Weibo Li


1 Answers

What is the difference between ServerBootStrap.option and ServerBootStrap.childOption?

The parameters that we set using ServerBootStrap.option apply to the ChannelConfig of a newly created ServerChannel, i.e., the server socket which listens for and accepts the client connections. These options will be set on the Server Channel when bind() or connect() method is called. This channel is one per server.

And the ServerBootStrap.childOption applies to to a channel's channelConfig which gets created once the serverChannel accepts a client connection. This channel is per client (or per client socket).

So ServerBootStrap.option parameters apply to the server socket (Server channel) that is listening for connections and ServerBootStrap.childOption parameters apply to the socket that gets created once the connection is accepted by the server socket.

The same can be extended to attr vs childAttr and handler vs childHandler methods in the ServerBootstrap class.

How could I know which option should be an option and which should be a childOption ?

Which ChannelOptions are supported depends on the channel type we are using. You can refer to the API docs for the ChannelConfig that you’re using. http://netty.io/4.0/api/io/netty/channel/ChannelConfig.html and its sub classes. You should find Available Options section for each ChannelConfig.

like image 103
Madhusudana Reddy Sunnapu Avatar answered Sep 20 '22 11:09

Madhusudana Reddy Sunnapu