Priority queues, as the name suggests, give priority to messages with higher priority to be consumed.
Points to note for RabbitMQ priority queues:
- Priority queues only take effect when there are insufficient consumers and consumption cannot be done in a timely manner.
- Priority queues are supported only after RabbitMQ version 3.5.
Steps to Use RabbitMQ Priority Queues
- Set the maximum priority of the queue.
- Set the priority of the message.
Set the Maximum Priority of the Queue
When declaring the queue, set the maximum priority of the queue through the queue property (x-max-priority). The maximum value for priority is 255, and it is recommended to set it between 1 and 10. Setting in Golang:
// Queue properties
props := make(map[string]interface{})
// Set the maximum priority of the queue
props["x-max-priority"] = 10
// Declare the queue
q, err := ch.QueueDeclare(
"tizi365.hello", // Queue name
true, // Queue durable
false, // Delete when unused
false, // Exclusive
false, // No-wait
props, // Set queue properties
)
Tip: The setting method in other languages is similar.
Set the Message Priority
Setting in Golang:
err = ch.Publish(
"tizi365", // Exchange
"", // Routing key
false,
false,
amqp.Publishing{
Priority:5, // Set the message priority
DeliveryMode:2, // Message delivery mode, 1 for non-persistent, 2 for persistent
ContentType: "text/plain",
Body: []byte(body),
})