死信队列 - 死信交换机绑定配置【RabbitMQ】

一、逻辑图

二、死信交换机绑定配置

复制代码
 1 package cn.itcast.mq.config;
 2 
 3 import org.springframework.amqp.core.*;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 
 7 import java.util.HashMap;
 8 import java.util.Map;
 9 
10 @Configuration
11 public class TTLConfig {
12 
13     /**
14      * 默认交换机(可不要)
15      */
16     @Bean
17     public DirectExchange directExchange01() {
18         return new DirectExchange("mf.exchange");
19     }
20 
21     /**
22      * 死信队列
23      */
24     @Bean
25     public Queue queue01() {
26         Map<String, Object> args = new HashMap<>();
27         // 1.标识是一个死信队列
28         args.put("x-dead-letter-routing-key", "ttl");
29         // 2.标识信息死亡后进入哪个死信交换机
30         args.put("x-dead-letter-exchange", "dl.exchange");
31         // 3.死亡时间
32         args.put("x-message-ttl", 10000);
33         return QueueBuilder.durable("dl.queue").withArguments(args).build();
34     }
35 
36     /**
37      * 关系建立 - 默认交换机 与 死信队列
38      */
39     @Bean
40     public Binding queueBinding01(DirectExchange directExchange01, Queue queue01) {
41         return BindingBuilder.bind(queue01).to(directExchange01).with("ttl");
42     }
43 
44     /**
45      * 死信交换机
46      */
47     @Bean
48     public DirectExchange directExchange02() {
49         return new DirectExchange("dl.exchange");
50     }
51 
52     /**
53      * 普通队列
54      */
55     @Bean
56     public Queue queue02() {
57         return new Queue("mf.queue");
58     }
59 
60     /**
61      * 关系建立 - 死信交换机 与 普通队列
62      */
63     @Bean
64     public Binding queueBinding02(DirectExchange directExchange02, Queue queue02) {
65         return BindingBuilder.bind(queue02).to(directExchange02).with("ttl");
66     }
67 }
复制代码

 三、死信交换机测试

复制代码
package cn.itcast.mq.helloworld;

import cn.itcast.mq.PublisherApplication;
// import org.junit.Test;
import org.junit.jupiter.api.Test;
// import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
// import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = PublisherApplication.class)
// @RunWith(SpringRunner.class)
public class SpringAMQPTest {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 延迟消息
     */
    @Test
    public void test06() {
        String exchange = "mf.exchange";
        String msg = "10秒后的延迟消息";
        String routingKey = "ttl";
        rabbitTemplate.convertAndSend(exchange, routingKey, msg);
    }
}
复制代码

四、监听类

复制代码
 1 package cn.itcast.mq.easyqueue;
 2 
 3 import org.springframework.amqp.rabbit.annotation.RabbitListener;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component
 7 public class SpringRabbitListener {
 8 
 9     @RabbitListener(queues = "mf.queue")
10     public void msg02(String msg) {
11         System.out.println("接收到的消息为:" + msg);
12     }
13 }
复制代码

五、启动类

复制代码
 1 package cn.itcast.mq;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class ConsumerApplication {
 8     public static void main(String[] args) {
 9         SpringApplication.run(ConsumerApplication.class, args);
10     }
11 }
复制代码

六、执行测试类后,在RabbitMQ 控制台的效果

七、运行启动类后,再次使用测试类的结果

 

posted @   青核桃啊  阅读(207)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示