Netty 4, using ExecutorService

I would like to reuse ExecutorService in my application.

Reusing NioWorkerPool for multiple server and client bootstraps

I tried to reproduce the code laid out above using netty 4, but I did not find a way to do this, I also googled threw a lot, but it seems we can not provide ExecutorService for bootstrap or NioEventLoopGroup object.

For example, with netty 3, here you can share the executorservice service:

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, , -... EventLoop, ​​ NioEventLoopGroup, , . , , : , 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)
+4
1

netty 5, NioEventLoopGroup , :

EventLoopGroup bossGroup = new NioEventLoopGroup(nThreads, yourExecutor);

, netty 4

+4

Source: https://habr.com/ru/post/1526239/


All Articles