Search code examples
javaspringamqpspring-amqp

Rabbit prefetch


I use spring amqp with rabbitmq. I want get one message without prefetch. I configured with

      SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(rabbitConnectionFactory());
    container.setQueueNames(
            ProjectConfigs.getInstance().get_RABBIT_TASK_QUEUE()
    );
    container.setMessageListener(taskListener());
    container.setConcurrentConsumers(1);
    container.setPrefetchCount(1);
    container.setTxSize(1);
    return container;

How to disable prefetch and get only one message/


Solution

  • prefetch simply controls how many messsages the broker allows to be outstanding at the consumer at a time. When set to 1, this means the broker will send 1 message, wait for the ack, then send the next.

    It defaults to 1. Setting it to 0 will mean the broker will send unlimited messages to the consumer, regardless of acks.

    If you only want one message and then stop, you shouldn't use a container, you can use one of the RabbitTemplate.receive() methods.