SpringBoot integrates RabbitMQ
Starting from version 3.6.0 of RabbitMQ, the concept of Lazy Queues, that is, lazy queues, has been added.
The characteristics of lazy queue are as follows:
- After receiving the message, it is directly stored on disk instead of memory;
- Consumers read from disk and load into memory when they want to consume messages;
- Support millions of message storage;
1. How to define lazy queue
To set a queue as a lazy queue, you only need to specify the x-queue-mode attribute as lazy when declaring the queue.
Lazy queues can be defined through command line, @Bean, and annotation. Here we only talk about @Bean and @RabbitListener.
1. @Bean way to define lazy queue (recommended this way)
@Bean
public Queue lazyQueue(){
return QueueBuilder.durable("lazy.queue")
.lazy() //设置为惰性队列
.build();
}
@Bean
public Queue lazyQueue(){
Map<String, Object> args = new HashMap();
//队列设置给惰性队列
args.put("x-queue-mode", "lazy");
return new Queue("lazy.queue",true,false,false,args);
}
2. @RabbitListener way to define lazy queue
When declaring the queue in the @RabbitListener annotation, add the x-queue-mode parameter
@RabbitListener(queuesToDeclare = @Queue(
value = "lazy.queue",
durable = "true",
arguments = @Argument(name = "x-queue-mode", value = "lazy")
))
public void handleLazyQueueMsg(String msg) {
}