RabbitMQ消息优先级队列不工作
我在eclipse中运行优先级队列,第一次得到正确答案时遇到了一个问题。另一次,我在队列中又添加了一条消息,但这次我得到了不同的结果RabbitMQ消息优先级队列不工作,rabbitmq,spring-rabbit,Rabbitmq,Spring Rabbit,我在eclipse中运行优先级队列,第一次得到正确答案时遇到了一个问题。另一次,我在队列中又添加了一条消息,但这次我得到了不同的结果 public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); Connection conn = factory.newConnection(); Channel
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());
}
又添加了一条队列消息
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
怎么会这样?我做错了什么吗?我遇到了同样的问题。对我有效的是定义一个由定义的限制,例如
channel.basicQos(1)代码>。
如果不设置此限制,消息将在到达代理时传递给消费者,因此它们永远不会使用优先级进行排序
当您设置一个下限时,代理一次发送的邮件不会超过此限制,因此在发送前会对邮件进行排序。删除队列并尝试使用以下命令:args.put(“x-max-priority”,250)代码>对于偶然发现这个问题的C#开发人员,请参阅。简而言之,“推送api”不尊重优先级。非常感谢Toresan!它的工作100%,如果我可以,我会标记为正确的答案!我试过了,但没有成功,@GeorgeAnisimov你是怎么做到的?你添加了什么代码
Received update-queue message with priority 141
Received update-queue message with priority 250
Received update-queue message with priority 110
Finished