This topic introduces the concepts, scenarios, and usage instructions of MQ scheduled messages and delayed messages.
- Scheduled message: A producer sends a message to MQ server, hoping the message is postponed to reach the consumer at a certain time. The message is a scheduled message.
- Delayed message: A producer sends a message to MQ server, hoping the message is delayed in order to reach the consumer at a certain time. The message is a delayed message.
There are some coding differences between scheduled messages and delayed messages, but they have the same effect. The message will not be delivered immediately after it is sent to the MQ server, but will be delivered to the consumer after being delayed for a fixed period of time, which is specified in the attributes of the message.
Scheduled or delayed messages apply to the following scenarios:
- When a time window is required for message production and consumption. For example, in e-commerce transactions, an order is closed after a payment timer times out. A delayed message is sent upon the creation of an order. This message will be delivered to the corresponding consumer in 30 minutes. After receiving this message, the consumer needs to determine whether payment has been made. If the payment is not complete, the order is then closed. Otherwise, the consumer ignores the message.
- To trigger scheduled tasks with messages. For example, sending a notification message to a user at a specified time.
Scheduled messages and delayed messages possess the following differences in terms of codes:
- For a scheduled message, a time point after the message transmitting time should be specified as the message delivery time.
- For a delayed message, a delay duration needs to be specified and the message will be sent after the duration elapses.
- For scheduled/delayed messages, the parameter msg.setStartDeliverTime needs to be set to a time point after the current time stamp (ms). If it is set to a time point before the current time stamp, the message will be immediately delivered to the consumer.
- For scheduled/delayed messages, the parameter msg.setStartDeliverTime can be set to any time point within 40 days (ms). If set to a time point beyond 40 days, the message delivery will fail.
- StartDeliverTime is the time for the server to start delivering the message to the consumer. If there is message accumulation for the consumer, the scheduled or delayed message will be queued after the accumulated messages, and will not be delivered according to the specified time exactly.
- Due to the potential time difference between the client and the server, time difference might also occur betweeen the actual delivery time and the delivery time set by the client.
- The 3-day message storage limit still applies for scheduled or delayed messages.For example, a message is scheduled to be consumed in 5 days. If it is not consumed after 5 days, the message will be deleted on the 8th day.
- Delayed messages are only supported for Java language with TCP access.
Sample code for sending scheduled messages and delayed messages can be found in the following documents: