日常记录-SpringBoot整合RabbitMQ第六节(惰性队列)

从RabbitMQ的3.6.0版本开始,就增加了Lazy Queues的概念,也就是惰性队列。

惰性队列的特征如下:

  1. 接收到消息后直接存入磁盘而非内存;
  2. 消费者要消费消息时才会从磁盘中读取并加载到内存;
  3. 支持数百万条的消息存储;

一、如何在定义惰性队列

要设置一个队列为惰性队列,只需要在声明队列时,指定x-queue-mode属性为lazy即可。
可以通过命令行方式、@Bean方式以及注解方式定义惰性队列,这里只讲@Bean方式以及@RabbitListener方式。

在这里插入图片描述

1、@Bean方式定义惰性队列 (推荐这种方式)


@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方式定义惰性队列

在@RabbitListener注解中声明队列时,添加x-queue-mode参数

@RabbitListener(queuesToDeclare = @Queue(
        value = "lazy.queue",
        durable = "true",
        arguments = @Argument(name = "x-queue-mode", value = "lazy")
))
public void handleLazyQueueMsg(String msg) {
    
    

}

猜你喜欢

转载自blog.csdn.net/qq407995680/article/details/132149647