Program.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using Amqp;
  3. using Amqp.Framing;
  4. using Amqp.Sasl;
  5. using Amqp.Types;
  6. namespace receive
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string url = "amqp://localhost:61616";
  13. string addr = "sampleTopic";
  14. bool durable = false;
  15. string cid = "";
  16. if (args.Length > 0) {
  17. durable = bool.Parse(args[0]);
  18. }
  19. if (durable && args.Length > 1) {
  20. cid = args[1];
  21. } else if (durable) {
  22. cid = Guid.NewGuid().ToString();
  23. }
  24. Address a = new Address(url);
  25. Connection c;
  26. if (durable) {
  27. c = new Connection(a, SaslProfile.Anonymous,
  28. new Open() { ContainerId = cid },
  29. null);
  30. } else {
  31. c = new Connection(a);
  32. }
  33. Console.WriteLine("Created new " + (durable ? "" : "non-") + "durable subscriber" + (durable ? " with ID " + cid : ""));
  34. Session s = new Session(c);
  35. ReceiverLink rl;
  36. if (durable) {
  37. Source src = new Source() {
  38. Address = addr,
  39. Durable = 2,
  40. ExpiryPolicy = new Symbol("never"),
  41. };
  42. rl = new ReceiverLink(s, "receiver", src, null);
  43. } else {
  44. rl = new ReceiverLink(s, "receiver", addr);
  45. }
  46. Console.WriteLine("Receiving random metrics from " + url + "/" + addr);
  47. while (true) {
  48. Message m = rl.Receive();
  49. rl.Accept(m);
  50. if (m != null) {
  51. Console.WriteLine("Got: " + m.Body.ToString());
  52. continue;
  53. }
  54. System.Threading.Thread.Sleep(1000);
  55. }
  56. }
  57. }
  58. }