当前位置:网站首页>死信队列 和消息TTL过期代码
死信队列 和消息TTL过期代码
2022-07-25 23:41:00 【一个风轻云淡】
死信的概念
先从概念解释上搞清楚这个定义,死信,顾名思义就是无法被消费的消息,字面意思可以这样理解,一般来说,producer将消息投递到broker或者直接到queue里了,consumer从queue取出消息进行消费,但某些时候由于特定的原因导致queue中的某些消息无法被消费,这样的消息如果没有后续的处理,就变成了死信,有死信自然就有了死信队列。
应用场景:为了保证订单业务的消息数据不丢失,需要使用到RabbitMQ的死信队列机制,当消息消费发生异常时,将消息投入死信队列中.还有比如说: 用户在商城下单成功并点击去支付后在指定时间未支付时自动失效
死信的来源
消息TTL过期
队列达到最大长度(队列满了,无法再添加数据到mq中)
消息被拒绝(basic.reject或basic.nack)并且requeue=false.
消息TTL过期代码实战:
工具类:
public class untils {
public static Channel getChannel() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.231.133");
factory.setUsername("admin");
factory.setPassword("123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
return channel;
}
}
生产者:
public class Producer {
private static final String NORMAL_EXCHANGE="normal_exchange";
public static void main(String[] args) throws Exception{
Channel channel = untils.getChannel();
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
//设置TTL时间
AMQP.BasicProperties properties=new AMQP.BasicProperties().builder().expiration("10000").build();
//该消息用作队列的个数限制
for(int i=0;i<10;i++)
{
String message="info"+i;
channel.basicPublish(NORMAL_EXCHANGE,"zhangsan",properties,message.getBytes(StandardCharsets.UTF_8));
System.out.println("生产者发送消息"+message);
}
}
}消费者:
public class Consumer01 {
//普通交换机
private static final String NORMAL_EXCHANGE="normal_exchange";
//死信交换机
private static final String DEAD_EXCHANGE="dead_exchange";
public static void main(String[] args) throws Exception{
Channel channel = untils.getChannel();
//声明死信交换机,类型为direct
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);
//声明死信队列
String deadQueue="dead_queue";
channel.queueDeclare(deadQueue,false,false,false,null);
//死信队列绑定交换和routingKey值
channel.queueBind(deadQueue,DEAD_EXCHANGE,"lisi");
//正常队列绑定死信队列
Map<String,Object> params=new HashMap<>();
//正常队列设置死信交换机,参数key是固定值
params.put("x-dead-letter-exchange",DEAD_EXCHANGE);
//正常队列设置死信routing-key,参数key是固定值
params.put("x-dead-letter-routing-key", "lisi");
System.out.println("等待接收消息....");
String normalQueue="normal_queue";
channel.queueDeclare(normalQueue,false,false,false,params);
channel.queueBind(normalQueue,NORMAL_EXCHANGE,"zhangsan");
DeliverCallback deliverCallback=(consumerTag, message) -> {
String s = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println("01接收到消息"+message);
};
channel.basicConsume(normalQueue,true,deliverCallback,consumerTag -> {});
}
}
/**
* 死信队列消费者
*/
public class Consumer02 {
//死信交换机
private static final String DEAD_EXCHANGE="dead_exchange";
public static void main(String[] args) throws Exception{
Channel channel = untils.getChannel();
//声明死信交换机,类型为direct
channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);
String dealQueue="dead_queue";
DeliverCallback deliverCallback=(consumerTag, message) -> {
String s = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println("01接收到消息"+s);
};
channel.basicConsume(dealQueue,true,deliverCallback,consumerTag -> {});
}
}正常情况:(不关闭消费者,只开启消费者1.消费者2不开启)

正常情况结束以后:
关闭消费者01,再一次开启生产者:

可以看到正常队列这个时候还存在10个等待消费
10s以后可以发现,这个队列跑到了死信队列

这个时候开启消费者02,即死信队列的消费者

可以看到死信队列的被消费变为0
边栏推荐
- 762. Prime number calculation setting in binary representation
- Classes and objects (2) (6 default member functions)
- Vscode shortcut key: collapse and expand code
- typescript ts 基础知识之类
- Classes and objects (3)
- numeric学习之iota,accumulate
- Same origin strategy and cross domain
- [QNX Hypervisor 2.2用户手册]9.7 generate
- Learning exploration - waves
- Write a select drop-down list
猜你喜欢

Duplicate numbers in array

Read the field status of account in ABAP code (hidden, optional, required)

arcgis根据矢量范围裁取tif影像(栅格数据)、批量合并shp文件、根据矢量范围裁取区域内的矢量,输出地理坐标系、转换16位TIF影像的像素深度至8位、shp文件创建和矢量框标绘设置

Leetcode 0919. complete binary tree inserter: array representation of complete binary tree

赋值时'1和'b1有什么区别

利用用户脚本优化 Yandere/Konachan 站点浏览体验

数组中重复的数字

redis-基本数据类型(String/list/Set/Hash/Zset)

WebMvcConfigurationSupport

initializer_ List tool library learning
随机推荐
Deep and shallow copies
[code case] blog page design (with complete source code)
Practical skills of easyexcel
Problem set
Moment.js
意向不到的Dubug妙招
Grain Academy p98 trample pit e.globalexceptionhandler: null
Serialize common default values and column parameters
Native JS perfectly realizes deep copy
Implementation of date class
arcgis根据矢量范围裁取tif影像(栅格数据)、批量合并shp文件、根据矢量范围裁取区域内的矢量,输出地理坐标系、转换16位TIF影像的像素深度至8位、shp文件创建和矢量框标绘设置
Numerical learning iota, accumulate
SAP Message No. VG202 IDoc E1EDK18 中付款条款已经转移:检查数据
XXE&XML-外部实体注入-利用和绕过
【MUDUO】EventLoop事件循环
Write a select drop-down list
152. Product maximum subarray - dynamic programming
Matchmaker's words
S4/hana mm & SD EDI Nast based integrated configuration (orders, ordrsp, desadv, invoice)
Summary of built-in instructions and custom instructions