All Products
Search
Document Center

Send and receive scheduled messages

Last Updated: Sep 13, 2019

Scheduled messages are consumed after a specified timestamp. These messages are sent when a time window is required between message production and consumption, or when tasks need to be triggered at a scheduled time.

For information about the concept of scheduled messages and use precautions for these messages, see Scheduled messages.

Send scheduled messages

The sample code for sending scheduled messages 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 AccessKeyId you created in the Alibaba Cloud console for identity authentication.
  13. properties.put(PropertyKeyConst.AccessKey, "XXX");
  14. // The AccessKeySecret you created in the Alibaba Cloud console for identity authentication.
  15. properties.put(PropertyKeyConst.SecretKey, "XXX");
  16. // Set the TCP endpoint: Go to the **Instances** page in the MQ console, and view the endpoint in the **Endpoint Information** area.
  17. properties.put(PropertyKeyConst.NAMESRV_ADDR,
  18. "XXX ");
  19. Producer producer = ONSFactory.createProducer(properties);
  20. //Before sending a message, call the start() method once to start the producer.
  21. producer.start();
  22. Message msg = new Message( //
  23. // The topic of the message.
  24. "Topic",
  25. // The message tag, which is similar to a Gmail tag. It is used to sort messages, enabling the consumer to filter messages on the MQ broker based on the specified criteria.
  26. "tag",
  27. // The message body in any binary format. MQ does not process the message body. The producer and consumer must negotiate the consistent serialization and deserialization methods.
  28. "Hello MQ".getBytes())
  29. // Set a key service property representing the message, that is, the message key, and try to keep it globally unique.
  30. // A unique identifier enables you to query a message and resend it in the console if you fail to receive the message.
  31. // Note: Messages can still be sent and received if you do not set this attribute.
  32. msg.setKey("ORDERID_100");
  33. try {
  34. // The scheduled message delivery time (unit: ms) after a specific timestamp, for example, 2016-03-07 16:21:00. If the scheduled time is earlier than the current timestamp, the message is immediately delivered to the consumer.
  35. long timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-03-07 16:21:00").getTime();
  36. msg.setStartDeliverTime(timeStamp);
  37. // The message is sent if no exception is thrown.
  38. SendResult sendResult = producer.send(msg);
  39. System.out.println("Message Id:" + sendResult.getMessageId());
  40. }
  41. catch (Exception e) {
  42. // The message failed to be sent and requires a retry. The system can resend the message or store message data persistently.
  43. System.out.println(new Date() + " Send mq message failed. Topic is:" + msg.getTopic());
  44. e.printStackTrace();
  45. }
  46. // Destroy the producer object before exiting from the application.
  47. // Note: You can choose not to destroy the producer object.
  48. producer.shutdown();
  49. }
  50. }

Subscribe to scheduled messages

The method for subscribing to scheduled messages is the same as that for subscribing to normal messages. For more information, see Subscribe to messages.