Java Netty 4,执行器服务的使用
我想在我的应用程序中重用ExecutorService 我试图用netty 4复制上面发布的代码,但我没有找到方法,我也在谷歌上退出了很多次,但似乎我们无法为引导或NioEventLoopGroup对象提供ExecutorService 以netty 3为例,以下是您如何共享Executor服务:Java Netty 4,执行器服务的使用,java,netty,nio,executorservice,Java,Netty,Nio,Executorservice,我想在我的应用程序中重用ExecutorService 我试图用netty 4复制上面发布的代码,但我没有找到方法,我也在谷歌上退出了很多次,但似乎我们无法为引导或NioEventLoopGroup对象提供ExecutorService 以netty 3为例,以下是您如何共享Executor服务: ExecutorService executor = Executors.newCachedThreadPool(); NioClientBossPool clientBossPool = new
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,据我所知你不能使用Executor服务。。。
您必须提供像NioEventLoopGroup这样的EventLoop实现,但我确实希望
使用我将在整个应用程序中使用的通用Executor服务。因为我想在一个线程池中让线程做不同类型的工作:计算、网络和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)
EventLoopGroup bossGroup=new NioEventLoopGroup();//(1)
EventLoopGroup workerGroup=新的NioEventLoopGroup()
ServerBootstrap b=新的ServerBootstrap();//(2)
b、 组(bossGroup、workerGroup)
.channel(NioServerSocketChannel.class)//(3)
.childHandler(新的ChannelInitializer(){//(4)
@凌驾
public void initChannel(SocketChannel ch)引发异常{
ch.pipeline().addLast(新的丢弃服务器处理程序());
}
})
.选项(ChannelOption.SO_BACKLOG,128)/(5)
.childOption(ChannelOption.SO_KEEPALIVE,true);//(6)
在netty 5中,NioEventLoopGroup附带了一个构造函数,该构造函数将执行器作为参数:
EventLoopGroup bossGroup = new NioEventLoopGroup(nThreads, yourExecutor);
但我不确定netty 4中是否存在这种情况