0
0
RabbitmqHow-ToBeginner · 4 min read

How to Implement Priority Queue in RabbitMQ

To implement a priority queue in RabbitMQ, declare a queue with the x-max-priority argument set to the maximum priority value. Then, when publishing messages, set the priority property on each message to control its processing order.
📐

Syntax

To create a priority queue in RabbitMQ, you declare a queue with the x-max-priority argument. This argument defines the highest priority number allowed. When sending messages, you set the priority property on each message to a value between 0 and the max priority.

  • x-max-priority: Queue argument to enable priority and set max priority.
  • priority: Message property to assign priority level.
java
channel.queueDeclare("priority_queue", true, false, false, Map.of("x-max-priority", 10));

AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
    .priority(5)
    .build();

channel.basicPublish("", "priority_queue", props, "Message with priority 5".getBytes());
💻

Example

This example shows how to declare a priority queue and publish messages with different priorities using RabbitMQ Java client. Messages with higher priority (higher number) will be delivered before lower priority messages.

java
import com.rabbitmq.client.*;
import java.util.Map;

public class PriorityQueueExample {
    private final static String QUEUE_NAME = "priority_queue";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            // Declare queue with max priority 10
            channel.queueDeclare(QUEUE_NAME, true, false, false, Map.of("x-max-priority", 10));

            // Publish messages with different priorities
            for (int i = 0; i < 5; i++) {
                int priority = i * 2; // 0, 2, 4, 6, 8
                AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
                        .priority(priority)
                        .build();
                String message = "Message with priority " + priority;
                channel.basicPublish("", QUEUE_NAME, props, message.getBytes());
                System.out.println("Sent: " + message);
            }
        }
    }
}
Output
Sent: Message with priority 0 Sent: Message with priority 2 Sent: Message with priority 4 Sent: Message with priority 6 Sent: Message with priority 8
⚠️

Common Pitfalls

  • Not setting x-max-priority on the queue means priority is ignored.
  • Using priorities outside the range 0 to max priority can cause unexpected behavior.
  • Assuming priority guarantees strict ordering; it only influences delivery order, not absolute order.
  • Not setting the priority property on messages defaults them to lowest priority.
java
/* Wrong: Queue declared without x-max-priority, priority ignored */
channel.queueDeclare("no_priority_queue", true, false, false, null);
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
    .priority(5)
    .build();
channel.basicPublish("", "no_priority_queue", props, "Message".getBytes());

/* Right: Declare queue with x-max-priority to enable priority */
channel.queueDeclare("priority_queue", true, false, false, Map.of("x-max-priority", 10));
📊

Quick Reference

Summary tips for RabbitMQ priority queues:

  • Declare queue with x-max-priority argument (e.g., 10).
  • Set message priority property between 0 and max priority.
  • Higher priority messages are delivered first.
  • Priority is a hint, not a strict guarantee of order.
  • Use priority queues only when message ordering by importance is needed.

Key Takeaways

Declare queues with the x-max-priority argument to enable priority support.
Set the priority property on messages to control their delivery order.
Priority values must be within the range 0 to the max priority set on the queue.
Priority queues influence message order but do not guarantee strict ordering.
Without x-max-priority, message priority settings are ignored.