This topic describes the terms, scenarios, and usage notes of scheduled messages and delayed messages in ApsaraMQ for RocketMQ.
Terms
- Scheduled messages: Scheduled messages are messages that are delivered by ApsaraMQ for RocketMQ brokers to consumers at a specified point in time.
- Delayed messages: Delayed messages are messages that are delivered by ApsaraMQ for RocketMQ brokers to consumers after a specified period of time.
Coding differences exist between scheduled messages and delayed messages. However, scheduled messages and delayed messages achieve the same effect. After messages of the two types are sent to ApsaraMQ for RocketMQ brokers, the messages are delayed for a specific period of time before they are sent to consumers.
Scenarios
Scheduled messages and delayed messages can be used in the following scenarios:
- A time window is required between message production and consumption. For example, when a consumer creates an order that times out in 30 minutes in an e-commerce transaction, a delayed message is immediately sent to the Message Queue for RabbitMQ broker. The Message Queue for RabbitMQ broker delivers the message to the consumer after 30 minutes. When receiving the message, the consumer needs to check whether the payment is complete. If the payment is not complete, the consumer closes the order. If the payment is complete, the consumer ignores the message.
- Use messages to trigger scheduled tasks, such as sending a notification to a user at a specific point in time.
Usage
Scheduled messages and delayed messages have the following differences in terms of code:
- If you want to send a scheduled message, specify a point in time that is later than the point in time when the message is sent by a producer. The broker sends the message to consumers at the specified point in time.
- If you want to send a delayed message, specify a period of time that starts from the point in time when the message is sent by a producer. The broker sends the message to consumers after the specified period of time is elapsed.
Setting rules for scheduled time and delayed time
- The
msg.setStartDeliverTime
parameter for a scheduled message or delayed message must be set to a point in time or a period of time after the current timestamp. Unit: milliseconds. If the scheduled time is earlier than the current time, the message is immediately delivered to the consumer. - The
msg.setStartDeliverTime
parameter for a scheduled message or delayed message can be set to a point in time or a period of time within 40 days. Unit: milliseconds. If the specified point in time or period of time exceeds 40 days, the message cannot be sent.
Usage notes
Message type consistency
The type of message that a topic can be used to send must be the same as the message type that you select when you create the topic. For example, if you select Scheduled/Delayed Message for the Message Type parameter when you create a topic in the ApsaraMQ for RocketMQ console. The topic can be used to send only scheduled messages and delayed messages.
Latency for delivering scheduled messages and delayed messages
- A latency of 1 to 2 seconds may occur when you deliver scheduled messages.
- The
StartDeliverTime
parameter specifies the time when the ApsaraMQ for RocketMQ broker starts to send messages to the consumer. If accumulated messages exist, scheduled messages or delayed messages are queued after the accumulated messages and cannot be sent to the consumer at the specified time. - Due to the potential time difference between the client and the broker, the actual delivery time may be different from the delivery time specified on the client.
- Scheduled messages and delayed messages can be retained for up to three days in a ApsaraMQ for RocketMQ broker.
For example, a message is scheduled to be consumed in 5 days. If the message is not consumed after 5 days, the message is deleted on the eighth day.
TCP sample code
For information about the sample code for sending scheduled messages and delayed messages over the TCP protocol, see the following documentation:
HTTP sample code
For information about the sample code for sending scheduled messages and delayed messages over the HTTP protocol, see the following documentation:
- Java: Send and subscribe to scheduled messages and delayed messages
- Go: Send and subscribe to scheduled messages and delayed messages
- Python: Send and subscribe to scheduled messages and delayed messages
- Node.js: Send and subscribe to scheduled messages and delayed messages
- PHP: Send and subscribe to scheduled messages and delayed messages
- C#: Send and subscribe to scheduled messages and delayed messages
- C++: Send and subscribe to scheduled messages and delayed messages