Are queue priorities supported?

更新时间:
复制 MD 格式

Yes. ApsaraMQ for RabbitMQ supports priority queues on dedicated instances. Priority queues deliver messages based on their priority level rather than in FIFO order, so higher-priority messages are consumed first.

Prerequisites

Before you begin, make sure that you have:

  • A dedicated instance of ApsaraMQ for RabbitMQ (shared instances do not support priority queues)

  • The priority queue feature enabled on your instance. To enable it, submit a ticket.

Declare a priority queue

Declare a priority queue from your client by setting the x-max-priority argument. The recommended value is a positive integer from 1 to 10. This value defines the maximum priority level that the queue supports. Use only as many levels as your use case requires, because each level adds memory and CPU overhead.

The following Java example declares a priority queue with a maximum priority of 10:

// Declare a priority queue with maximum priority 10
Map<String, Object> args = new HashMap<>();
args.put("x-max-priority", 10);
channel.queueDeclare("my-priority-queue", true, false, false, args);

To publish a message with a specific priority, set the priority property. Larger values indicate higher priority:

// Publish a message with priority 5
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
    .priority(5)
    .build();
channel.basicPublish("", "my-priority-queue", props, "message body".getBytes());

How priority queues work

In a standard queue, messages are delivered in arrival order (FIFO). A priority queue reorders messages by priority before delivery:

MessageArrival orderPriorityDelivery order
A113
B231
C322

Messages B, C, and A are delivered in priority order (3, 2, 1) instead of arrival order.

Note: Priority ordering takes effect only when messages accumulate in the queue. If consumers process messages faster than publishers produce them, messages are delivered immediately without prioritization. To allow prioritization, set a consumer prefetch limit (basic.qos) in manual acknowledgment mode so that messages queue up before delivery.