本文介绍了netty 4.x中ServerBootstrap.option()和ServerBootstrap.childOption()之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,netty4提供了一个新的bootstrap API,doc提供了以下代码示例:

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();
    }
}

代码使用 ServerBootStrap。选项设置 ChannelOption.SO_BACKLOG ,然后使用 ServerBootStrap.childOption 设置 ChannelOption.TCP_NODELAY

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

ServerBootStrap.option 和 ServerBootStrap.childOption ?我怎么知道哪个选项应该是选项哪个应该是 childOption

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 ?

推荐答案

我们使用 ServerBootStrap.option 设置的参数适用于新创建的ServerChannel的ChannelConfig,即侦听并接受客户端连接的服务器套接字。调用bind()或connect()方法时,将在服务器通道上设置这些选项。此通道是每个服务器一个。

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.

ServerBootStrap.childOption 适用于通道的channelConfig,该通道创建一次serverChannel接受客户端连接。此通道是每个客户端(或每个客户端套接字)。

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).

所以 ServerBootStrap.option 参数适用于服务器套接字正在侦听连接的(服务器通道)和 ServerBootStrap.childOption 参数适用于服务器套接字接受连接后创建的套接字。

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.

同样可以扩展到 attr vs childAttr handler vs childHandler ServerBootstrap 类中的方法。

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

支持哪种ChannelOptions取决于我们使用的渠道类型。
您可以参考您正在使用的ChannelConfig的API文档。 及其子类。您应该为每个ChannelConfig找到可用选项部分。

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.

这篇关于netty 4.x中ServerBootstrap.option()和ServerBootstrap.childOption()之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:58