一、逻辑图
二、死信交换机绑定配置
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 控制台的效果
七、运行启动类后,再次使用测试类的结果
标签:RabbitMQ,死信,交换机,springframework,org,import,public From: https://www.cnblogs.com/Rover20230226/p/17494382.html