• RabbitMQ spring boot TTL延时消费


    关于延时消费主要分为两种实现,一种是rabbitmq的TTL机制,一种是rabbitmq的插件实现。

    实现一:TTL

    1、设置队列的过期时间
    2、设置消息的过期时间

    添加相关maven依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4

    1、设置队列过期时间实现延时消费

    交换机及队列配置

    代码中有四个配置,
    第一个配置的exchange是用来接收已过期的队列信息并进行重新分配队列进行消费,
    第二个配置的repeatTradeQueue为exchange重新分配的队列名,
    第三个是将repeatTradeQueue队列与exchange交换机绑定,并指定对应的routing key,
    第四个配置的就是我们要设置过期时间的队列deadLetterQueue,
    配置中有三个参数,x-message-ttl为过期时间,该队列所有消息的过期时间都为配置的这个值,单位为毫秒,这里设置过期时间为3秒,x-dead-letter-exchange是指过期消息重新转发到指定交换机,也就是exchange,x-dead-letter-routing-key是该交换机上绑定的routing-key,将通过配置的routing-key分配对应的队列,也就是前面配置的repeatTradeQueue。

    import java.util.HashMap;
    import java.util.Map;
     
    import org.springframework.amqp.core.Binding;
    import org.springframework.amqp.core.BindingBuilder;
    import org.springframework.amqp.core.DirectExchange;
    import org.springframework.amqp.core.Queue;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
     
    @SpringBootApplication
    public class Application {
        
        //交换机用于重新分配队列
        @Bean
        DirectExchange exchange() {
            return new DirectExchange("exchange");
        }
        
        //用于延时消费的队列
        @Bean
        public Queue repeatTradeQueue() {
            Queue queue = new Queue("repeatTradeQueue",true,false,false);
            return queue; 
        }
        
        //绑定交换机并指定routing key
        @Bean
        public Binding  repeatTradeBinding() {
            return BindingBuilder.bind(repeatTradeQueue()).to(exchange()).with("repeatTradeQueue");
        }
        
        //配置死信队列
        @Bean
        public Queue deadLetterQueue() {
            Map<String,Object> args = new HashMap<>();
            args.put("x-message-ttl", 3000);
            args.put("x-dead-letter-exchange", "exchange");
            args.put("x-dead-letter-routing-key", "repeatTradeQueue");
            return new Queue("deadLetterQueue", true, false, false, args);
        }
     
        
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    配置生产者,这里生产者需要指定前面配置了过期时间的队列deadLetterQueue

    import java.time.LocalDateTime;
     
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
     
    @Component
    public class DeadLetterSender {
     
        @Autowired
        private AmqpTemplate rabbitTemplate;
     
     
        public void send(String msg) {
            System.out.println("DeadLetterSender 发送时间:"+LocalDateTime.now().toString()+" msg内容:"+msg);
            rabbitTemplate.convertAndSend("deadLetterQueue", msg);
        }
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    配置消费者,消费者监听指定用于延时消费的队列repeatTradeQueue

    import java.time.LocalDateTime;
     
    @Component
    @RabbitListener(queues = "repeatTradeQueue")
    public class RepeatTradeReceiver {
        
        @RabbitHandler
        public void process(String msg) {
            System.out.println("repeatTradeQueue 接收时间:"+LocalDateTime.now().toString()+" 接收内容:"+msg);
        }
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    写一个简单的接口调用测试延时消费是否成功

    import org.springframework.beans.factory.annotation.Autowired;
     
    @RestController
    @RequestMapping("/rabbit")
    public class RabbitTest {
     
        @Autowired
        private DeadLetterSender deadLetterSender;
     
     
        @GetMapping("/deadTest")
        public void deadTest() {
            deadLetterSender.send("队列设置过期时间测试");
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2、设置消息过期时间实现延时消费

    先贴上配置的代码,基本配置都一样,唯一的区别是deadLetterQueue的过期时间这里不做配置,需要注意的是,因为我这里用的是同一个队列名,所以即使将队列过期时间配置删除,mq中该队列过期时间仍然还是存在的,所以需要删除该队列,启动项目时才能重新配置该队列属性,可能可以通过配置的方式重新覆盖属性配置,当然也可以保留队列过期时间的配置,当两个过期时间都存在时,消息取更小的过期时间。

    import java.util.HashMap;
     
    @SpringBootApplication
    public class Application {
        
        //用于死信队列转发的交换机
        @Bean
        DirectExchange exchange() {
            return new DirectExchange("exchange");
        }
        
        //用于延时消费的队列
        @Bean
        public Queue repeatTradeQueue() {
            Queue queue = new Queue("repeatTradeQueue",true,false,false);
            return queue; 
        }
        
        //绑定交换机并指定routing key
        @Bean
        public Binding  repeatTradeBinding() {
            return BindingBuilder.bind(repeatTradeQueue()).to(exchange()).with("repeatTradeQueue");
        }
        
        //配置死信队列
        @Bean
        public Queue deadLetterQueue() {
            Map<String,Object> args = new HashMap<>();
            args.put("x-dead-letter-exchange", "exchange");
            args.put("x-dead-letter-routing-key", "repeatTradeQueue");
            return new Queue("deadLetterQueue", true, false, false, args);
        }
     
        
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    配置生产者,message的expiration就是过期时间的设置,单位也是毫秒

    import java.time.LocalDateTime;
     
    import org.springframework.amqp.AmqpException;
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.core.MessagePostProcessor;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
     
    @Component
    public class DeadLetterSender {
     
        @Autowired
        private AmqpTemplate rabbitTemplate;
     
        public void send(String msg, long times) {
            System.out.println("DeadLetterSender 发送时间:" + LocalDateTime.now().toString() + " msg内容:" + msg);
            MessagePostProcessor processor = new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    message.getMessageProperties().setExpiration(times + "");
                    return message;
                }
            };
            rabbitTemplate.convertAndSend("deadLetterQueue", (Object)msg, processor);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    消费者不变,用之前的类即可

    稍微修改一下接口,设置时间为5秒

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    import me.miaobo.mq.sender.DeadLetterSender;
     
    @RestController
    @RequestMapping("/rabbit")
    public class RabbitTest {
     
        @Autowired
        private DeadLetterSender deadLetterSender;
        
        @GetMapping("/deadTest")
        public void deadTest() {
            deadLetterSender.send("消息设置过期时间测试",5000);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    基于web在线餐饮网站的设计与实现——仿Coco线上订奶茶饮料6个页面(HTML+CSS+JavaScript)
    Linux UWB Stack实现——MCPS帧处理
    springboot+华迪企业合同管理平台 毕业设计-附源码191555
    盘点编写 sql 上的那些骚操作(针对mysql而言)
    程序放在哪儿?
    RabbitMQ面试篇
    iOS 将字符串分割成单个字符| 字符串转成数组
    【python】(四)python常用数据结构
    Revit导入CAD翻模丨CAD图层管理控制显示隐藏图层
    展锐S 上微距模式不识别问题跟进 --- sensor_config的解析
  • 原文地址:https://blog.csdn.net/qq_41158114/article/details/138171886