当前位置:网站首页>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);
边栏推荐
- Three tier architecture of enterprise network
- 大佬们flink的JDBC SQL Connector现在不支持所有的数据库吗,例如vertica?
- 安装postgis时报找不到“POSTGIS_VERSION”这个函数
- The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout
- [paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation
- 小程序:区域滚动、下拉刷新、上拉加载更多
- C language - character array - string array - '\0' -sizeof-strlen() -printf()
- Object detection: object_ Detection API +ssd target detection model
- C语言:浅谈各种复杂的声明
- LCA 板子
猜你喜欢

Data mining -- Introduction to the basis of association analysis (Part 1)

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

SVG--loading动画

Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes

Svg -- loading animation

2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology

Object detection: object_ Detection API +ssd target detection model

The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout

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

The principle of inverse Fourier transform (IFFT) in signal processing
随机推荐
The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout
Methods of using multiple deformations on an element
Common components of solder pad (2021.4.6)
C语言:结构体简单语法总结
数据集成这个地方的过滤条件该咋写,用的啥语法?sql语法处理bizdate可以不
HCIP BGP
不会就坚持70天吧 数组中第k大的数
How to write the filter conditions of data integration and what syntax to use? SQL syntax processing bizdate can not be
这个报错是什么鬼啊,不影响执行结果,但是在执行sql时一直报错。。。连接maxComputer是使用
Is the array name a pointer
Pointer of pointer???...
店铺排名问题,如何解决?
[kvm] create virtual machine from kickstart file
"Weilai Cup" 2022 Niuke summer multi school training camp 2H
基于STM32和阿里云的环境检测系统设计
信号处理中的反傅里叶变换(IFFT)原理
不会就坚持59天吧 替换单词
HC06 HC05 BT
LCA 板子
优炫数据库有办法查到主集群每天传给备集群的日志量吗?