Multiple consumers on same channel; message consumption order? #732
-
Let's suppose I add consumer c1 processing queue q1 and consumer c2 processing q2 on the same channel (c1 declared before c2); the ack is manual and the prefetch count for c1 is pc1=5 while for c2 is pc2=8. Both queues are initially empty but at some same moment 2 parallel producers start sending each 800 messages at same constant sending rate, let's say a fast one. I'm trying to find, for a java client, what threads are used to make things work. q1) What thread is downloading the messages? How does it know how many messages to get? especially if many same queue consumers exist with different prefetch count (on different channels) q2) How the channel is dispatching the available messages to consumers? e.g. 3 messages available for c1 and 5 for c2 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
This belongs to the Java client repository then. You should not depend on any particular dispatch order of consumers, on a single channel or across multiple ones. It's like depending on a specific thread interleaving order. All inbound protocol threads are read off the socket using the general socket API or NIO. Each carries a channel ID (number), Channels dispatch all inbound protocol methods to a JDK execution service (you can provide your own) in a way that guarantees that they are dispatched in the same order as received. Obviously actual processing of different frames can take a different amount of time and resources, so we cannot assume that they also complete in that order. Consumers are looked up using delivery's consumer tag value and their handler methods are invoked in the aforementioned execution service context. Prefetch is a channel-level feature first and foremost, consumer-level prefetch is supported but rarely used. Regardless, this value is only used by RabbitMQ nodes to decide whether a delivery |
Beta Was this translation helpful? Give feedback.
-
In your example, the order of message consumption by two consumers with different prefetch values should be considered undefined. You have never stated the end goal but if it is to build a priority mechanism for consumers on top of prefetch, this is not going to |
Beta Was this translation helpful? Give feedback.
This belongs to the Java client repository then.
You should not depend on any particular dispatch order of consumers, on a single channel or across multiple ones. It's like depending on a specific thread interleaving order.
All inbound protocol threads are read off the socket using the general socket API or NIO. Each carries a channel ID (number),
so connections know what channel to dispatch to.
Channels dispatch all inbound protocol methods to a JDK execution service (you can provide your own) in a way that guarantees that they are dispatched in the same order as received. Obviously actual processing of different frames can take a different amount of time and resources, so we cannot assu…