gpt4 book ai didi

java - netty 4.x 中的 ServerBootstrap.option() 和 ServerBootstrap.childOption() 有什么区别

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:48:14 26 4
gpt4 key购买 nike

根据文档 New and noteworthy in 4.0 ,netty4提供了一个新的bootstrap API,文档给出了如下代码示例:

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.option设置ChannelOption.SO_BACKLOG,然后使用ServerBootStrap.childOption设置ChannelOption.TCP_NODELAY

ServerBootStrap.optionServerBootStrap.childOption 有什么区别?我怎么知道哪个选项应该是 option 哪个应该是 childOption

最佳答案

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

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

并且 ServerBootStrap.childOption 适用于 channel 的 channelConfig,它在 serverChannel 接受客户端连接后创建。此 channel 针对每个客户端(或每个客户端套接字)。

因此 ServerBootStrap.option 参数适用于正在监听连接的服务器套接字(服务器 channel ),而 ServerBootStrap.childOption 参数适用于在连接成功后创建的套接字服务器套接字接受连接。

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

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

支持哪些 ChannelOptions 取决于我们使用的 channel 类型。您可以引用您正在使用的 ChannelConfig 的 API 文档。 http://netty.io/4.0/api/io/netty/channel/ChannelConfig.html及其子类。您应该找到每个 ChannelConfig 的 Available Options 部分。

关于java - netty 4.x 中的 ServerBootstrap.option() 和 ServerBootstrap.childOption() 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35496345/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com