netty 4.x中ServerBootstrap.option()和ServerBootstrap.childOption()之间有什么区别
根据4.0中的文档New和值得注意的 ,netty4提供了一个新的bootstrap API,doc提供了以下代码示例:
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() { @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.option
和ServerBootStrap.childOption
什么ServerBootStrap.childOption
? 我怎么知道哪个选项应该是一个option
,哪个应该是childOption
?
ServerBootStrap.option和ServerBootStrap.childOption有什么区别?
我们使用ServerBootStrap.option
设置的参数适用于新创建的ServerChannel的ChannelConfig,即侦听并接受客户端连接的服务器套接字。 调用bind()或connect()方法时,将在服务器通道上设置这些选项。 此通道是每个服务器一个。
ServerBootStrap.childOption
适用于通道的channelConfig,一旦serverChannel接受客户端连接,它就会被创建。 此通道是每个客户端(或每个客户端套接字)。
因此, ServerBootStrap.option
参数适用于正在侦听连接的服务器套接字(服务器通道), ServerBootStrap.childOption
参数适用于在服务器套接字接受连接后创建的套接字。
同样可以扩展到ServerBootstrap
类中的attr
vs childAttr
和handler
vs childHandler
方法。
我怎么知道哪个选项应该是一个选项,哪个应该是childOption?
支持哪种ChannelOptions取决于我们使用的渠道类型。 您可以参考您正在使用的ChannelConfig的API文档。 http://netty.io/4.0/api/io/netty/channel/ChannelConfig.html及其子类。 您应该为每个ChannelConfig找到“ 可用选项”部分。
换句话说, ServerBootStrap.option
与bossGroup ServerBootStrap.childOption
使用, ServerBootStrap.childOption
与workerGroup ServerBootStrap.childOption
使用。