edit-icon download-icon

Send and receive scheduled messages

Last Updated: Oct 09, 2018

Scheduled messages can be consumed by consumers after a specified time stamp, which are used in scenarios where there are time window requirements for message production and consumption, or when messages are used to trigger scheduled tasks.

To learn more about scheduled messages, see Scheduled messages.

Send Scheduled Messages

The sample code for sending scheduled message is as follows:

  1. import com.aliyun.openservices.ons.api.Message;
  2. import com.aliyun.openservices.ons.api.ONSFactory;
  3. import com.aliyun.openservices.ons.api.Producer;
  4. import com.aliyun.openservices.ons.api.PropertyKeyConst;
  5. import com.aliyun.openservices.ons.api.SendResult;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Properties;
  9. public class ProducerDelayTest {
  10. public static void main(String[] args) {
  11. Properties properties = new Properties();
  12. // Alibaba Cloud ID verification, which is created on Alibaba Cloud Management Console
  13. properties.put(PropertyKeyConst.AccessKey, "XXX");
  14. // Alibaba Cloud ID verification, which is created on Alibaba Cloud Management Console
  15. properties.put(PropertyKeyConst.SecretKey, "XXX");
  16. // Set a TCP access domain name (the following uses public cloud production environment as an example)
  17. properties.put(PropertyKeyConst.ONSAddr,
  18. "http://onsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal");
  19. Producer producer = ONSFactory.createProducer(properties);
  20. // Before sending messages, the start method must be called once to start the producer.
  21. producer.start();
  22. Message msg = new Message( //
  23. // The Topic of the message
  24. "Topic",
  25. //Message tag, which is similar to tag in Gmail, and is used to classify messages. Consumers can then set filtering conditions for messages to be filtered in MQ broker.
  26. "Tag",
  27. // Message body, which can be any data in binary format. Consistent serialization and deserialization methods should be consistent between the producer and the consumer.
  28. "Hello MQ".getBytes());
  29. // The setting represents the key business property of the message, so please keep it globally unique.
  30. // You can query a message and resend it through the MQ console when you cannot receive the message properly.
  31. // Note: Message sending and receiving is not affected if you do not configure this setting.
  32. msg.setKey("ORDERID_100");
  33. try {
  34. // Scheduled message delivery, set the specific time stamp for delivery (ms), for example, deliver at 2016-03-07 16:21:00. If set to a time point beyond 40 days, the message delivery will fail.
  35. long timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-03-07 16:21:00").getTime();
  36. msg.setStartDeliverTime(timeStamp);
  37. // Message sending will succeed as long as no exception is thrown.
  38. SendResult sendResult = producer.send(msg);
  39. System.out.println("Message Id:" + sendResult.getMessageId());
  40. }
  41. catch (Exception e) {
  42. // If the message sending fails and a retry is needed, the message can be re-sent or persisted for compensated processing.
  43. System.out.println(new Date() + " Send mq message failed. Topic is:" + msg.getTopic());
  44. e.printStackTrace();
  45. }
  46. // The producer object will be destroyed before exiting the application.
  47. // Note: It's ok if the producer object is not destroyed.
  48. producer.shutdown();
  49. }
  50. }

Subscribe to Scheduled Messages

The subscription of scheduled messages is the same with that of normal messages, see Subscribe to messages.

Thank you! We've received your feedback.