? ? ? ? 优先级队列,顾名思义就是说该队列中的消息有优先级,优先级越高的消息越先被消费。如果当前队列中只有一条消息,那么优先级就没有存在的意义,即消费速度大于生产速度的时候优先级队列就没有存在的价值。
? ? ? ? 可以通过设置队列的x-max-priority来设置队列的最大优先级(0-255,超过255之后声明队列会报错),然后发送消息的时候通过priority设置消息的优先级,最小优先级为0,最大优先级不能超过队列的最大优先级,否则不生效。
Map<String, Object> map = new HashMap<>();
map.put("x-max-priority", 255);
channel.queueDeclare(queueName, true, false, false, map);
channel.queueBind(queueName,exchangeName,"pri.#");
AMQP.BasicProperties.Builder builder1 = new AMQP.BasicProperties.Builder();
builder1.priority(5);
channel.basicPublish(exchangeName,"pri.message",builder1.build(),"我的优先级5".getBytes());
AMQP.BasicProperties.Builder builder2 = new AMQP.BasicProperties.Builder();
builder2.priority(20);
channel.basicPublish(exchangeName,"pri.message",builder2.build(),"我的优先级20".getBytes());
AMQP.BasicProperties.Builder builder3 = new AMQP.BasicProperties.Builder();
builder3.priority(10);
channel.basicPublish(exchangeName,"pri.message",builder3.build(),"我的优先级10".getBytes());
|