Temporarily stop consuming RabbitMQ messages and resume later



I use Java’s rabbitmq-client (https://mvnrepository.com/artifact/com.rabbitmq/amqp-client) and I need to implement the following scenario:

  • While receiving Rabbit messages, I may need to pause Rabbitmq consumption from particular queues if I suspect that all awaiting data will not fit in memory.
  • After I processed some messages, I need to open consumption again for the following set of messages.
  • Repeat as needed.

What would be the best way to implement pause/resume of listening from a RabbitMQ queue using the amqp-client Java library?

Answer

You don’t mention which method you’re using to consume messages, so I assume you are using basicConsume to subscribe to messages from a queue.

As that document mentions, you can use basicCancel to stop consuming from a queue. You would then use basicConsume when you wish to start again.

Be sure to use basicQos to set a reasonable prefetch count.



Source: stackoverflow