当前位置:网站首页>12.优先级队列和惰性队列
12.优先级队列和惰性队列
2022-07-29 04:14:00 【Machoul】
优先级队列
如何添加优先级

- 选择
Maximum priority,指定优先级的数值,设定范围为0-255,如果值为10,那么就是0-10,最大不能超过255
代码形式
Map<String,Object> params = new HashMap<>();
params.put("x-max-priority",10);
channel.queueDeclare("hello",true,false,false,params);
案例演示
生产者代码
/** * 优先级队列 生产者 */
public class PriorityProducer {
public static void main(String[] args) throws IOException {
Channel channel = RabbitUtil.getChannel();
AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().priority(5).build();
for (int i = 0; i < 10; i++) {
String message = "info"+ i;
if (i==5){
channel.basicPublish("","hello",properties,message.getBytes(StandardCharsets.UTF_8));
}else {
channel.basicPublish("","hello",null,message.getBytes(StandardCharsets.UTF_8));
}
}
System.out.println("发送消息完成");
}
}
消费者代码
/** * 优先级队列 消费者 */
public class PriorityConsumer {
public static void main(String[] args) throws IOException {
Channel channel = RabbitUtil.getChannel();
Map<String,Object> params = new HashMap<>();
params.put("x-max-priority",10);
channel.queueDeclare("hello",true,false,false,params);
System.out.println("消费者启动等待消费...");
DeliverCallback deliverCallback = (consumerTag, message) -> {
String received = new String( message.getBody());
System.out.println("接收到消息:"+received);
};
channel.basicConsume("hello",true,deliverCallback,consumerTag -> {
System.out.println("消费者无法消费消息时调用,如队列被删除");
});
}
}
结果
- 先启动生产者,然后启动消费者,发现
info5被最先消费 - 如果先启动消费者,再启动生产者,由于处理速度足够快,队列中没有消息堆积,所以按照顺序执行
惰性队列
惰性队列会尽可能的将消息存入磁盘中,而在消费者消费到相应的消息时才会被加载到内存中,它的一个重要的设计目标是能够支持更长的队列,即支持更多的消息存储
当消息由于各种各样的原因而致使长时间不能消费消息造成堆积时,惰性队列就很有必要了
队列具备两种模式:default和lazy。可以使用x-queue-mode来设置队列的模式,取值为default和lazy。
惰性队列的声明
Map<String,Object> params = new HashMap();
params.put("x-queue-mode","lazy");
channel.queueDeclare("hello",false,false,false,params);
边栏推荐
- openFeign异步调用问题
- mmdetection初步使用
- Whole house WiFi solution: mesh router networking and ac+ap
- LCA board
- Pointer of pointer???...
- How to solve the problem of store ranking?
- opengauss预检查安装
- Interview notes of a company
- pat A1041 Be Unique
- 2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology
猜你喜欢

Svg -- loading animation

Const char* and char*, string constants

不会就坚持60天吧 神奇的字典

RMAN do not mark expired backups

通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值

Basic configuration of BGP - establish peers and route announcements

不会就坚持59天吧 替换单词

Mmdetection preliminary use

Fu Yingna: Yuan universe is the new generation of Internet!

不会就坚持67天吧 平方根
随机推荐
不会就坚持66天吧 权重生成随机数
The function "postgis_version" cannot be found when installing PostGIS
MPU6050
Do you have a boss to help me check whether the parameter configuration of the Flink SQL connection Kafka authentication Kerberos is wrong
Note: restframe work records many to one tables, how to serialize in that table (reverse query)
Pointer variables -printf%d and%p meaning
Problems encountered in vscode connection SSH
"Weilai Cup" 2022 Niuke summer multi school training camp 2H
Cad2020 introductory learning (2021.4.13)
Multi rotor six axis hardware selection
Some problems about pointers
The return value of the function is the attention of the pointer, the local variables inside the static limit sub function, and how the pointer to the array represents the array elements
Mmdetection preliminary use
“蔚来杯“2022牛客暑期多校训练营1 J Serval and Essay(启发式合并)
C语言:联合体知识点总结
Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)
View partition table format
淘宝商品详情接口(商品详情页面数据接口)
Machine vision Series 2: vs DLL debugging
不会就坚持65天吧 只出现一次的数字