【问题标题】:Using Netty4 with multiple clients将 Netty4 与多个客户端一起使用
【发布时间】:2013-10-29 10:48:02
【问题描述】:

我正在使用 Netty4 创建一个需要为多个客户端连接提供服务的服务器。 ServerBootstrap 由 Parent 和工作线程组构成。根据 ServerBootStrap.group() 方法的文档

“为父(接受者)和子(客户端)设置EventLoopGroup。这些EventLoopGroup用于处理SocketChannel和Channel的所有事件和IO。”

据我了解,ParentExecutor 组将处理任何传入连接并将其传递给 Child Executor 组以执行。所以为了服务许多客户,我有以下设置

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

现在的问题是,我的 Handler 中的以下方法是否会在子 executor 组上执行。我怀疑它是通过 SingleThreadEventExecutor 以单线程方式处理的?

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

【问题讨论】:

    标签: netty


    【解决方案1】:

    子组也称为工作组实际上是由Netty创建的一个线程(在Netty中称为EventLoop)池。默认 NioEventLoopGroup 的池大小为Runtime.getRuntime().availableProcessors() * 2)。每次连接到来时,Netty 都会从子组中安排一个线程(又名 EventLoop)来处理通道和消息传输等。所有属于 ChannelPipeline 的处理程序都会在这个线程中执行。

    【讨论】:

      【解决方案2】:

      是的,在fireChannelRead()执行过程中,handler会在子线程组中执行

      【讨论】:

        猜你喜欢
        • 2012-10-16
        • 2013-06-18
        • 2010-12-24
        • 2021-01-21
        • 1970-01-01
        • 2014-01-14
        • 2013-01-19
        • 2017-12-15
        • 2015-11-14
        相关资源
        最近更新 更多