当前位置:网站首页>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);
边栏推荐
- The pit I walked through: the first ad Sketchpad
- C语言:浅谈各种复杂的声明
- 不会就坚持66天吧 权重生成随机数
- 不会就坚持70天吧 数组中第k大的数
- Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes
- RMAN do not mark expired backups
- Some problems about pointers
- LCA board
- How to solve the problem of store ranking?
- [kvm] common commands
猜你喜欢

Summary on the thought of double pointer

全屋WiFi方案:Mesh路由器组网和AC+AP

Basic configuration of BGP - establish peers and route announcements

Machine vision Series 2: vs DLL debugging
![[deep learning CPU (part outside) - virtual memory]](/img/f7/4c72d583456f6f68c52424602ff5d9.png)
[deep learning CPU (part outside) - virtual memory]

Locally call tensorboard and Jupiter notebook on the server (using mobaxterm)

Applet: Area scrolling, pull-down refresh, pull-up load more

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

不会就坚持61天吧 最短的单词编码

Fu Yingna: Yuan universe is the new generation of Internet!
随机推荐
Why do I delete the original record (OP d) and then add a new one in Kafka when I update MySQL data
First knowledge of C language (3)
这个报错是什么鬼啊,不影响执行结果,但是在执行sql时一直报错。。。连接maxComputer是使用
What the hell is this error? It doesn't affect the execution result, but it always reports errors when executing SQL... Connecting maxcomputer uses
Function pointer and callback function
C declaration and initialization and assignment
不会就坚持67天吧 平方根
openFeign异步调用问题
不会就坚持65天吧 只出现一次的数字
不会就坚持64天吧 查找插入位置
Pointer of pointer???...
"Weilai Cup" 2022 Niuke summer multi school training camp 2H
Some problems about pointers
有没有大佬帮我看下flink sql连接kafka认证kerberos的参数配置是否有误
Note: restframe work records many to one tables, how to serialize in that table (reverse query)
开课!看smardaten如何分解复杂业务场景
Value transmission and address transmission of C language, pointer of pointer
Taobao product details interface (product details page data interface)
数据源是SQL server ,我要配置日期字段 updateDate 最后两天日期的增量数据,做增
不会就坚持60天吧 神奇的字典