当前位置:网站首页>死信队列入门(两个消费者,一个生产者)
死信队列入门(两个消费者,一个生产者)
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->{
});
}
}
边栏推荐
- Go language learning tutorial (XV)
- DP:树DP
- 《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
- Jvmrandom cannot set seeds | problem tracing | source code tracing
- CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
- Process file and directory names
- Tasks in GStreamer
- After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
- Wildcard selector
- 微信小程序正则表达式提取链接
猜你喜欢

【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)

建立自己的网站(16)

S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo

js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)

SecureRandom那些事|真伪随机数

leetcode刷题:二叉树14(左叶子之和)
![[quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods](/img/3a/7eaff0bf819c129b4f866388e57b87.png)
[quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods

Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)

Scala基础【HelloWorld代码解析,变量和标识符】

Interviewer: what is the internal implementation of set data types in redis?
随机推荐
零道云新UI设计中
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
Relationship between floating elements and parent and brother boxes
微信小程序正则表达式提取链接
95后阿里P7晒出工资单:狠补了这个,真香...
How to safely and quickly migrate from CentOS to openeuler
基金网上开户安全吗?去哪里开,可以拿到低佣金?
Bzoj 3747 poi2015 kinoman segment tree
[quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
Go language learning tutorial (XV)
selenium 元素信息
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
Ffplay document [easy to understand]
-v parameter of GST launch
【数字IC验证快速入门】3、数字IC设计全流程介绍
解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
线程池参数及合理设置
【c语言】快速排序的三种实现以及优化细节
leetcode刷题:二叉树10(完全二叉树的节点个数)