Example of combining spring boot with rabbitmq to implement delay queue
background
What is a delay queue?
As the name suggests, a delay queue is a queue in which messages entering the queue will be delayed for consumption. In the general queue, once the message enters the queue, it will be consumed by consumers immediately.
Scenario 1: in the order system, a user usually has 30 minutes to pay after placing an order. If the payment is not successful within 30 minutes, the order will be processed. This enables you to use the delay queue to send order information to the delay queue.
Scenario 2: the user wants to remotely control the smart device at home through the mobile phone and work at the specified time. At this time, the user instruction can be sent to the delay queue. When the set time of the instruction is up, the instruction can only be pushed to the device.
What can delay queues do?
Delay queues are mostly used in scenarios where work needs to be delayed. The most common are the following two scenarios:
1. Delay consumption. For example:
2. Delay retry. For example, consumers fail to consume messages from the queue, but want to delay for a period of time and automatically retry.
If the delay queue is not used, we can only do it through a polling scanner. This scheme is neither elegant nor convenient to make a unified service for developers. But with delay queues, we can do it easily.
How?
Don't worry. In the following, we will describe in detail how to use spring boot and rabbitmq to implement delay queue.
The sample code presented in this article has been pushed into the GitHub Repository: https://github.com/Lovelcp/blog-demos/tree/master/spring-boot-rabbitmq-delay-queue