当前位置:网站首页>死信队列入门(两个消费者,一个生产者)
死信队列入门(两个消费者,一个生产者)
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->{
});
}
}
边栏推荐
- leetcode刷题:二叉树12(二叉树的所有路径)
- Process file and directory names
- sun.misc.BASE64Encoder报错解决方法[通俗易懂]
- Tasks in GStreamer
- Let's talk about threadlocalinsecurerandom
- nprogress插件 进度条
- [quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
- Cocos2d-x项目总结中的一些遇到的问题
- 图嵌入Graph embedding学习笔记
- Summer Challenge harmonyos - realize message notification function
猜你喜欢
- Oui. Net Distributed Transaction and Landing Solution
A solution to PHP's inability to convert strings into JSON
Database logic processing function
[quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)
【数字IC验证快速入门】2、通过一个SoC项目实例,了解SoC的架构,初探数字系统设计流程
[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)
Leetcode skimming: binary tree 16 (path sum)
leetcode刷题:二叉树14(左叶子之和)
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
leetcode刷题:二叉树18(最大二叉树)
随机推荐
Elk distributed log analysis system deployment (Huawei cloud)
秋招字节面试官问你还有什么问题?其实你已经踩雷了
Android interview classic, 2022 Android interview written examination summary
leetcode刷题:二叉树11(平衡二叉树)
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
- Oui. Net Distributed Transaction and Landing Solution
线程池参数及合理设置
淺淺的談一下ThreadLocalInsecureRandom
How to safely and quickly migrate from CentOS to openeuler
leetcode刷题:二叉树10(完全二叉树的节点个数)
ICTCLAS用的字Lucene4.9捆绑
本季度干货导航 | 2022年Q2
[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)
Jvmrandom cannot set seeds | problem tracing | source code tracing
微信小程序正则表达式提取链接
Leetcode brush question: binary tree 13 (the same tree)
Debezium series: parsing the default value character set
中金财富在网上开户安全吗?
C language OJ gets PE, OJ of ACM introduction~
Relationship between floating elements and parent and brother boxes