Spring-Rabbit模板-批量操作

ita*_*ind 3 rabbitmq spring-rabbit

任何人都知道是否可以使用 Rabbit 模板将一组消息发送到队列?

显然我可以一次发送一个,但我想在单个批量操作中完成(以获得性能)。

谢谢!

Ani*_*wat 5

您可以创建一个 beanBatchingRabbitTemplate并使用它。这是一个工作示例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;
}
Run Code Online (Sandbox Code Playgroud)

现在你可以注入BatchingRabbitTemplate另一个 bean 并使用它:

@Bean
public ApplicationRunner runner(BatchingRabbitTemplate template) {
    MessageProperties props = //...
    return args -> template.send(new Message("Test").getBytes(), props);
}
Run Code Online (Sandbox Code Playgroud)