gaurav
gaurav

Reputation: 3

Using Netty4 with multiple clients

I am using Netty4 to create a Server which needs to serve multiple client connections. The ServerBootstrap is constructed with Parent and worker thread group. As per the documentation on ServerBootStrap.group() method it

"Set the EventLoopGroup for the parent (acceptor) and the child (client). These EventLoopGroup's are used to handle all the events and IO for SocketChannel and Channel's."

As I understand the ParentExecutor group would handle any incoming connections and pass it on to the Child Executor group to execute. So in order to server many clients I have the following setup

final ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup(Runtime.getRuntime()
            .availableProcessors() * 3))
    .channel(NioServerSocketChannel.class)
    .childHandler(new MyInitializer());

Now the question is , would the following method in my Handler be executed on the child executor group. I have a suspicion that it is handled in a single threaded manner via SingleThreadEventExecutor ?

protected void channelRead0(final ChannelHandlerContext ctx, final AppMessage msg)
        final AppMessage msg) throws Exception {

Upvotes: 0

Views: 1139

Answers (2)

neo
neo

Reputation: 350

The child group also known as worker group is actually a thread(called EventLoop in Netty) pool created by Netty. The default NioEventLoopGroup's pool size is Runtime.getRuntime().availableProcessors() * 2). Everytime, when a connection comes, Netty will arrange one thread(aka, EventLoop) from the child group to handle the channel and the message transportation and so on. All the handlers that belong to the ChannelPipeline will be executed in this thread.

Upvotes: 1

Igor Alelekov
Igor Alelekov

Reputation: 311

Yes, the handler will be executed in the child thread group during fireChannelRead() execution

Upvotes: 0

Related Questions