Netty中的工作组与处理程序线程 [英] worker Group vs handler Thread in Netty

查看:284
本文介绍了Netty中的工作组与处理程序线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个Netty Server来接受多个传入的客户端连接,这些连接反过来会进行一些处理,但是对于Wroker线程组与Handler线程感到困惑

I am setting up a Netty Server to accept multiple incoming client connections which will in turn do some processing, but confused about the wroker thread group vs Handler threads

我尝试如下分配10个工作线程和20个处理程序线程.

I have tried assigning 10 worker threads and 20 handler threads as below.

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(10);
EventExecutorGroup handlerThread = new DefaultEventExecutorGroup(20);

try {
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossGroup, workerGroup);
    serverBootstrap.channel(NioServerSocketChannel.class);
    serverBootstrap.localAddress(new 
    InetSocketAddress(hostName,Integer.parseInt(port)));

    // initialize a new child handler for incoming request
    logger.debug("Incoming request from TCP client...assigning a new Server Handler");
    serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
    protected void initChannel(SocketChannel socketChannel) throws Exception 
    {
    socketChannel.pipeline().addLast(handlerThread,new NettyServerHandler());
                }
            });
            ChannelFuture channelFuture = serverBootstrap.bind().sync();
            channelFuture.channel().closeFuture().sync();

        } catch (Exception e) {
            logger.error("Unable to initialize TCP Server");
        } 

我无法理解工作组的任务与创建新服务器处理程序之间的区别.据我了解,处理程序线程池将分配给 NettyServerHandler 的每个实例.但是,创建10个线程的工作组池的作用是什么?

I cannot understand the difference between worker group's task and creating a new server handler. As per my understanding, the handler thread pool will be assigned to each instance of NettyServerHandler. But then what is the role of creating worker group pool of 10 threads?

推荐答案

您不必使用具有10个线程的EventLoopGroup来接受10个客户端.它足以在bossGroup上传递一个线程,而在workerGroup上传递一个线程:

You don't have to make an EventLoopGroup with 10 threads for accepting 10 clients. Its enough to pass one thread on the bossGroup and none on the workerGroup:

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();

确保ServerBootstrap().childOption(ChannelOption.SO_BACKLOG, 10); 设置为至少10个,以接受10个客户.

Make sure the ServerBootstrap().childOption(ChannelOption.SO_BACKLOG, 10); is set to at least 10 for accepting 10 clients.

据我所知,EventLoopGroup的作用是接受客户. 另一方面,ServerHandler可以接收数据并管理该数据,并且可以通过覆盖ChannelHandlerAdapter方法来拦截是否有人加入或连接有问题.

So the role of the EventLoopGroup is accepting the clients as far as i know. The ServerHandler on the other hand is there to receive the data and manage what to do with it, and you can intercept if someone joined or if there is any problem with the connection by overriding the ChannelHandlerAdapter methods.

希望我能理解您的问题并帮助您理解它,

I hope i understood your problem and helped you to understand it,

亲切的问候

这篇关于Netty中的工作组与处理程序线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆