当前位置:网站首页>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);
边栏推荐
- HCIP BGP
- 编译与链接
- Basic configuration of BGP - establish peers and route announcements
- Opengauss pre check installation
- 从淘宝,天猫,1688,微店,京东,苏宁,淘特等其他平台一键复制商品到拼多多平台(批量上传宝贝详情接口教程)
- 不会就坚持63天吧 最大的异或
- "Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
- 不会就坚持58天吧 实现前缀树
- %s. %c, character constant, string constant, const char*, pointer array, string array summary
- "Weilai Cup" 2022 Niuke summer multi school training camp 2H
猜你喜欢
The principle of inverse Fourier transform (IFFT) in signal processing
不会就坚持68天吧 狒狒吃香蕉
HC06 HC05 BT
店铺排名问题,如何解决?
不会就坚持66天吧 权重生成随机数
Problems encountered in vscode connection SSH
Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation
Whole house WiFi solution: mesh router networking and ac+ap
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)
不会就坚持62天吧 单词之和
随机推荐
Asp. Net MVC, how can the controller in the folder jump to the controller in the root directory?
The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout
不会就坚持66天吧 权重生成随机数
Is the array name a pointer
BIO、NIO、AIO的区别和原理
Press the missing number of interview question 17.04 | | 260. the number that appears only once (including bit operation knowledge points)
Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
Common components of solder pad (2021.4.6)
C language to achieve three chess game (detailed explanation)
不会就坚持68天吧 狒狒吃香蕉
"Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
有没有大佬帮我看下flink sql连接kafka认证kerberos的参数配置是否有误
Fu Yingna: Yuan universe is the new generation of Internet!
小程序:区域滚动、下拉刷新、上拉加载更多
Svg -- loading animation
[principle] several ways of horizontal penetration
安装postgis时报找不到“POSTGIS_VERSION”这个函数
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
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)
Object detection: object_ Detection API +ssd target detection model