Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set prefetch count on @RabbitListener

I know it is possible to make SimpleMessageListenerContainer bean and set prefetch count and message listener here, like this:

@Bean
public SimpleMessageListenerContainer messageListenerContainer(
        ConnectionFactory rabbitConnectionFactory,
        Receiver receiver) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(rabbitConnectionFactory);
    container.setQueueNames("hello");
    container.setMessageListener(new MessageListenerAdapter(receiver, "receive"));
    container.setPrefetchCount(1000);
    return container;
}

But how to set prefetch count for channel if I want to use declarative approach using @RabbitListener?

@Component
public class Receiver {

    private static final Logger log = LoggerFactory.getLogger(Receiver.class);

    @RabbitListener(queues = "hello") // how to set prefetch count here?
    public void receive(String message) {
        log.info(" [x] Received '{}'.", message);
    }

}

It is not possible?

like image 702
Ruslan Stelmachenko Avatar asked Jun 01 '16 00:06

Ruslan Stelmachenko


People also ask

How do you change the prefetch count in RabbitMQ?

RabbitMQ allows you to set either a channel or consumer count using this method. The basic_qos function contains the global flag. Setting the value to false applies the count to each new consumer. Setting the value to true applies a channel prefetch count to all consumers.

Where is prefetch count in RabbitMQ?

The default value of prefetch count is 20. You can set the prefetch count for the RabbitMQ broker through the system property.

What is QOS in RabbitMQ?

qos multiple times with different global values. RabbitMQ interprets this as meaning that the two prefetch limits should be enforced independently of each other; consumers will only receive new messages when neither limit on unacknowledged messages has been reached.

What is Spring AMQP?

The Spring AMQP project applies core Spring concepts to the development of AMQP-based messaging solutions. It provides a "template" as a high-level abstraction for sending and receiving messages. It also provides support for Message-driven POJOs with a "listener container".


1 Answers

Solution according to @artem-bilan answer:

Declare RabbitListenerContainerFactory bean with prefetch count 10 in some @Configuration class:

@Bean
public RabbitListenerContainerFactory<SimpleMessageListenerContainer> prefetchTenRabbitListenerContainerFactory(ConnectionFactory rabbitConnectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(rabbitConnectionFactory);
    factory.setPrefetchCount(10);
    return factory;
}

Receiver bean uses this factory bean:

@Component
public class Receiver {

    private static final Logger log = LoggerFactory.getLogger(Receiver.class);

    @RabbitListener(queues = "hello", containerFactory = "prefetchTenRabbitListenerContainerFactory")
    public void receive(String message) {
        log.info(" [x] Received '{}'.", message);
    }

    @RabbitListener(queues = "hello")
    public void receiveWithoutPrefetch(String message) {
        log.info(" [x] Received without prefetch '{}'.", message);
    }

}

Two listeners here is just for demo purpose.
With this configuration Spring creates two AMQP channels. One for each @RabbitListener. First with prefetch count 10 using our new prefetchTenRabbitListenerContainerFactory bean and second with prefetch count 1 using default rabbitListenerContainerFactory bean.

like image 198
Ruslan Stelmachenko Avatar answered Nov 14 '22 09:11

Ruslan Stelmachenko