您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
尚硅谷RabbitMQ视频-032-进阶篇-死信-产生原因1拒绝
发布时间:2026-11-15 02:03:01编辑:雪饮阅读()
-
上篇中忘了正常队列与正常交换机的绑定
- 正常交换机:exchange.normal.video
- 正常队列:queue.normal.video
- 正常路由键:routing.key.normal.video
三种情况
# 二、消费端拒收消息
## 1、发送消息的代码
@Test
public void testSendMessageButReject() {
rabbitTemplate
.convertAndSend(
EXCHANGE_NORMAL,
ROUTING_KEY_NORMAL,
"测试死信情况1:消息被拒绝");
}
2、接收消息的代码
①监听正常队列
@RabbitListener(queues = {QUEUE_NORMAL})
public void processMessageNormal(Message message, Channel channel) throws IOException {
// 监听正常队列,但是拒绝消息
log.info("★[normal]消息接收到,但我拒绝。");
channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
}②监听死信队列
@RabbitListener(queues = {QUEUE_DEAD_LETTER})
public void processMessageDead(String dataString, Message message, Channel channel) throws IOException {
// 监听死信队列
log.info("★[dead letter]dataString = " + dataString);
log.info("★[dead letter]我是死信监听方法,我接收到了死信消息");
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}然后别忘了在rabbitmq中为死信队列绑定来自正常队列所指定的死信交换机以及死信routing key
开始写代码
我们新建模块model32
D:\rabbitMqLearning\pom.xml中新增
<modules>
<module>currentLimiting</module>
<module>timeout</module>
<module>model32</module>
</modules>然后D:\rabbitMqLearning\model32\pom.xml:
<?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">
<parent>
<artifactId>rabbitMqLearning</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>model32</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<!--普通项目转换为spring boot项目-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.1.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- spring-boot rabbitmq amqp 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- spring-boot lombok 用于注解 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>然后D:\rabbitMqLearning\model32\src\main\resources\application.yml:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: 123456
virtual-host: /
listener:
simple:
acknowledge-mode: manual # 把消息确认模式改为手动确认,因为默认都是按成功处理,默认都是ack
publisher-confirm-type: correlated #交换机的确认(**开启【异步关联发布确认】,用来知道消息有没有成功送到交换机(Broker),发送消息的时候,你可以给这条消息带上一个唯一标识 `CorrelationData`;当 RabbitMQ 交换机收到消息后,回调会把这个标识还给你,**让你精准匹配:是哪一条消息 ack/nack 了。)
publisher-returns: true #队列的确认(作用:开启【消息回退(Return)】机制,消息已经成功发到交换机(exchange),**但是交换机找不到任何队列可以路由这条消息**(routingKey 不匹配、没有队列绑定),此时把这条消息退回给生产者,让生产者感知这个异常)然后D:\rabbitMqLearning\model32\src\main\java\org\example\listener\MyMessageListener.java:
package org.example.listener;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@Component
@Slf4j
public class MyMessageListener {
public static final String QUEUE_NORMAL = "queue.normal.video";
public static final String QUEUE_DEAD_LETTER = "queue.dead.letter.video";
@RabbitListener(queues = {QUEUE_NORMAL})
public void processMessageNormal(Message message, Channel channel) throws IOException {
// 监听正常队列,但是拒绝消息 并不重投(requeue为false)
log.info("★[normal]消息接收到,但我拒绝。");
channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
}
@RabbitListener(queues = {QUEUE_DEAD_LETTER})
public void processMessageDead(String dataString, Message message, Channel channel) throws IOException {
// 监听死信队列
log.info("★[dead letter]dataString = " + dataString);
log.info("★[dead letter]我是死信监听方法,我接收到了死信消息");
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}
}然后D:\rabbitMqLearning\model32\src\main\java\org\example\RabbitMQConsumerMainType.java:
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitMQConsumerMainType {
public static void main(String[] args) {
SpringApplication.run(RabbitMQConsumerMainType.class, args);
}
}然后运行这个main函数就监听正常队列进行消费在正常里面拒绝并不重放进入队列就进入了死信队列中
然后在正常交换机上面发送消息
在正常交换机exchange.normal.video中向
Routing key为routing.key.normal.video
进行发送消息如test dead letter
查看消费端打印
2026-11-15T01:56:33.049+08:00 INFO 12128 --- [ntContainer#1-1] org.example.listener.MyMessageListener : ★[normal]消息接收到,但我拒绝。
2026-11-15T01:56:33.092+08:00 INFO 12128 --- [ntContainer#0-1] org.example.listener.MyMessageListener : ★[dead letter]dataString = test dead letter
2026-11-15T01:56:33.092+08:00 INFO 12128 --- [ntContainer#0-1] org.example.listener.MyMessageListener : ★[dead letter]我是死信监听方法,我接收到了死信消息
关键字词:RabbitMQ,死信,产生原因,拒绝
上一篇:尚硅谷RabbitMQ视频-031-进阶篇-死信-测试准备
下一篇:返回列表