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

边栏推荐
- [SQL简单题] LeetCode 627. 变更性别
- Alibaba cloud technology expert Yang Zeqiang: Construction of observability on elastic computing cloud
- 最大连续子序列(DAY 77)
- 关于url编解码应该选用的函数
- 196. Delete duplicate email addresses
- 【flask】服务端获取客户端请求的文件
- vs2019 中编译和使用 protobuf 库
- Use the most primitive method to manually implement the common 20 array methods
- 水仙花数(DAY 78)
- Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
猜你喜欢

论构造函数的原型是谁

身家破亿!86版「红孩儿」拒绝出道成学霸,已是中科院博士,名下52家公司

vector 转 svg 方法

185. 部门工资前三高的所有员工(必会)

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

185. All employees with the top three highest wages in the Department (mandatory)

How to use devaxpress WPF to create the first MVVM application in winui?

仿知乎论坛社区社交微信小程序

QT编译出来的exe以管理员权限启动

1.28亿美元!芬兰量子计算公司IQM获世界基金支持
随机推荐
The most complete basic knowledge of software testing in the whole network (a must for beginners)
积分发放带给商家的两个帮助
Inftnews | ggac and China Aerospace ases exclusively produce "China 2065 Collection Edition"
cocos小游戏实战-04-碰撞检测与NPC渲染
用最原始的方法纯手工实现常见的 20 个数组方法
HCIP第十三天笔记
185. All employees with the top three highest wages in the Department (mandatory)
Redis四大特殊数据类型的学习和理解
身家破亿!86版「红孩儿」拒绝出道成学霸,已是中科院博士,名下52家公司
智能指针shared_ptr、unique_ptr、weak_ptr
B-树的应用以及添加和删除操作
177. The nth highest salary (simple)
水仙花数(DAY 78)
Worthington果胶酶的特性及测定方案
Ten thousand words long text, take you to understand the kubernetes network model
Abbkine AbFluor 488 细胞凋亡检测试剂盒特点及实验建议
Shell (38): SSH port forwarding
食物链(DAY 79)
关于url编解码应该选用的函数
MarqueeView实现滑动展示效果