简体   繁体   中英

Can RabbitMQ (or spring cloud stream) consume messages exclusively?

I have a rabbitmq queue and two spring cloud spring consumers. I want that each consumers process messages in order.

I thought that when consumer1 send ack, consumer2 receive second message, so I expected message1, message2 is processed in order in each consumers.

-------------------- time pass ------------------------>

consumer1:   message1             message3
consumer2:              message2            message4

But it wasn't. consumer1, consumer2 receive message1, message2, and process simultaneously.

-------------------- time pass ------------------------>

consumer1:   message1  message3
consumer2:   message2  message4

Is there a way for spring cloud stream to consume messages exclusively?

RabbitMQ (AMQP) doesn't support that; each consumer gets prefetch messages.

It does support exclusive consumers, but it means consumer1 would get all the messages and consumer2 would only get messages if consumer1 dies.

However, Spring Cloud Stream doesn't currently provide a property to set that option.

you would have to model your queues in a different way. Eg by having an "incoming" queue which has exactly one consumer-coordinator. This consumer would relay messages to the "work" queue where consumer1+2 are both waiting and pick up work in a round robin way.

They would then signal completion to the coordinator on a third queue which would cause it to resume relaying a single message to the work queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM