All Products
Search
Document Center

Send and receive ordered messages

Last Updated: Mar 29, 2019

Send ordered messages

The sample code for sending ordered messages is as follows:

  1. using System;
  2. using ons;
  3. public class OrderProducerExampleForEx
  4. {
  5. public OrderProducerExampleForEx()
  6. {
  7. }
  8. static void Main(string[] args) {
  9. // Configure your account according to the following settings. You can obtain these settings in the console.
  10. ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
  11. // The AccessKeyId you created in the Alibaba Cloud console for identity authentication.
  12. factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, "Your access key");
  13. // The AccessKeySecret you created in the Alibaba Cloud console for identity authentication.
  14. factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, "Your access secret");
  15. // The group ID you created in the console.
  16. factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, "GID_example");
  17. // The topic you created in the console.
  18. factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, "T_example_topic_name");
  19. // Set the TCP endpoint: Go to the **Instances** page in the RocketMQ console, and view the endpoint in the **Endpoint Information** area.
  20. factoryInfo.setFactoryProperty(ONSFactoryProperty.NAMESRV_ADDR, "NameSrv_Addr");
  21. // Set the log path.
  22. factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "C://log");
  23. // Create producer instances.
  24. // Note: Producer instances are thread-secure and can be used to send messages of different topics. Each of your threads
  25. // needs only one producer instance.
  26. OrderProducer producer = ONSFactory.getInstance().createOrderProducer(factoryInfo);
  27. // Start the instance at the client.
  28. producer.start();
  29. // Create message objects.
  30. Message msg = new Message(factoryInfo.getPublishTopics(), "tagA", "Example message body");
  31. string shardingKey = "App-Test";
  32. for (int i = 0; i < 32; i++) {
  33. try
  34. {
  35. SendResultONS sendResult = producer.send(msg, shardingKey);
  36. Console.WriteLine("send success {0}", sendResult.getMessageId());
  37. }
  38. catch (Exception ex)
  39. {
  40. Console.WriteLine("send failure{0}", ex.ToString());
  41. }
  42. }
  43. // Disable the producer instance when the thread is about to exit.
  44. producer.shutdown();
  45. }
  46. }

The sample code for consuming ordered messages is as follows:

  1. using System;
  2. using System.Text;
  3. using System.Threading;
  4. using ons;
  5. namespace demo
  6. {
  7. public class MyMsgOrderListener : MessageOrderListener
  8. {
  9. public MyMsgOrderListener()
  10. {
  11. }
  12. ~MyMsgOrderListener()
  13. {
  14. }
  15. public override ons.OrderAction consume(Message value, ConsumeOrderContext context)
  16. {
  17. Byte[] text = Encoding.Default.GetBytes(value.getBody());
  18. Console.WriteLine(Encoding.UTF8. GetString(text));
  19. return ons.OrderAction.Success;
  20. }
  21. }
  22. class OrderConsumerExampleForEx
  23. {
  24. static void Main(string[] args)
  25. {
  26. // Configure your account according to the following settings. You can obtain these settings in the console.
  27. ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
  28. // The AccessKeyId you created in the Alibaba Cloud console for identity authentication.
  29. factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, "Your access key");
  30. // The AccessKeySecret you created in the Alibaba Cloud console for identity authentication.
  31. factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, "Your access secret");
  32. // The group ID you created in the console.
  33. factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, "GID_example");
  34. // The topic you created in the console.
  35. factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, "T_example_topic_name");
  36. // Set the TCP endpoint: Go to the **Instances** page in the RocketMQ console, and view the endpoint in the **Endpoint Information** area.
  37. factoryInfo.setFactoryProperty(ONSFactoryProperty.NAMESRV_ADDR, "NameSrv_Addr");
  38. // Set the log path.
  39. factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "C://log");
  40. // Create producer instances.
  41. OrderConsumer consumer = ONSFactory.getInstance().createOrderConsumer(factoryInfo);
  42. // Subscribe to topics.
  43. consumer.subscribe(factoryInfo.getPublishTopics(), "*",new MyMsgOrderListener());
  44. // Start the consumer instance.
  45. consumer.start();
  46. // Enable the main thread to sleep for a period of time.
  47. Thread.Sleep(30000);
  48. // Disable the consumer instance when you no longer use it.
  49. consumer.shutdown();
  50. }
  51. }
  52. }