0

我启动了一个带有多个业务线程的 netty4 nio 服务器,用于处理如下的长期业务

public void start(int listenPort, final ExecutorService ignore)
        throws Exception {
            ...
    bossGroup = new NioEventLoopGroup();
    ioGroup = new NioEventLoopGroup();
    businessGroup = new DefaultEventExecutorGroup(businessThreads);

    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, ioGroup).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.TCP_NODELAY, 
                         Boolean.parseBoolean(System.getProperty(
                                 "nfs.rpc.tcp.nodelay", "true")))
            .childOption(ChannelOption.SO_REUSEADDR, 
                         Boolean.parseBoolean(System.getProperty(
                                 "nfs.rpc.tcp.reuseaddress", "true")))
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch)
                        throws Exception {
                    ch.pipeline().addLast("decoder",
                            new Netty4ProtocolDecoder());
                    ch.pipeline().addLast("encoder",
                            new Netty4ProtocolEncoder());
                    ch.pipeline().addLast(businessGroup, "handler",
                            new Netty4ServerHandler());
                }
            });

    b.bind(listenPort).sync();
    LOGGER.warn("Server started,listen at: " + listenPort + ", businessThreads is " + businessThreads);
}

我发现当服务器接受一个连接时只有一个线程在工作。如何引导一台服务器,它只能为一个连接启动多个业务线程?

谢谢, 敏斯

4

1 回答 1

2

Netty 将始终为一个连接使用相同的线程。这是设计使然。如果您想更改此设置,您可以实现自定义 EventExecutorGroup 并在将 ChannelHandler 添加到 ChannelPipeline 时将其传入。

请注意,这可能会导致数据包顺序混乱。

于 2013-07-30T07:04:43.827 回答