If you want messages to be consumed with a delay, you can use the message delay scheme of Message Queue for RabbitMQ.
What is a delayed message?
A delayed message is consumed with a specified delay after it is sent by a producer.
Delayed messages are used in the following scenarios:
- A time window between message production and consumption is required. For example, when an order is created on an e-commerce platform, a producer sends a delayed message to the Message Queue for RabbitMQ broker. The message is used to ask a consumer to check whether the order is paid within a required period of time. If the order is unpaid, the consumer closes the order. The Message Queue for RabbitMQ broker delivers the message to the consumer 30 minutes after the broker receives the message. After the consumer receives the message, it checks whether the payment is complete. If the payment is not complete, the consumer closes the order. Otherwise, the consumer ignores the message.
- Messages are used to trigger scheduled tasks. For example, you can use a delayed message to trigger a scheduled task that sends notifications to users.
- The value of the delay for a message must be a non-negative integer. Unit: milliseconds.
- The maximum delay is 86,400,000 milliseconds, which are equal to one day. If the delay for a message is set to a value larger than the allowed maximum value, the message is considered as a normal message.
Comparison between message delay schemes
The following table compares the message delay schemes supported by Message Queue for RabbitMQ and open source RabbitMQ.
|Scheme||Open source RabbitMQ||Message Queue for RabbitMQ|
|Dead-letter exchange and message time-to-live (TTL) of a queue||✔️||✔️|
|Dead-letter exchange and TTL of a message||✔️||✔️|
|Open source delayed message plug-in scheme||✔️||✔️|
Open source delayed message plug-in scheme
To align with open source RabbitMQ, Message Queue for RabbitMQ is compatible with the open source delayed message plug-in scheme and frees you from installing the plug-in. To use the open source delayed message plug-in scheme, perform the following operations:
- Declare an exchange of the
x-delayed-messagetype and set the x-delayed-type extended attribute of the exchange to specify the routing rule. The following sample code provides an example:
Map<String, Object> args = new HashMap<String, Object>(); args.put("x-delayed-type", "direct"); channel.exchangeDeclare("ExchangeName", "x-delayed-message", true, false, args);The following table describes the parameters.
Parameter Description x-delayed-type The type of the exchange. This parameter is used to specify the routing rule. Valid values:
ExchangeName The name of the exchange.Note Make sure that the declared exchange has been created. For more information, see Create an exchange. x-delayed-message Specifies the exchange type to route delayed messages.
- Send a delayed message. Add a header attribute in the key-value format for the message and specify that the message will
be sent to the exchange declared in the previous step. In the key-value pair of this
header attribute, the key is x-delay and the value is milliseconds. The following sample code provides an example:
byte messageBodyBytes = "delayed payload".getBytes("UTF-8"); Map<String, Object> headers = new HashMap<String, Object>(); headers.put("x-delay", 5000); AMQP.BasicProperties.Builder props = new AMQP.BasicProperties.Builder().headers(headers); channel.basicPublish("ExchangeName", "", props.build(), messageBodyBytes);
In this example, the exchange delivers the message to the corresponding queue 5,000 milliseconds after the exchange receives the message.
Why is the actual delay longer than the specified delay?
The consumer client uses the basic.get method in pull mode to consume messages. Message Queue for RabbitMQ messages are stored in clusters. When a consumer is routed to a Message Queue for RabbitMQ broker by using the basic.get method in pull mode, the consumer may not receive messages from other brokers immediately after the specified delay.