Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - Rabbit template - Bulk operation

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!

like image 783
italktothewind Avatar asked Oct 25 '25 14:10

italktothewind


1 Answers

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);
}
like image 171
Aniket Sahrawat Avatar answered Oct 27 '25 08:10

Aniket Sahrawat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!