【发布时间】:2014-02-10 20:55:21
【问题描述】:
我想在我的应用程序中重用一个 ExecutorService。
Reusing a NioWorkerPool across multiple server and client bootstraps
我尝试使用 netty 4 重现上面发布的代码,但我没有找到方法,我也搜索了很多退出但似乎我们无法将 ExecutorService 提供给引导程序或 NioEventLoopGroup 对象。
例如使用 netty 3,您可以共享 executorservice:
ExecutorService executor = Executors.newCachedThreadPool();
NioClientBossPool clientBossPool = new NioClientBossPool(executor, clientBossCount);
NioServerBossPool serverBossPool = new NioServerBossPool(executor, serverBossCount);
NioWorkerPool workerPool = new NioWorkerPool(executor, workerCount);
ChannelFactory cscf = new NioClientSocketChannelFactory(clientBossPool, workerPool);
ChannelFactory sscf = new NioServerSocketChannelFactory(serverBossPool, workerPool);
...
ClientBootstrap cb = new ClientBootstrap(cscf);
ServerBootstrap sb = new ServerBootstrap(sscf);
但是对于 netty 4,据我所知你不能使用 executorservice ... 你必须提供一个像 NioEventLoopGroup 这样的 EventLoop 实现,但我真的很想 使用我将在我的应用程序中使用的通用 executorService。因为我希望在一个线程池中让线程做不同类型的工作:计算、带有 netty 的网络 ...
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup()
ServerBootstrap b = new ServerBootstrap(); // (2)
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new DiscardServerHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
【问题讨论】:
标签: java netty nio executorservice