【问题标题】:Netty 4, use of ExecutorServiceNetty 4、ExecutorService的使用
【发布时间】: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


    【解决方案1】:

    在 netty 5 中,NioEventLoopGroup 带有一个以执行器为参数的构造函数:

    EventLoopGroup bossGroup = new NioEventLoopGroup(nThreads, yourExecutor);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      相关资源
      最近更新 更多