当前位置:网站首页>队列达到最大长度代码实战
队列达到最大长度代码实战
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 -> {});
}
}结果:

边栏推荐
- 2649: 段位计算
- 正方形数组的数目(DAY 81)
- QT编译出来的exe以管理员权限启动
- Go to export excel form
- Hcip day 14 notes
- 围圈报数(北理工机试题)(DAY 83)
- HCIP第十四天笔记
- [binary search simple question] leetcode 35. search insertion position, 69. Square root of X, 367. Effective complete square, 441. Arrange coins
- 软件测试面试常见问题及答案(发散思维、接口、性能、概念、)
- After two years of graduation, I switched to software testing and got 12k+, and my dream of not taking the postgraduate entrance examination with a monthly salary of more than 10000 was realized
猜你喜欢

Integrated water conservancy video monitoring station telemetry terminal video image water level water quality water quantity flow velocity monitoring

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

Use the most primitive method to manually implement the common 20 array methods

论构造函数的原型是谁

易灵思T35 FPGA驱动LVDS显示屏

如何使用DevExpress WPF在WinUI中创建第一个MVVM应用程序?

毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了
The most complete basic knowledge of software testing in the whole network (a must for beginners)

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

关于url编解码应该选用的函数
随机推荐
如何白嫖最新版BurpSuite Pro
Go to export excel form
Call jshaman's Web API interface to realize JS code encryption.
vector 转 svg 方法
身家破亿!86版「红孩儿」拒绝出道成学霸,已是中科院博士,名下52家公司
从ACL 2022 Onsite经历看NLP热点
B-树的应用以及添加和删除操作
185. 部门工资前三高的所有员工(必会)
哈希表与一致性哈希的原理理解以及应用
用最原始的方法纯手工实现常见的 20 个数组方法
一个测试类了解BeanUtils.copyProperties
杀毒软件 clamav 的安装和使用
175. 组合两个表(非常简单)
一道数学题,让芯片巨头亏了5亿美金!
How to use devaxpress WPF to create the first MVVM application in winui?
Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
[动态规划中等题] LeetCode 198. 打家劫舍 740. 删除并获得点数
商城小程序项目完整源码(微信小程序)
Use the most primitive method to manually implement the common 20 array methods
[SQL simple question] leetcode 627. change gender