[英]Netty 4 - Use different thread for send and receive
I am using Netty 4 in my project. 我在项目中使用Netty 4。 I found that it assigns a single thread to each channel(connection) and uses it for both receiving and sending data.
我发现它为每个通道(连接)分配了一个线程,并将其用于接收和发送数据。
In my application i get requests from channel, process them in different threads(my own threads) and then send responses back through the same channel. 在我的应用程序中,我从通道获取请求,在不同的线程(我自己的线程)中处理它们,然后通过同一通道将响应发送回去。 Because of using one thread for inbound and outbound, processing received packets waits when thread is sending packets.
由于使用一个线程进行入站和出站,因此当线程正在发送数据包时,处理接收到的数据包将等待。
Is there any way to use 2 thread for each channel, One for receive (inbound data processing) and one for send (outbound data processing)? 有什么方法可以为每个通道使用2个线程,一个用于接收(入站数据处理),一个用于发送(出站数据处理)?
With different EventExecutorGroup you can put send and receive handler logic into different threads, ie send handler in one EventExecutorGroup and write handler in another. 使用不同的EventExecutorGroup,您可以将发送和接收处理程序逻辑放入不同的线程中,即在一个EventExecutorGroup中发送发送处理程序,在另一个线程中写入处理程序。
But usually for sending is pretty fast and you don't need to use your own thread. 但是通常发送速度非常快,您无需使用自己的线程。 For receiving and the consecutive while lengthy processing, you probably will need to use your own thread by putting the logic into a DefaultEventExecutorGroup that provides a pool of threads to be used by all connected channels while giving one thread for one such channel only.
为了接收和进行连续而漫长的处理,您可能需要使用自己的线程,方法是将逻辑放入DefaultEventExecutorGroup中,该逻辑组提供供所有连接的通道使用的线程池,同时仅为一个此类通道提供一个线程。
Example code below: 下面的示例代码:
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.
我发现它仅使用一个线程进行读写,并且没有更改它的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.