1

我在我的项目中使用 Netty 4。我发现它为每个通道(连接)分配了一个线程,并将其用于接收和发送数据。
在我的应用程序中,我从通道获取请求,在不同的线程(我自己的线程)中处理它们,然后通过同一个通道发送回响应。因为入站和出站使用一个线程,所以在线程发送数据包时处理接收到的数据包会等待。
有没有办法为每个通道使用 2 个线程,一个用于接收(入站数据处理),一个用于发送(出站数据处理)?

4

2 回答 2

2

使用不同的 EventExecutorGroup,您可以将发送和接收处理程序逻辑放入不同的线程中,即在一个 EventExecutorGroup 中发送处理程序,在另一个中写入处理程序。

但通常发送速度非常快,您不需要使用自己的线程。对于接收和连续而冗长的处理,您可能需要通过将逻辑放入 DefaultEventExecutorGroup 来使用自己的线程,DefaultEventExecutorGroup 提供一个线程池供所有连接的通道使用,同时只为一个这样的通道提供一个线程。

下面的示例代码:

final EventExecutorGroup bizEventLoopGroup = new     DefaultEventExecutorGroup(50);

public void initChannel(Channel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();
//other code...

p.addLast(bizEventLoopGroup, LOGIN_HANDLER, new LoginHandler());
//LoginHandler is time-consuming since it will need to ensure sending back all previously cached message.
于 2016-05-18T07:19:04.687 回答
0

I found that it use only one thread for read and write, and there is no method for changing it.

于 2014-09-14T13:40:10.483 回答