Consume only N messages from RabbitMQ with react\stomp, ack them separately and then exit
Asked Answered
C

1

3

I am using RabbitMQ with PHP react\stomp. I have two queues - one is "todo" other is "done". Consumer reads from "todo", do its work, ACKs the message, then publishes it to the "done" queue.

Is there any way of ensuring that I consume only N messages from "todo" (and ack them individually) and then quit? The main reason for that is we dont want to have long running consumers and we want to restart them after N messages.

Colossian answered 23/2, 2015 at 11:16 Comment(0)
H
1

You can set a prefetch count for a destination:

The prefetch count for all subscriptions is set to unlimited by default. This can be controlled by setting the prefetch-count header on SUBSCRIBE frames to the desired integer count.

https://www.rabbitmq.com/stomp.html

So to consume only ten messages, add the header

prefetch-count:10

to the SUBSCRIBE frame.

You can set the ack mode to client-individual for message-by-message manual acknowledgement.

Hormone answered 1/3, 2015 at 15:15 Comment(1)
That is true, however I found out that prefetch message windows overlap..By the time consumer processes 10th message, there is already another window of 10 messages loaded. One solution is to set prefetch-count:1 and that simply means we can control consumer on per message basis and stop exactly after 10 consumed messages.Colossian

© 2022 - 2024 — McMap. All rights reserved.