edit-icon download-icon

Send and receive scheduled messages

Last Updated: Jun 20, 2018

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

Subscribe to Scheduled Messages

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

Thank you! We've received your feedback.