All Products
Document Center

Send and receive scheduled messages

Last Updated: Mar 29, 2019

The currently supported domains 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. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using ons;
  7. namespace ons
  8. {
  9. class onscsharp
  10. {
  11. static void Main(string[] args)
  12. {
  13. //A mandatory parameter required for the producer creation and message handling
  14. ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
  15. factoryInfo.setFactoryProperty(factoryInfo.PublishTopics, "XXX");//The topic you created on the MQ console
  16. factoryInfo.setFactoryProperty(factoryInfo.MsgContent, "XXX");//Message content
  17. factoryInfo.setFactoryProperty(factoryInfo.AccessKey, "XXX");//AccessKey, Alibaba Cloud ID verification, which is created on Alibaba Cloud Management Console
  18. factoryInfo.setFactoryProperty(factoryInfo.SecretKey,"XXX");//SecretKey, Alibaba Cloud ID verification, which is created on Alibaba Cloud Management Console
  19. //Create a producer
  20. ONSFactory onsfactory = new ONSFactory();
  21. Producer pProducer = onsfactory.getInstance().createProducer(factoryInfo);
  22. //Before sending messages, the start method must be called once to start the producer.
  23. pProducer.start();
  24. Message msg = new Message(
  25. //Message Topic
  26. factoryInfo.getPublishTopics(),
  27. //Message Tag
  28. "TagA",
  29. //Message Body
  30. factoryInfo.getMessageContent()
  31. );
  32. // The setting represents the key service property of the message, so please set it as globally unique as possible.
  33. // You can query a message and resend it through the MQ console when you cannot receive the message properly.
  34. // Note: Normal sessage sending and receiving will not be affected if message key is not configured.
  35. msg.setKey("ORDERID_100");
  36. // 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.
  37. long deliverTime = obtain current system time (ms) + 3000;
  38. msg.setStartDeliverTime(deliverTime);
  39. //If no exceptions are thrown, then the message is sent successfully.
  40. try
  41. {
  42. SendResultONS sendResult = pProducer.send(msg);
  43. }
  44. catch(ONSClientException e)
  45. {
  46. //Handle the message sending failures
  47. }
  48. //The object Producer must be destroyed before exiting the application. Otherwise there will be memory leakage.
  49. pProducer.shutdown();
  50. }
  51. }
  52. }

Subsribe to Scheduled Messages

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