【rabbitmq】绑定死信队列示例

目录

          • [1. 说明](#1. 说明)
          • [2. 截图示例](#2. 截图示例)
          • [3. 代码示例](#3. 代码示例)
1. 说明
  • 1.正常队列绑定死信交换机和死信routing_key,正常队列中的消息在特殊情况下变为死信时,将死信通过绑定的死信交换机转移到死信队列中。
  • 2.死信队列说明
  • 3.这里演示的是10秒未消费的消息,进入死信队列。
2. 截图示例
  • 1.消息未过期
  • 2.消息10秒后过期,进入死信队列
  • 3.项目结构图
3. 代码示例
  • 1.pom依赖

    <?xml version="1.0" encoding="UTF-8"?>

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.5.4</version>
      </parent>
    
      <groupId>com.learning</groupId>
      <artifactId>springboot</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
              <version>2.5.4</version>
          </dependency>
    
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-test</artifactId>
              <version>2.5.4</version>
          </dependency>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-amqp</artifactId>
          </dependency>
          <dependency>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
          </dependency>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <scope>test</scope>
          </dependency>
      </dependencies>
    
      <!--打jar包使用-->
      <build>
          <plugins>
              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
              </plugin>
          </plugins>
      </build>
    
    </project>
  • 2.application.yaml

    spring:
    rabbitmq:
    host: 192.168.2.11
    port: 5672
    username: admin
    password: admin
    virtual-host: /

  • 3.RabbitMQConfig配置类

    package com.learning.config;

    import org.springframework.amqp.core.*;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;

    /**

    • rabbitmq配置类
      */
      @Configuration
      public class RabbitMQConfig{

      /**

      • 正常交换机名
        */
        public static final String EXCHANGE_NAME = "topic_exchange";

      /**

      • 正常队列名
        */
        public static final String QUEUE_NAME = "topic_queue";

      @Bean("topic_queue")
      public Queue queue() {
      Queue queue = new Queue(QUEUE_NAME, true, false, false);
      // 设置死信交换机
      queue.addArgument("x-dead-letter-exchange", "dlx_exchange");
      // 设置死信routingkey
      queue.addArgument("x-dead-letter-routing-key", "dlx_routing_key");
      // 设置队列的过期时间
      queue.addArgument("x-message-ttl", 10000);
      // 设置队列的长度限制
      queue.addArgument("x-max-length", 10);
      return queue;
      }

      /**

      • 主题模式,这里按实际业务切换不同模式
      • @return
        */
        @Bean("topic_exchange")
        public Exchange exchange() {
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
        }

      @Bean
      public Binding binding(@Qualifier("topic_queue") Queue queue,@Qualifier("topic_exchange") Exchange exchange) {
      return BindingBuilder.bind(queue).to(exchange).with("topic_routing_key").noargs();
      }

      public static final String DLX_QUEUE_NAME = "dlx_queue";

      public static final String DLX_EXCHANGE_NAME = "dlx_exchange";

      @Bean("dlx_queue")
      public Queue dlxQueue() {
      return new Queue(DLX_QUEUE_NAME, true);
      }

      @Bean("dlx_exchange")
      public Exchange dlxExchange() {
      return ExchangeBuilder.topicExchange(DLX_EXCHANGE_NAME).durable(true).build();
      }

      @Bean
      public Binding dlxBinding(@Qualifier("dlx_queue") Queue queue,@Qualifier("dlx_exchange") Exchange exchange) {
      return BindingBuilder.bind(queue).to(exchange).with("dlx_routing_key").noargs();
      }
      }

  • 4.测试类

    package com.learning;

    import org.junit.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;

    /**

    • @Author wangyouhui

    • @Description
      **/
      @RunWith(SpringRunner.class)
      @SpringBootTest
      public class RabbitMQTest {

      @Autowired
      private RabbitTemplate rabbitTemplate;

      @Test
      public void test1(){
      rabbitTemplate.convertAndSend("topic_exchange", "topic_routing_key", "测试死信消息,10秒过期");
      }
      }

相关推荐
bug_null1 天前
RabbitMQ消息可靠性保证机制4--消费端限流
rabbitmq
青石路1 天前
不依赖 Spring,你会如何自实现 RabbitMQ 消息的消费(一)
rabbitmq
呼啦啦啦啦啦啦啦啦1 天前
【Rabbitmq篇】RabbitMQ⾼级特性----持久性,发送⽅确认,重试机制
分布式·rabbitmq
斯普信专业组2 天前
RabbitMQ实战启程:从配置到故障排查的实战处理(下)
网络·分布式·rabbitmq
呼啦啦啦啦啦啦啦啦2 天前
【Rabbitmq篇】RabbitMQ⾼级特性----消息确认
分布式·rabbitmq
泰山小张只吃荷园2 天前
快速入门消息队列MQ、RabbitMQ
java·spring boot·分布式·spring·spring cloud·rabbitmq·java-rabbitmq
呼啦啦啦啦啦啦啦啦3 天前
基于SpringBoot+RabbitMQ完成应⽤通信
spring boot·rabbitmq·java-rabbitmq
小马爱打代码3 天前
SpringBoot 整合 RabbitMQ
spring boot·rabbitmq·java-rabbitmq
漫天转悠4 天前
RabbitMQ的基本概念和入门
分布式·rabbitmq
dvlinker4 天前
大数据技术Kafka详解 ① | 消息队列(Messages Queue)
大数据·kafka·rabbitmq·rocketmq·activemq·分布式发布订阅消息系统·messages queue