Rabbitmq的优先级队列机制已经过测试,只有在生产者开始在消费者使用之前开始生产者发布消息之前,它才会生效。如何解决这个问题?
**Code snippet**
消费者:
Map<String,Object> args = new HashMap<String,Object>();
args.put("x-max-priority", 10);
channel.queueDeclare(TEST_PRIORITY_QUEUE, true, false, false,args);
//omit... ...
DeliverCallback deliverCallback= (consumerTag, delivery) -> {
try {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println("message="+message);
Thread.sleep(20*1000);
}catch (Exception e) {
e.printStackTrace();
}finally {
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
};
//... ...
制作人:
for (int i = 0; i <20; i++) {
String messagelow = "lowLevelMsg";
channel.basicPublish(TEST_EXCHANGE_direct,
"prikey",
new BasicProperties.Builder().priority(1).build(),
messagelow.getBytes());
}
String messagehigh = "HigherLevelMsg";
channel.basicPublish(TEST_EXCHANGE_direct,
"prikey",
new BasicProperties.Builder().priority(9).build(),
messagehigh.getBytes());