edit-icon download-icon

Send and receive scheduled messages

Last Updated: Jun 19, 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.

Note: For information on TCP access point domain names, see TCP access instructions.

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

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.