edit-icon download-icon

Send and receive scheduled messages

Last Updated: Jun 25, 2018

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

Subsribe to Scheduled Messages

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

Thank you! We've received your feedback.