tencent cloud

Scheduled Message and Delayed Message
Last updated: 2025-07-15 17:46:16
Scheduled Message and Delayed Message
Last updated: 2025-07-15 17:46:16
This document primarily describes the concepts and usage of scheduled messages and delayed messages in TDMQ for RocketMQ.

Concepts

Scheduled message: In actual business, after a message is sent to the server, the consumer is expected to receive it at a later time point rather than immediately. This type of message is called scheduled message.
Delayed message: In actual business, after a message is sent to the server, the consumer is expected to receive it after a period of time rather than immediately. This type of message is called delayed message.
Actually, the delayed message can be regarded as a special type of scheduled message, which is essentially the same thing.

Instructions

Apache RocketMQ does not provide an API for you to freely set the delay time. In order to ensure compatibility with the open-source RocketMQ client, TDMQ for RocketMQ has designed a method to specify the message sending time by adding the property key-value pair to the message. You only need to add the __STARTDELIVERTIME property value to the property of the message that needs to be sent at a scheduled time (within 40 days). For delayed messages, you can first calculate the time point for scheduled sending and then send them as scheduled messages.
A code sample is given below to show how to use scheduled messages and delayed messages in TDMQ for RocketMQ. You can also view the complete code sample >>.
Delayed messages first calculate the scheduled sending time via System.currentTimeMillis() + delayTime, then send them as scheduled messages.
4.X Client
5.X Client
Message msg = new Message("test-topic", ("message content").getBytes(StandardCharsets.UTF_8));

// Set the message to be sent after 10 seconds
long delayTime = System.currentTimeMillis() + 10000;
// Set __STARTDELIVERTIME into the property of msg.
msg.putUserProperty("__STARTDELIVERTIME", String.valueOf(delayTime));

SendResult result = producer.send(msg);
System.out.println("Send delay message: " + result);
Duration messageDelayTime = Duration.ofSeconds(10);
final Message message = provider.newMessageBuilder()
// Set topic for the current message.# LF#.setTopic(topic)
// Message secondary classifier of message besides topic.# LF#.setTag(tag)
// Key(s) of the message, another way to mark message besides message id.# LF#.setKeys("yourMessageKey-3ee439f945d7")
// Set expected delivery timestamp of message.
.setDeliveryTimestamp(System.currentTimeMillis() + messageDelayTime.toMillis())
.setBody(body)
.build();

Use Restrictions

When using delayed messages, make sure that the time on the client is in sync with the time on the server (UTC+8 Beijing time in all regions). Otherwise, there will be a time difference.
There is a precision deviation of about one second for scheduled and delayed messages.
The time ranges of scheduled and delayed messages varies based on different cluster specifications. For details, see Product Series.
When using scheduled messages, you need to set a time point after the current time. Otherwise, the message will be sent to the consumer immediately.

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback