当前位置:网站首页>死信队列入门(两个消费者,一个生产者)
死信队列入门(两个消费者,一个生产者)
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 | 01 wsl+vscode environment construction pit avoidance Guide
- ffplay文档[通俗易懂]
- Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
- 期货如何网上开户?安不安全?
- . Net distributed transaction and landing solution
- Tasks in GStreamer
- [quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design
- What is PyC file
- Guidelines for application of Shenzhen green and low carbon industry support plan in 2023
- Build your own website (16)
猜你喜欢

Jvmrandom cannot set seeds | problem tracing | source code tracing

Leetcode: binary tree 15 (find the value in the lower left corner of the tree)

Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
![[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design](/img/92/7af0db21b3d7892bdc5dce50ca332e.png)
[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design

A solution to PHP's inability to convert strings into JSON

Scala基础【HelloWorld代码解析,变量和标识符】
![[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)](/img/6d/110b87747f0a4be52da9fd49a05b82.png)
[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)

B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05

How to select the Block Editor? Impression notes verse, notation, flowus
随机推荐
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
【c语言】快速排序的三种实现以及优化细节
ROS2专题【01】:win10上安装ROS2
[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design
Leetcode(347)——前 K 个高频元素
Notes on key vocabulary in the English original of the biography of jobs (12) [chapter ten & eleven]
解决php无法将string转换为json的办法
leetcode刷题:二叉树13(相同的树)
2023年深圳市绿色低碳产业扶持计划申报指南
建立自己的网站(16)
leetcode刷题:二叉树11(平衡二叉树)
Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
1:引文;
Analysis of openh264 decoded data flow
[quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)
ICTCLAS用的字Lucene4.9捆绑
kubernetes资源对象介绍及常用命令(五)-(ConfigMap&Secret)
ACM getting started Day1
Is it safe for Guosen Securities to open an account online?
C - sequential structure