edit-icon download-icon

Send and receive ordered messages

Last Updated: Jun 19, 2018

Send ordered messages

Sample Code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7. using ons;
  8. namespace test
  9. {
  10. public class OnscSharp
  11. {
  12. private static OrderProducer _orderproducer;
  13. private static string Ons_Topic = "xxxxxxxxxxx";
  14. private static string Ons_ProducerID = "xxxxxxxxxxx";
  15. private static string Ons_AccessKey = "xxxxxxxxxxxx";
  16. private static string Ons_SecretKey = "xxxxxxxxxxxxxxx";
  17. private static string Ons_ConsumerId = "xxxxxxxxxxxx";
  18. public static void SendOrderMessage(string msgBody, String tag = "RegisterLog", String key = "test")
  19. {
  20. Message msg = new Message(Ons_Topic, tag, msgBody);
  21. msg.setKey(Guid.NewGuid().ToString());
  22. try
  23. {
  24. SendResultONS sendResult = _orderproducer.send(msg, shardingKey);
  25. Console.WriteLine("send success {0}", sendResult.getMessageId());
  26. }
  27. catch (Exception ex)
  28. {
  29. Console.WriteLine("send failure{0}", ex.ToString());
  30. }
  31. }
  32. public static void StartOrderProducer()
  33. {
  34. _orderproducer.start();
  35. }
  36. public static void ShutdownOrderProducer()
  37. {
  38. _orderproducer.shutdown();
  39. }
  40. private static ONSFactoryProperty getFactoryProperty()
  41. {
  42. ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
  43. factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, Ons_AccessKey);
  44. factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, Ons_SecretKey);
  45. factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, Ons_ConsumerId);
  46. factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, Ons_ProducerID);
  47. factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, Ons_Topic);
  48. return factoryInfo;
  49. }
  50. public static void CreateOrderProducer()
  51. {
  52. _orderproducer = ONSFactory.getInstance().createOrderProducer(getFactoryProperty());
  53. }
  54. }
  55. class OrderProducerForEx
  56. {
  57. static void Main(string[] args)
  58. {
  59. OnscSharp.CreateOrderProducer();
  60. OnscSharp.StartOrderProducer();
  61. System.DateTime beforDt = System.DateTime.Now;
  62. for (int i = 0;i < 1000; ++i)
  63. {
  64. OnscSharp.SendOrderMessage("Test", "Tag", "key");
  65. }
  66. System.DateTime endDt = System.DateTime.Now;
  67. System.TimeSpan ts = endDt.Subtract(beforDt);
  68. Console.WriteLine("per message:{0}ms.", ts.TotalMilliseconds / 10000);
  69. OnscSharp.ShutdownOrderProducer();
  70. }
  71. }
  72. }

Receive ordered messages

Sample code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7. using ons;
  8. namespace test
  9. {
  10. public class MyMsgOrderListener : MessageOrderListener
  11. {
  12. public MyMsgOrderListener()
  13. {
  14. }
  15. ~MyMsgOrderListener()
  16. {
  17. }
  18. public override ons.OrderAction consume(Message value, ConsumeOrderContext context)
  19. {
  20. Byte[] text = Encoding.Default.GetBytes(value.getBody());
  21. Console.WriteLine(Encoding.UTF8.GetString(text));
  22. return ons.OrderAction.Success;
  23. }
  24. }
  25. public class OnscSharp
  26. {
  27. private static OrderConsumer _orderconsumer;
  28. private static MyMsgOrderListener _order_listen;
  29. private static string Ons_Topic = "xxxxxxxxxxx";
  30. private static string Ons_ProducerID = "xxxxxxxxxxx";
  31. private static string Ons_AccessKey = "xxxxxxxxxxxx";
  32. private static string Ons_SecretKey = "xxxxxxxxxxxxxxx";
  33. private static string Ons_ConsumerId = "xxxxxxxxxxxx";
  34. public static void StartOrderConsumer()
  35. {
  36. _order_listen = new MyMsgOrderListener();
  37. _orderconsumer.subscribe(Ons_Topic, "*", _order_listen);
  38. _orderconsumer.start();
  39. }
  40. public static void shutdownOrderConsumer()
  41. {
  42. _orderconsumer.shutdown();
  43. }
  44. private static ONSFactoryProperty getFactoryProperty()
  45. {
  46. ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
  47. factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, Ons_AccessKey);
  48. factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, Ons_SecretKey);
  49. factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, Ons_ConsumerId);
  50. factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, Ons_ProducerID);
  51. factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, Ons_Topic);
  52. factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "E://log");
  53. return factoryInfo;
  54. }
  55. public static void CreateOrderConsumer()
  56. {
  57. _orderconsumer = ONSFactory.getInstance().createOrderConsumer(getFactoryProperty());
  58. }
  59. }
  60. class OrderConsumerForEx
  61. {
  62. static void Main(string[] args)
  63. {
  64. OnscSharp.CreateOrderConsumer();
  65. OnscSharp.StartOrderConsumer();
  66. Thread.Sleep(1000 * 1000);
  67. OnscSharp.shutdownOrderConsumer();
  68. }
  69. }
  70. }
Thank you! We've received your feedback.