Search code examples
javaspringspring-amqpspring-rabbit

How to Prevent Spring AMQP from Blocking on Unacked Messages?


I have a @RabbitListener annotated method for which Spring AMQP blocks after returning from the method. The underlying SimpleRabbitListenerContainerFactory uses AcknowledgeMode.MANUAL. I don’t want to acknowledge the message in the listener method, yet.

Is there any way to not have Spring AMQP block in such a scenario?

In more detail

I use a listener like this:

@RabbitListener(queues = "#{ @myQueue }")
void recordRequestsFromMyMessages(
        @Payload MyMessage myMessagePayload,
        @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag,
        Channel channel) {

    // record relevant parts of the given message and combine them with
    // parts from previous/future messages
    // DON'T acknowledge the consumed message, yet; instead only keep a
    // record of the channel and the delivery tag
}

Since I batch/combine multiple messages before I actually process them (asynchronously) later, I don’t want to acknowledge the consumed message right away. Instead, I only want to do this once the messages have been successfully processed later.

With my current approach, Spring AMQP blocks after returning from calling the recordRequestsFromMyMessages method above and no further messages are consumed from the same queue anymore.

This SO answer suggests that batch processing should work, however, I’m not sure how.


Solution

  • It's not the container that's "blocking".

    You need to increase the prefetchCount on the container (default 1) - the broker only allows that number of unacked messages to be outstanding.