Reputation: 85
everybody. I test some simple example, but find that if I set worker thread pool size with fixed 2 when start a NIOServer, then try to connect netty server with 2 more tcp conection(connecting and not close), I find the 3rd and 3rd+ connection can connect the server, but the write operation be blocked, noting go into my handler. Only if i close 1st or 2nd connection, the 3rd write request can get into my handler, and my hander do nothing just print helloworld.
Main Code:
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newSingleThreadExecutor(),
Executors.newFixedThreadPool(2)));
bootstrap.setPipelineFactory(new DispatchServerPipeLineFactory());
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
bootstrap.bind(new InetSocketAddress(8080));
only one handler code:
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
System.out.println("hello");
super.messageReceived(ctx, e);
}
Can anyone help me? am I must use Executors.newCachedThreadPool()?
Upvotes: 0
Views: 400
Reputation: 3
Actually while using Netty you can assign the task of data processing to a ServerHandler. You require the NioEventLoopGroup to listen to client connections and EventExecutorGroup to perform the manipulations(receive the data and manage what to do with it). This way the incoming connections wont be blocked; but may be limited by the below snippet.
ServerBootstrap().childOption(ChannelOption.SO_BACKLOG, 10)
For more information you can visit this link: worker Group vs handler Thread in Netty
Upvotes: 0
Reputation: 23567
Use a Executors.newChachedThreadPool() and specify the worker cound in the constructor of NioServerSocketChannelFactory and everything will work.
Upvotes: 2
Reputation: 311050
You have set up a fixed thread pool with two threads such that all submitted jobs after the 2nd will wait until a thread becomes free. Then you are creating two connections, which consume those two threads. Then you are creating a third connection. So it waits. That's what you told it to do. If you don't want that, allocate more threads, or use a different thread pool, depending on what it is you actually want to happen, which you haven't told us.
Upvotes: 0