目录
-
-
-
-
- [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>
</project><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>
-
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秒过期");
}
}
-