gpt4 book ai didi

RabbitMQ 消息优先级队列不工作

转载 作者:行者123 更新时间:2023-12-05 04:15:40 27 4
gpt4 key购买 nike

我运行优先级队列 java program在我的 eclipse 中,我遇到了一个问题,我第一次得到正确答案。还有一次我在队列中添加了一条消息,但这次我得到了不同的结果。

public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
Connection conn = factory.newConnection();
Channel ch = conn.createChannel();
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-max-priority", 10);
ch.queueDeclare(QUEUE_UPDATE, true, false, false, args);

publish(ch, 141);
publish(ch, 250);

final CountDownLatch latch = new CountDownLatch(2);
ch.basicConsume(QUEUE_UPDATE, true, new DefaultConsumer(ch) {
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
System.out.println("Received " + new String(body));
latch.countDown();
}
});

latch.await();
conn.close();
System.out.println("Finished");
}

private static void publish(Channel ch, int priority) throws Exception {
BasicProperties props = MessageProperties.PERSISTENT_BASIC.builder().priority(priority).build();
String body = QUEUE_UPDATE + " message with priority " + priority ;
ch.basicPublish("", QUEUE_UPDATE, props, body.getBytes());
}

正确的输出:

Received update-queue message with priority 250
Received update-queue message with priority 141
Finished

添加了一条队列消息

         publish(ch, 141);    
publish(ch, 250);
publish(ch, 110); // newly added

预期输出

Received update-queue message with priority 250
Received update-queue message with priority 141
Received update-queue message with priority 110
Finished

实际输出

Received update-queue message with priority 141
Received update-queue message with priority 250
Received update-queue message with priority 110
Finished

怎么会变成这样?我做错了什么吗?

最佳答案

我遇到了同样的问题。对我有用的是定义由 consumer prefetch 定义的限制。 ,例如 channel.basicQos(1);。如果您不设置此限制,消息将在到达代理时传送给消费者,因此它们永远不会使用优先级进行排序。

当您设置下限时,代理不会一次发送超过此限制的消息,因此会在传递前对消息进行排序。

关于RabbitMQ 消息优先级队列不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31355228/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com