Netty 4, use of ExecutorService

executorservice, java, netty, nio

Solution

in netty 5, NioEventLoopGroup comes with a constructor which takes a executor as parameter:

EventLoopGroup bossGroup = new NioEventLoopGroup(nThreads, yourExecutor);

but I am not sure if this is the case in netty 4

Problem

I would like to reuse an ExecutorService across my application. Reusing a NioWorkerPool across multiple server and client bootstraps I tried to reproduce the code posted above with netty 4 but i didn't find a way to do it, i also googled quit a lot but it seems we can't provide ExecutorService to a bootstrap or NioEventLoopGroup object. For example with netty 3 here is how you can share 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); ``` But with netty 4, as far as i know you can't use a executorservice ... You must provide a EventLoop implementation like NioEventLoopGroup, but i really would like to use a generic executorService that i will use across my application. Because i want in one thread-pool to have threads doing different kind of work : computation, networks with 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) ```

Original source