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?
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.
The default value of prefetch count is 20. You can set the prefetch count for the RabbitMQ broker through the system property.
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.
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With