Delivering Scheduled Messages
In DMS for RocketMQ, you can schedule messages to be delivered at any time, with a maximum delay of one year. You can also cancel scheduled messages.
After being sent from producers to DMS for RocketMQ, scheduled messages are delivered to consumers only after a specified point in time.
Before delivering scheduled messages, collect RocketMQ connection information by referring to Collecting Connection Information.
- This function is supported only for instances created on or after March 30, 2022.
- To receive and send scheduled messages, ensure the topic message type is Scheduled before connecting a client to a RocketMQ instance of version 5.x.
Application Scenarios
Scheduled messages can be used in the following scenarios:
- The service logic requires a time window. For example, an e-commerce order is closed if it is not paid within a period of time. When an order is created, a scheduled message is sent and will be delivered to the consumer five minutes later. After receiving the message, the consumer checks whether the order is paid. If the order is not paid, it is closed. If the order is paid, the message is ignored.
- A scheduled task is triggered by a message. For example, a reminder is sent to a user at a specific time.
Note
- The delivery time can be scheduled to up to one year later. If the delay time exceeds one year, the message cannot be delivered.
- If the delivery time is scheduled to a time point earlier than the current timestamp, the message is immediately sent to the consumer.
- Ideally, the difference between the scheduled delivery time and the actual delivery time is smaller than 0.1s. However, if the pressure of scheduled message delivery is too high, flow control will be triggered, and the precision will deteriorate.
- The message delivery order is not ensured for precision of 0.1s. That is, if the difference between the scheduled delivery time of two messages is smaller than 0.1s, they may not be delivered in the order that they were sent.
- Exactly-once delivery is not guaranteed. A scheduled message may be delivered repeatedly.
- The scheduled time is the time when the server starts to deliver a message to a consumer. If messages are stacked on the consumer, the scheduled message is delivered after the stacked messages, and cannot be delivered exactly at the configured time.
- Due to a potential time difference between the client and server, the actual delivery time may be different from the delivery time set by the client. The server time is used.
- Messages are retained for a period (two days by default) after the scheduled delivery time. For example, if a scheduled message is not retrieved in five days as scheduled, it is deleted on the seventh day.
- Scheduled messages occupy about three times the storage space of normal messages. If you use a large number of scheduled messages, pay attention to the storage space usage.
Preparing the Environment
You can connect open-source Java clients to DMS for RocketMQ. The recommended Java client version is 4.9.8.
- Using Maven
<dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>4.9.8</version> </dependency>
- Downloading the dependency.
Delivering Scheduled Messages
The code for delivering a scheduled message is as follows:
import java.nio.charset.StandardCharsets; import java.time.Instant; import org.apache.rocketmq.client.exception.MQBrokerException; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.client.producer.DefaultMQProducer; import org.apache.rocketmq.client.producer.SendResult; import org.apache.rocketmq.common.UtilAll; import org.apache.rocketmq.common.message.Message; import org.apache.rocketmq.remoting.exception.RemotingException; public class ScheduledMessageProducer1 { public static final String TOPIC_NAME = "ScheduledTopic"; public static void main(String[] args) throws MQClientException, InterruptedException, MQBrokerException, RemotingException { DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName"); // Enter the address. producer.setNamesrvAddr("192.168.0.1:8100"); //producer.setUseTLS(true); // Add this line if SSL has been enabled during instance creation. producer.start(); // Timestamp for scheduled delivery. The message will be delivered in 10 seconds. final long deliverTimestamp = Instant.now().plusSeconds(10).toEpochMilli(); // Create a message object. Message msg = new Message(TOPIC_NAME, "TagA", "KEY", "scheduled message".getBytes(StandardCharsets.UTF_8)); // Set the timestamp for scheduled message delivery. msg.putUserProperty("__STARTDELIVERTIME", String.valueOf(deliverTimestamp)); // Send the message. The message will be delivered in 10 seconds. SendResult sendResult = producer.send(msg); // Print the delivery result and estimated delivery time. System.out.printf("%s %s%n", sendResult, UtilAll.timeMillisToHumanString2(deliverTimestamp)); producer.shutdown(); } }
Canceling a Scheduled Message
The code for canceling a scheduled message is as follows:
import java.nio.charset.StandardCharsets; import java.time.Instant; import org.apache.rocketmq.client.exception.MQBrokerException; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.client.producer.DefaultMQProducer; import org.apache.rocketmq.client.producer.SendResult; import org.apache.rocketmq.common.UtilAll; import org.apache.rocketmq.common.message.Message; import org.apache.rocketmq.remoting.exception.RemotingException; public class ScheduledMessageProducer1 { public static final String TOPIC_NAME = "ScheduledTopic"; public static void main(String[] args) throws MQClientException, InterruptedException, MQBrokerException, RemotingException { DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName"); // Enter the address. producer.setNamesrvAddr("192.168.0.1:8100"); //producer.setUseTLS(true); // Add this line if SSL has been enabled during instance creation. producer.start(); // Timestamp for scheduled delivery. The message will be delivered in 10 seconds. final long deliverTimestamp = Instant.now().plusSeconds(10).toEpochMilli(); // Create a message object. Message msg = new Message(TOPIC_NAME, "TagA", "KEY", "scheduled message".getBytes(StandardCharsets.UTF_8)); // Set the timestamp for scheduled message delivery. msg.putUserProperty("__STARTDELIVERTIME", String.valueOf(deliverTimestamp)); // Send the message. The message will be delivered in 10 seconds. SendResult sendResult = producer.send(msg); // Print the delivery result and estimated delivery time. System.out.printf("%s %s%n", sendResult, UtilAll.timeMillisToHumanString2(deliverTimestamp)); // ====== Sending the cancellation logic ====== // Create an object for the cancellation. Message cancelMsg = new Message(TOPIC_NAME, "", "", "cancel".getBytes(StandardCharsets.UTF_8)); // Set the timestamp of the message to be canceled. This timestamp must be the same as that of the scheduled delivery. cancelMsg.putUserProperty("__STARTDELIVERTIME", String.valueOf(deliverTimestamp)); // Set the unique ID (UNIQUE_KEY) of the message to be canceled. The ID can be obtained from the message sending result. cancelMsg.putUserProperty("__CANCEL_SCHEDULED_MSG", sendResult.getMsgId()); // Send the cancellation message before the scheduled delivery time. SendResult cancelSendResult = producer.send(cancelMsg, sendResult.getMessageQueue()); System.out.printf("cancel %s%n", cancelSendResult); producer.shutdown(); }}
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot