当前位置:网站首页>死信队列入门(两个消费者,一个生产者)
死信队列入门(两个消费者,一个生产者)
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->{
});
}
}
边栏推荐
- Unity编辑器扩展 UI控件篇
- leetcode刷题:二叉树12(二叉树的所有路径)
- Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
- Go language | 03 array, pointer, slice usage
- Notes on key vocabulary in the English original of the biography of jobs (12) [chapter ten & eleven]
- IC科普文:ECO的那些事儿
- [quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
- js方法传Long类型id值时会出现精确损失
- Build your own website (16)
- Bzoj 3747 poi2015 kinoman segment tree
猜你喜欢
95后阿里P7晒出工资单:狠补了这个,真香...
Build your own website (16)
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
Leetcode brush questions: binary tree 18 (largest binary tree)
Leetcode brush questions: binary tree 11 (balanced binary tree)
关于BRAM IP复位的优先级
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
Leetcode(695)——岛屿的最大面积
Let's talk about threadlocalinsecurerandom
随机推荐
Unity编辑器扩展 UI控件篇
计算lnx的一种方式
Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
Is it safe for CICC fortune to open an account online?
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
期货如何网上开户?安不安全?
建立自己的网站(16)
Go language learning tutorial (XV)
selenium 元素信息
js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
C langue OJ obtenir PE, ACM démarrer OJ
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
[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)
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
leetcode刷题:二叉树16(路径总和)
Oracle-表空间管理