Search code examples
javarabbitmqamqpspring-rabbitspring-amqp

RabbitMQ basic.get and acknowledgement


I'm invoking:

GetResponse response = channel.basicGet("some.queue", false); // no auto-ack
....
channel.basicAck(deliveryTag, ...);

However, when I invoke basicGet, the messages in the queue stay in "Ready", rather than in "Unacknowledged". I want them to be in unacknowledged, so that I can either basic.ack them (thus discarding them from the queue), or basic.nack them


Solution

  • When doing ack immediately after the get it works fine. However, in my case, they were separated by a request. And spring's template closes the channel and connection on each execution. So there are three options:

    • keep one channel and connection open throughout the whole lifetime of the application
    • have some kind of conversation-scope (or worst-case: use the session) to store the same channel and reuse it.
    • use one channel per request, acknowledge receipt immediately, and store the messages in memory.

    In the former two cases you can't do it with spring's RabbitTemplate