简体   繁体   English

Netty 4-使用不同的线程进行发送和接收

[英]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.

相关问题 在Netty中发送和接收不同类型的对象 - Send and receive different types of object in netty 将Netty通道传递到队列并在以后用于其他线程上的写入是否有效? - Is it valid to pass netty channels to a queue and use it for writes on a different thread later on? 为什么使用netty循环writeAndFlush发送DatagramPacket必须将线程休眠一会儿? - why use netty loop writeAndFlush to send DatagramPacket must sleep the thread for a while? Netty ChannelHandlerContext从不同线程写入 - Netty ChannelHandlerContext writing from different thread Netty TCP示例:如何发送/接收对象? - Netty TCP example: how do I send/receive objects? 如何使用 reactor-netty TcpClient 链接多个发送和接收操作 - How to chain multiple send and receive operations with reactor-netty TcpClient (Java / netty)使用Netty发送和接收可变大小对象的最简单方法是什么? - (Java/netty) What's the easiest way to send and receive variable-size objects with Netty? 我应该使用多少个线程来通过java.nio发送和接收数据? - How many thread should I use to send and receive data with java.nio? 当我们使用自己的线程池时,netty可以安全吗? 如果可以,为什么? - When we use our own thread pool, Can netty be thread safe? if netty can, Why? 如何在不同端口上运行的特定Netty Client实例上发送数据 - How to send data on a particular Netty Client instance running on different port
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM