操纵014:惰性队列

打印 上一主题 下一主题

主题 891|帖子 891|积分 2673

操纵014:惰性队列

一、创建惰性队列

1、官网说明


队列可以创建为默认或惰性模式,模式指定方式是:


  • 使用队列策略(建议)
  • 设置queue.declare参数
假如策略和队列参数同时指定,那么队列参数有更高优先级。假如队列模式是在声明时通过可选参数指定的,那么只能通过删除队列再重新创建来修改。
2、基于策略方式设定

  1. # 登录Docker容器
  2. docker exec -it rabbitmq /bin/bash
  3. # 运行rabbitmqctl命令
  4. rabbitmqctl set_policy Lazy "^lazy-queue$" '{"queue-mode":"lazy"}' --apply-to queues
复制代码
命令解读:


  • rabbitmqctl命令所在目录是:/opt/rabbitmq/sbin,该目录已配置到Path环境变量
  • set_policy是子命令,表示设置策略
  • Lazy是当前要设置的策略名称,是我们本身自定义的,不是系统定义的
  • "^lazy-queue$"是用正则表达式限定的队列名称,凡是名称符合这个正则表达式的队列都会应用这里的设置
  • '{“queue-mode”:“lazy”}'是一个JSON格式的参数设置指定了队列的模式为"lazy"
  • –-apply-to参数指定该策略将应用于队列(queues)级别
  • 命令执行后,所著名称符合正则表达式的队列都会应用指定策略,包括将来新创建的队列
假如需要修改队列模式可以执行如下命令(不必删除队列再重建):
  1. rabbitmqctl set_policy Lazy "^lazy-queue$" '{"queue-mode":"default"}' --apply-to queues
复制代码
3、在声明队列时使用参数设定



  • 参数名称:x-queue-mode
  • 可用参数值:

    • default
    • lazy

  • 不设置就是取值为default
Java代码原生API设置方式:
  1. Map<String, Object> args = new HashMap<String, Object>();
  2. args.put("x-queue-mode", "lazy");
  3. channel.queueDeclare("myqueue", false, false, false, args);
复制代码
Java代码注解设置方式:
  1. @Queue(value = QUEUE_NAME, durable = "true", autoDelete = "false", arguments = {
  2.         @Argument(name = "x-queue-mode", value = "lazy")
  3. })
复制代码
二、实操演练

1、生产者端代码

①配置POM

  1.     <parent>
  2.         <groupId>org.springframework.boot</groupId>
  3.         <artifactId>spring-boot-starter-parent</artifactId>
  4.         <version>3.1.5</version>
  5.     </parent>
  6.     <dependencies>
  7.         <dependency>
  8.             <groupId>org.springframework.boot</groupId>
  9.             <artifactId>spring-boot-starter-amqp</artifactId>
  10.         </dependency>
  11.         <dependency>
  12.             <groupId>org.springframework.boot</groupId>
  13.             <artifactId>spring-boot-starter-test</artifactId>
  14.         </dependency>
  15.         <dependency>
  16.             <groupId>org.projectlombok</groupId>
  17.             <artifactId>lombok</artifactId>
  18.         </dependency>
  19.     </dependencies>
复制代码
②配置YAML

  1. spring:
  2.   rabbitmq:
  3.     host: 192.168.200.100
  4.     port: 5672
  5.     username: guest
  6.     password: 123456
  7.     virtual-host: /
复制代码
③主启动类

  1. package com.atguigu.mq;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class RabbitMQLazyProducer {
  6.     public static void main(String[] args) {
  7.         SpringApplication.run(RabbitMQLazyProducer.class, args);
  8.     }
  9. }
复制代码
④发送消息

  1. package com.atguigu.mq.test;
  2. import jakarta.annotation.Resource;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. @SpringBootTest
  7. public class RabbitMQTest {
  8.     public static final String EXCHANGE_LAZY_NAME = "exchange.atguigu.lazy";
  9.     public static final String ROUTING_LAZY_KEY = "routing.key.atguigu.lazy";
  10.     @Resource
  11.     private RabbitTemplate rabbitTemplate;
  12.     @Test
  13.     public void testSendMessage() {
  14.         rabbitTemplate.convertAndSend(EXCHANGE_LAZY_NAME, ROUTING_LAZY_KEY, "I am a message for test lazy queue.");
  15.     }
  16. }
复制代码
2、消费者端代码

①配置POM

  1.     <parent>
  2.         <groupId>org.springframework.boot</groupId>
  3.         <artifactId>spring-boot-starter-parent</artifactId>
  4.         <version>3.1.5</version>
  5.     </parent>
  6.     <dependencies>
  7.         <dependency>
  8.             <groupId>org.springframework.boot</groupId>
  9.             <artifactId>spring-boot-starter-amqp</artifactId>
  10.         </dependency>
  11.         <dependency>
  12.             <groupId>org.springframework.boot</groupId>
  13.             <artifactId>spring-boot-starter-web</artifactId>
  14.         </dependency>
  15.         <dependency>
  16.             <groupId>org.projectlombok</groupId>
  17.             <artifactId>lombok</artifactId>
  18.         </dependency>
  19.     </dependencies>
复制代码
②配置YAML

  1. spring:
  2.   rabbitmq:
  3.     host: 192.168.200.100
  4.     port: 5672
  5.     username: guest
  6.     password: 123456
  7.     virtual-host: /
复制代码
③主启动类

  1. package com.atguigu.mq;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class RabbitMQLazyConsumerMainType {
  6.     public static void main(String[] args) {
  7.         SpringApplication.run(RabbitMQLazyConsumerMainType.class, args);
  8.     }
  9.    
  10. }
复制代码
④监听器

  1. package com.atguigu.mq.listener;
  2. import com.rabbitmq.client.Channel;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.amqp.core.Message;
  5. import org.springframework.amqp.rabbit.annotation.*;
  6. import org.springframework.stereotype.Component;
  7. @Component
  8. @Slf4j
  9. public class MyLazyMessageProcessor {
  10.     public static final String EXCHANGE_LAZY_NAME = "exchange.atguigu.lazy";
  11.     public static final String ROUTING_LAZY_KEY = "routing.key.atguigu.lazy";
  12.     public static final String QUEUE_LAZY_NAME = "queue.atguigu.lazy";
  13.     @RabbitListener(bindings = @QueueBinding(
  14.         value = @Queue(value = QUEUE_LAZY_NAME, durable = "true", autoDelete = "false", arguments = {
  15.             @Argument(name = "x-queue-mode", value = "lazy")
  16.         }),
  17.         exchange = @Exchange(value = EXCHANGE_LAZY_NAME, durable = "true", autoDelete = "false"),
  18.         key = {ROUTING_LAZY_KEY}
  19.     ))
  20.     public void processMessageLazy(String data, Message message, Channel channel) {
  21.         log.info("消费端接收到消息:" + data);
  22.     }
  23. }
复制代码
三、测试



  • 先启动消费端
  • 基于消费端@RabbitListener注解中的配置,自动创建了队列



  • 发送消息

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

杀鸡焉用牛刀

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表