当前位置:网站首页>队列达到最大长度代码实战
队列达到最大长度代码实战
2022-07-27 01:16:00 【一个风轻云淡】
生产者001
/**
* 队列到达最大长度的情况
*/
public class Producer001 {
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);
}
}
}消费者001
/**
* 队列最大长度的普通消费者
*/
public class Consumer001 {
//普通交换机
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);
System.out.println("01接收到消息"+s);
};
channel.basicConsume(normalQueue,true,deliverCallback,consumerTag -> {});
}
}结果:

边栏推荐
猜你喜欢

批量复制宝贝上传提示乱码,如何解决?

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

A math problem cost the chip giant $500million!

Attention should be paid to the first parameter of setTimeout

毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了

Best practices of opentelemetry in service grid architecture

vector 转 svg 方法

cocos小游戏实战-04-碰撞检测与NPC渲染

vs2019 中编译和使用 protobuf 库

185. 部门工资前三高的所有员工(必会)
随机推荐
机器学习【Matplotlib】
[SQL simple question] leetcode 627. change gender
百度云人脸识别
typora详细教程
红宝书第四版的一个错误?
Abbkine AbFluor 488 细胞凋亡检测试剂盒特点及实验建议
Thread.Sleep(0)的作用
我的爬虫笔记(七) 通过爬虫实现blog访问量+1
175. 组合两个表(非常简单)
cocos小游戏实战-05-NPC与角色攻击逻辑
使用 WebSocket 实现一个网页版的聊天室(摸鱼更隐蔽)
2649: 段位计算
2513: Xiao Yong's academic score (common divisor problem)
A math problem cost the chip giant $500million!
CAS deployment and successful login jump address
一道数学题,让芯片巨头亏了5亿美金!
[binary search medium] leetcode 34. find the first and last positions of elements in the sorted array
Complete source code of mall applet project (wechat applet)
数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比
关于url编解码应该选用的函数