当前位置:网站首页>消息被拒MQ
消息被拒MQ
2022-07-27 01:16:00 【一个风轻云淡】
生产者
/**
* 消息被拒的情况
*/
public class Produce0001 {
private static final String NORMAL_EXCHANGE="normal_exchange";
public static void main(String[] args) throws Exception{
Channel channel = untils.getChannel();
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
//该消息用作队列的个数限制
for(int i=0;i<10;i++)
{
String message="info"+i;
channel.basicPublish(NORMAL_EXCHANGE,"zhangsan",null,message.getBytes(StandardCharsets.UTF_8));
System.out.println("生产者发送消息"+message);
}
}
}消费者:
/**
* 消息被拒的情况
*/
public class Consumer0001 {
//普通交换机
private static final String NORMAL_EXCHANGE="normal_exchange";
//死信交换机
private static final String DEAD_EXCHANGE="dead_exchange";
public static void main(String[] args) throws Exception{
Channel channel = untils.getChannel();
//声明死信交换机,类型为direct
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);
//声明死信队列
String deadQueue="dead_queue";
channel.queueDeclare(deadQueue,false,false,false,null);
//死信队列绑定交换和routingKey值
channel.queueBind(deadQueue,DEAD_EXCHANGE,"lisi");
//正常队列绑定死信队列
Map<String,Object> params=new HashMap<>();
//正常队列设置死信交换机,参数key是固定值
params.put("x-dead-letter-exchange",DEAD_EXCHANGE);
//正常队列设置死信routing-key,参数key是固定值
params.put("x-dead-letter-routing-key", "lisi");
//正常队列设置的最大限制长度
params.put("x-max-length",6);
System.out.println("等待接收消息....");
String normalQueue="normal_queue";
channel.queueDeclare(normalQueue,false,false,false,params);
channel.queueBind(normalQueue,NORMAL_EXCHANGE,"zhangsan");
DeliverCallback deliverCallback=(consumerTag, message) -> {
String s = new String(message.getBody(), StandardCharsets.UTF_8);
if (s.equals("info5"))
{
System.out.println("info5拒接");
channel.basicReject(message.getEnvelope().getDeliveryTag(), false);
}
else
{
System.out.println("01接收到消息"+s);
channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
}
};
channel.basicConsume(normalQueue,false,deliverCallback,consumerTag -> {});
}
}结果:



边栏推荐
- 一个测试类了解BeanUtils.copyProperties
- Complete source code of mall applet project (wechat applet)
- [binary search medium] leetcode 34. find the first and last positions of elements in the sorted array
- On the prototype of constructor
- 二叉树(北京邮电大学机试题)(DAY 85)
- Shortcut keys commonly used in idea
- Naive Bayes -- Document Classification
- 正方形数组的数目(DAY 81)
- Hcip 13th day notes
- 全网最全的软件测试基础知识整理(新手入门必学)
猜你喜欢

Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns

百度云人脸识别

单例模式(双检锁)

抖音服务器带宽有多大,才能供上亿人同时刷?

Ten thousand words long text, take you to understand the kubernetes network model

论构造函数的原型是谁

Abbkine AbFluor 488 细胞凋亡检测试剂盒特点及实验建议

阿里云技术专家杨泽强:弹性计算云上可观测能力的构建

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

Worth more than 100 million! The 86 version of "red boy" refuses to be a Daocheng Xueba. He is already a doctor of the Chinese Academy of Sciences and has 52 companies under his name
随机推荐
全网最全的软件测试基础知识整理(新手入门必学)
Skywalking系列学习之告警通知源码分析
最大连续子序列(DAY 77)
[paper]PointLaneNet论文浅析
Inftnews | ggac and China Aerospace ases exclusively produce "China 2065 Collection Edition"
【flask】服务端获取客户端请求的文件
coco test-dev 测试代码
浅浅梳理一下双轴快排(DualPivotQuickSort)
175. 组合两个表(非常简单)
AcWing 2074. 倒计数 模拟
毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了
Best practices of opentelemetry in service grid architecture
【flask】服务端获取客户端的请求头信息
MarqueeView实现滑动展示效果
论构造函数的原型是谁
一体式水利视频监控站 遥测终端视频图像水位水质水量流速监测
What did kubedmin do?
window对象的常见事件
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
Coco test dev test code