All Products
Document Center

Send and receive scheduled messages

Last Updated: Mar 29, 2019

The currently supported regions include Internet, East China 1, East China 2, North China 2, and South China 1.

Scheduled messages can be consumed by consumers after a specified period, 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, similar to delayed queues.

Send Scheduled Messages

The following are sample codes for sending scheduled messages:

  1. #include "ONSFactory.h"
  2. #include "ONSClientException.h"
  3. using namespace ons;
  4. int main()
  5. {
  6. //Create producer and configure parameters required for sending messages;
  7. ONSFactoryProperty factoryInfo;
  8. factoryInfo.setFactoryProperty(ONSFactoryProperty::PublishTopics,"XXX" );//The topic you created on the MQ console
  9. factoryInfo.setFactoryProperty(ONSFactoryProperty::MsgContent, "xxx");//msg content
  10. factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, "xxx");//Alibaba Cloud ID verification, which is created on Alibaba Cloud Management Console
  11. factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, "xxx" );//Alibaba Cloud ID verification, which is created on Alibaba Cloud Management Console
  12. //create producer
  13. Producer *pProducer = ONSFactory::getInstance()->createProducer(factoryInfo);
  14. // Before sending messages, the start method must be called once to start the producer;
  15. pProducer->start();
  16. Message msg(
  17. // Message topic
  18. factoryInfo.getPublishTopics(),
  19. //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.
  20. "TagA",
  21. //Message Body, which cannot be null. Serialization and deserialization methods need to be negotiated and remain consistent between the producer and the consumer.
  22. factoryInfo.getMessageContent()
  23. );
  24. // The setting represents the key service property of the message, so please set it as globally unique as possible.
  25. // You can query a message and resend it through the MQ console when you cannot receive the message properly.
  26. // Note: Normal sessage sending and receiving will not be affected if message key is not configured.
  27. msg.setKey("ORDERID_100");
  28. // Deliver time (ms) specifies the time point after which the message can be consumed. The example means that the message will be consumed after 3 seconds.
  29. long deliverTime = obtain current system time (ms) + 3000;
  30. msg.setStartDeliverTime(deliverTime);
  31. // If no exceptions are thrown, then the message is sent successfully.
  32. try
  33. {
  34. SendResultONS sendResult = pProducer->send(msg);
  35. }
  36. catch(ONSClientException e)
  37. {
  38. //Customize the details for processing exceptions
  39. }
  40. // The object Producer must be destroyed before exiting the application. Otherwise there will be memory leakage.
  41. pProducer->shutdown();
  42. return 0;
  43. }

Subscribe to Scheduled Messages

For instructions and example codes of subscribing to scheduled messages, see Subscribe Message.