0

I'm trying to gracefully shutdown a process that is consuming messages off RabbitMQ. I know that I can use Channel.cancel to stop RabbitMQ from sending any new messages to the process, but I need to deal with any pending, unacknowledged messages as well. I could just call Channel.nackAll and re-queue all of them, but it would be better if I could wait till all of the pending messages - messages that were consumed, but not yet acked/nacked - were done.

Any ideas how to achieve this?

Fabis
  • 1,932
  • 2
  • 20
  • 37

1 Answers1

0
  • Cancel the consumer
  • Ack the messages you are currently "working" on when the work is done
  • When the above is done, close channels and connections. Anything that is un-acked will be re-enqueued by RabbitMQ.

NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.

Luke Bakken
  • 8,993
  • 2
  • 20
  • 33
  • How would I know that all pending work is done? Is there some way to hook into when that happens? – Fabis Mar 15 '20 at 19:22
  • Your application is doing work with each message. That is what I mean by "pending work". – Luke Bakken Mar 16 '20 at 16:20
  • Yes, but how would I be able to tell when all pending work is done? AFAIK there is no "get count of currently consumed, but not yet acked messages" method in RabbitMQ. I could track incoming/processed messages manually in code, but it seems like it could be a bit brittle (one of the messages gets skipped for some reason and the count is off for the entire duration of the process). – Fabis Mar 17 '20 at 08:54
  • It is up to your application to keep track. You should be setting a QoS / prefetch value so that you know the maximum count of un-acked messages RabbitMQ will allow on your consumer's channel. – Luke Bakken Mar 17 '20 at 15:20
  • I guess that answers my question - RabbitMQ offers no way to get (at least the count of) currently received but not yet acked/nacked messages. – Fabis Mar 18 '20 at 06:30