【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秒过期");
      }
      }

相关推荐
Mao.O10 小时前
RabbitMQ延迟消息插件安装(Docker环境)
docker·rabbitmq·延迟消息发送
小小娥子10 小时前
rabbitmq高级特性(2)TTL、死信/延迟队列、事务与消息分发
分布式·rabbitmq
风随心飞飞1 天前
SpringBoot 集成RabbitMQ 实现钉钉日报定时发送功能
spring boot·rabbitmq·java-rabbitmq
山脚ice1 天前
【01初识】-初识 RabbitMQ
rabbitmq
元气满满的热码式1 天前
消息队列-RabbitMQ
分布式·rabbitmq
山脚ice2 天前
【02基础】- RabbitMQ基础
rabbitmq
王佑辉2 天前
【rabbitmq】RabbitMQ中的交换机有哪些类型
rabbitmq
不能再留遗憾了2 天前
RabbitMQ 高级特性——事务
分布式·rabbitmq·ruby
王佑辉2 天前
【rabbitmq】什么是死信
rabbitmq