【发布时间】:2016-02-19 02:39:00
【问题描述】:
根据文档New and noteworthy in 4.0,netty4提供了新的引导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.option 和 ServerBootStrap.childOption 有什么区别?我怎么知道哪个选项应该是option,哪个应该是childOption?
【问题讨论】: