How to Implement Delayed and Priority Queues in Spring Boot with RabbitMQ
This tutorial explains how to create delayed and priority queues in RabbitMQ using Spring Boot, covering dead‑letter queue configuration, TTL settings, priority parameters, and provides complete Java code examples for sending and consuming prioritized messages.
Delayed Queue
A delayed queue means that a message becomes visible to consumers only after a specified time has passed.
The implementation relies on the dead‑letter queue technique described in the previous article "Springboot Integration with RabbitMQ Dead‑Letter Queue".
Steps:
Create a queue (e.g., bs-queue ) and configure a dead‑letter exchange (and optionally a dead‑letter routing key). Set the x-message-ttl argument to the desired delay in milliseconds.
Publish messages to bs-queue . Because the consumer listens to the dead‑letter queue, the messages are automatically forwarded to the dead‑letter queue after the TTL expires.
Consume messages from the dead‑letter queue.
For detailed configuration, refer to the article "Springboot Integration with RabbitMQ Dead‑Letter Queue".
Priority Queue
Priority queues are supported from RabbitMQ 3.5.0 onward. Messages with higher priority are consumed before lower‑priority ones.
The queue priority is set via the x-max-priority argument.
Create a direct exchange named priority-exchange and bind a priority-queue to it.
Bind the priority-queue to the exchange.
Set x-max-priority to 100 (the allowed range is 1‑255; using a small range such as 1‑10 is recommended to avoid excessive Erlang process and CPU usage).
Demo code for sending messages with a priority:
<code>@GetMapping("/sendPriority")
public Object sendPriority(String msg, Integer priority) {
ms.sendPriorityQueue(msg, priority);
return "success";
}
public void sendPriorityQueue(String msg, Integer priority) {
logger.info("准备发送消息:{}", msg);
Message message = MessageBuilder.withBody(msg.getBytes())
.setPriority(priority).build();
rabbitTemplate.convertAndSend("priority-exchange", "pe.msg", message);
}
</code>Sending four messages with different priorities:
<code>// First message
msg=第一条消息&priority=2
// Second message
msg=第二条消息&priority=10
// Third message
msg=第三条消息&priority=1
// Fourth message
msg=第四条消息&priority=7
</code>Consumer code:
<code>@RabbitListener(queues = {"priority-queue"})
@RabbitHandler
public void listenerPriority(Message message, Channel channel) {
System.out.println("接受到消息.....income");
byte[] body = message.getBody();
MessageProperties mps = message.getMessageProperties();
String content = new String(body, Charset.forName("UTF-8"));
try {
System.out.println("接受到消息来自交换机: 【" + mps.getReceivedExchange() + "】, 队列:【" + mps.getConsumerQueue() + "】:\n内容: " + content);
channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
} catch (Exception e) {
e.printStackTrace();
try {
channel.basicReject(mps.getDeliveryTag(), false);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
</code>Running the service shows that messages are consumed according to the set priority order.
If a message’s priority exceeds the queue’s maximum (e.g., 101 or >255), RabbitMQ treats it as the maximum priority, which can lead to unordered consumption.
Messages without a priority property are treated as if their priority were 0. Messages with a priority which is higher than the queue's maximum are treated as if they were published with the maximum priority.
Testing with priorities above 100 resulted in out‑of‑order processing, confirming the above behavior.
End of tutorial.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.