Updated on 2025-11-20 GMT+08:00

Configuring a RabbitMQ Priority Queue

A priority queue is a special queue structure compared with the conventional first in first out (FIFO) rule. It features message consumption in priority-based order.

Priority queues are available in RabbitMQ 3.x.x and AMQP-0-9-1.

Notes and Constraints

  • In RabbitMQ 3.x.x, the priority value range is 0 to 255.
  • In RabbitMQ AMQP-0-9-1, the priority value range is 0 to 9.

Configuring a Priority Queue

  1. In queue declaration, set the x-max-priority parameter (for example, x-max-priority = 5). This parameter specifies the maximum priority supported by a queue. A larger value indicates a higher priority.
  2. In message sending, the priority attribute specifies the priority. A larger value indicates a higher priority.
  3. (Optional) Divide the queue into multiple sub-queues. Messages are processed in descending order of priority. Sample code:

    String queueName = "queue"
    channel.queueDeclare(queueName, true, false, false, args);
    final AMQP.BasicProperties.Builder basicProps = new AMQP.BasicProperties().builder();
    basicProps.priority(5);
    channel.basicPublish("", queueName, basicProps.build(), "test".getBytes())