I want to make so that each connection worked in the flow. I thought of making a separate ThreadPool , but when I initialized ServerBootstrap I already created 2 thread pools of the bossGroup and workGroup .

The Netty site says that the bossGroup accepts new connections, and the workGroup works with traffic. Maybe as it can be used to start the workGroup connection workGroup or is it better to create a separate pool of pots for this?

  • 2
    It’s like the whole NIO concept is breaking, there’s no point in making a separate stream for each connection. - Russtam
  • And then what can I do if I need to transfer and receive data from each connected client? Without the use of threads, I can not imagine how this can be done. - Bleser
  • one
    Enough workflow is enough to send / receive data. Next, in the server handler, process the received data, respond or start some work. - Russtam 5:56 pm
  • one
    Suppose a client requests a data connection from a database that is pulled out for a long time. Then you create a shared ExecutorService somewhere with several threads, when you receive a request in the server's handler, you create a Runnable / Callable transferring the Channel to it and upload it to the ExecutorService, everything is free, the workflows are also, and your task is completed in response. in the channel. - Russtam
  • So this is normal if I create the Connect class, pass the Channel to it and start Connect as a separate stream that will serve one client? - Bleser

1 answer 1

It turns out everything is much easier than I thought. When creating a server, we create two EventLoopGroup(bossGroup, workerGroup) .
For example:

 EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workGroup = new NioEventLoopGroup(5); 

When configuring ServerBootstrap you need to add event handlers that will do all the work of communicating with the client:

 bootServer.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception{ ch.pipeline().addLast(new ObjectEncoder()); ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(Pack.class.getClassLoader()))); ch.pipeline().addLast(new ConnectionWork()); } }); 

Since each new connection will call the initChannel method initChannel then everything we write in ConnectionWork will work separately for each connection, the number of connections is set in the workGroup constructor.