我想在我的应用程序中重用ExecutorService。
Reusing a NioWorkerPool across multiple server and client bootstraps
我试图用netty 4重现上面发布的代码,但我没有找到这样做的方法,我也谷歌了很多次quit,但似乎我们不能为bootstrap或NioEventLoopGroup对象提供ExecutorService。
以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中,据我所知你不能使用一个执行器服务...您必须提供一个像NioEventLoopGroup这样的EventLoop实现,但我真的想使用一个通用的executorService,我将在我的应用程序中使用它。因为我想在一个线程池中让线程做不同种类的工作:计算,网络和网络...
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)发布于 2014-02-12 10:55:35
在netty 5中,NioEventLoopGroup附带了一个构造函数,该构造函数接受一个executor作为参数:
EventLoopGroup bossGroup = new NioEventLoopGroup(nThreads, yourExecutor);但是我不确定在netty 4中是不是这样。
https://stackoverflow.com/questions/21687708
复制相似问题