Netty 4,使用ExecutorService

use*_*951 6 java nio executorservice netty

我想在我的应用程序中重用ExecutorService.

跨多个服务器和客户端引导重用NioWorkerPool

我尝试用netty 4重现上面发布的代码,但我没有找到办法,我也谷歌搜索了很多,但似乎我们不能提供ExecutorService到bootstrap或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);
Run Code Online (Sandbox Code Playgroud)

但是对于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)
Run Code Online (Sandbox Code Playgroud)

Sep*_*tem 5

在netty 5中,NioEventLoopGroup带有一个构造函数,该构造函数将执行程序作为参数:

EventLoopGroup bossGroup = new NioEventLoopGroup(nThreads, yourExecutor);
Run Code Online (Sandbox Code Playgroud)

但是我不确定netty 4是否是这种情况