当前位置:网站首页>死信队列入门(两个消费者,一个生产者)
死信队列入门(两个消费者,一个生产者)
2022-07-05 20:11:00 【为什么不好好卖蛋饼】
整理一下死信队列。
无法被消费的消息
死信来源
消息TTL过期
队列达到最大长度
队列满了,无法再添加消息到mq
消息被拒绝
50 死信代码架构图

直接交换机 zhangsan 被C1消费
死信交换机 lisi 添加到dead-queue 被C2消费
51 c1 消费者
public class Consumer01{
//普通交换机名称
public static final String NORMAL_EXCHANGE="normal_exchange";
//死信交换机
public static final String DEAD_EXCHANGE="dead_exchange";
//普通队列名称
public static final String NORMAL_QUEUE="normal_queue";
//死信队列名称
public static final String DEAD_QUEUE="dead_queue";
public static void main() throws Exception{
Channel channel=RabbitMqUtil.getChannel();
//声明普通交换机 /死信交换机
channel.exchangeDeclare(NORMAL_EXCHANGE,BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);
//声明普通队列
Map<String ,Object> arguments=new HashMap<>();
//过期时间
//arguments.put("x-message-ttl",1000000);
//正常队列设置过期之后的死信交换机
arguments.put("x-dead-letter-exchange",DEAD_EXCHANGE);
//设置死信RoutingKey
arguments.put("x-dead-letter-routing-key","lisi");
channel.queueDeclare(NORMAL_QUEUE,false,false,false,null);
//死信队列
channel.queueDeclare(DEAD_QUEUE,false,false,false,null);
//绑定普通交换机和队列
channel.queueBind(NORMAL_QUEUE,NORMAL_EXCHANGE,"zhangsan");
//绑定死信交换机和死信队列
channel.queueBind(DEAD_QUEUE,DEAD_EXCHANGE,"lisi");
sout("等待接收消息");
//回调函数
DeliverCallback deliverCallback=(consumerTag,message)->{
sout("Consumer01接收的消息时"+new String(message.getBody(),"UTF-8"));
};
channel.basicConsume(NORMAL_QUEUE,true,deliverCallback,consumeTag->{
});
}
}
52 生产者
public class Producer{
//普通交换机名称
public static final String NORMAL_EXCHANGE="normal_exchange";
public static void main() throws Exception{
Channel channel=RabbitMaUtils.getChannel();
//死信消息
AMQP.BasicProperties=new AMQP.BasicProperties().builder().expration("10000").build();
//死信消息 设置ttl时间
for(int i=1;i<11;i++){
String message="info"+i;
channel.basicPublish(NORMAL_EXCHANGE,"zhangsan",properties,message.getBytes());
}
}
}
停掉消费者,消息转发到死信队列
53 消费者2
这个简单,就是单纯的接收普通队列转发来的消息进行消费。
public class Consumer02{
//死信队列名称
public static final String DEAD_QUEUE="dead_queue";
public static void main() throws Exception{
Channel channel=RabbitMqUtil.getChannel();
sout("等待接收消息");
DeliverCallback deliverCallback=(consumerTag,message)->{
sout("Consumer02接收的消息时"+new String(message.getBody(),"UTF-8"));
};
channel.basicConsume(NORMAL_QUEUE,true,deliverCallback,consumeTag->{
});
}
}
边栏推荐
- 淺淺的談一下ThreadLocalInsecureRandom
- Go language | 02 for loop and the use of common functions
- 【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
- 深度學習 卷積神經網絡(CNN)基礎
- [quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods
- .Net分布式事務及落地解决方案
- .Net分布式事务及落地解决方案
- -v parameter of GST launch
- Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
- 基础篇——配置文件解析
猜你喜欢

Leetcode skimming: binary tree 12 (all paths of binary tree)

2023年深圳市绿色低碳产业扶持计划申报指南

淺淺的談一下ThreadLocalInsecureRandom

ACM getting started Day1
![[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives](/img/90/88a1f79a07016738d2688548e21949.png)
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives

解决php无法将string转换为json的办法

Elk distributed log analysis system deployment (Huawei cloud)

leetcode刷题:二叉树18(最大二叉树)

Leetcode skimming: binary tree 16 (path sum)
Android interview classic, 2022 Android interview written examination summary
随机推荐
Debezium series: PostgreSQL loads the correct last submission LSN from the offset
Notes on key vocabulary in the English original of the biography of jobs (12) [chapter ten & eleven]
Fundamentals of deep learning convolutional neural network (CNN)
Concept and syntax of function
leetcode刷题:二叉树10(完全二叉树的节点个数)
Four methods of random number generation | random | math | threadlocalrandom | securityrandom
什么是pyc文件
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
Tasks in GStreamer
【数字IC验证快速入门】8、数字IC中的典型电路及其对应的Verilog描述方法
ICTCLAS word Lucene 4.9 binding
解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
sun.misc.BASE64Encoder报错解决方法[通俗易懂]
js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
银河证券在网上开户安全吗?
Analysis of openh264 decoded data flow
[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
Android interview classic, 2022 Android interview written examination summary
Leetcode brush questions: binary tree 11 (balanced binary tree)