Anyone knows if it is possible to send a collection of messages to a queue using Rabbit template?
Obviously I can send them one at a time, but I want to do it in a single bulk operation (to gain performance).
Thanks!
You can create a bean of BatchingRabbitTemplate and use it. Here is a working example bean:
@Bean
public BatchingRabbitTemplate batchingRabbitTemplate(ConnectionFactory connectionFactory) {
    BatchingStrategy strategy = new SimpleBatchingStrategy(500, 25_000, 3_000);
    TaskScheduler scheduler = new ConcurrentTaskScheduler();
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(strategy, scheduler);
    template.setConnectionFactory(connectionFactory);
    // ... other settings
    return template;
}
Now you can inject BatchingRabbitTemplate in another bean and use it:
@Bean
public ApplicationRunner runner(BatchingRabbitTemplate template) {
    MessageProperties props = //...
    return args -> template.send(new Message("Test").getBytes(), props);
}
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