Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. using RabbitMQ.Client;
  2. using RabbitMQ.Client.Events;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace prod
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15.  
  16. var factory = new ConnectionFactory()
  17. {
  18. UserName = "pfolihar",
  19. Password = "NrX1IaunZAQxYQ9enonUZoyYDu2aEN8O",
  20. HostName = "bulldog.rmq.cloudamqp.com",
  21. VirtualHost = "pfolihar"
  22. };
  23.  
  24. string queueName = "message_queue";
  25. using (var connection = factory.CreateConnection())
  26. using (var channel = connection.CreateModel())
  27. {
  28. channel.QueueDeclare(queueName, false, false, false, null);
  29. for (int i = 0; i < 10; ++i)
  30. {
  31. IBasicProperties properties = channel.CreateBasicProperties();
  32. properties.Headers = new Dictionary<string, object>();
  33. properties.Headers.Add("num", i);
  34. string message = "Mssg " + i.ToString();
  35. var body = Encoding.UTF8.GetBytes(message);
  36.  
  37. string replyQueueName = channel.QueueDeclare().QueueName;
  38. EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
  39. channel.BasicConsume(replyQueueName, true, consumer);
  40. properties.ReplyTo = replyQueueName;
  41. var corrId = Guid.NewGuid().ToString();
  42. properties.CorrelationId = corrId;
  43.  
  44. channel.BasicPublish("", queueName, properties, body);
  45. Console.WriteLine("Sending: " + message);
  46. consumer.Received += (model, ea) =>
  47. {
  48. if (ea.BasicProperties.CorrelationId == corrId)
  49. {
  50. Console.WriteLine("\n" + Encoding.UTF8.GetString(ea.Body));
  51. }
  52. };
  53.  
  54. }
  55. Console.ReadKey();
  56. }
  57. }
  58. }
  59. }
  60.  
  61.  
  62.  
  63. using RabbitMQ.Client;
  64. using RabbitMQ.Client.Events;
  65. using System;
  66. using System.Collections.Generic;
  67. using System.Linq;
  68. using System.Text;
  69. using System.Threading.Tasks;
  70.  
  71. namespace odb_1
  72. {
  73. class Program
  74. {
  75. static void Main(string[] args)
  76. {
  77. var factory = new ConnectionFactory()
  78. {
  79. UserName = "pfolihar",
  80. Password = "NrX1IaunZAQxYQ9enonUZoyYDu2aEN8O",
  81. HostName = "bulldog.rmq.cloudamqp.com",
  82. VirtualHost = "pfolihar"
  83. };
  84.  
  85. string queueName = "message_queue";
  86. using (var connection = factory.CreateConnection())
  87. using (var channel = connection.CreateModel())
  88. {
  89. channel.BasicQos(0, 1, true);
  90. var consumer = new EventingBasicConsumer(channel);
  91.  
  92. consumer.Received += (model, ea) =>
  93. {
  94. ((EventingBasicConsumer)model).Model.BasicAck(ea.DeliveryTag, true);
  95. var body = ea.Body;
  96. var message = Encoding.UTF8.GetString(body);
  97. Console.WriteLine("Received: " + message);
  98.  
  99. int num = (int)ea.BasicProperties.Headers["num"];
  100. Console.WriteLine("13+" + num.ToString());
  101.  
  102. System.Threading.Thread.Sleep(2000);
  103.  
  104. var responseBytes = Encoding.UTF8.GetBytes("13+" + num.ToString() + " = " + (13 + num));
  105. var replyProps = channel.CreateBasicProperties();
  106. replyProps.CorrelationId = ea.BasicProperties.CorrelationId;
  107.  
  108. channel.BasicPublish("", ea.BasicProperties.ReplyTo, true, replyProps, responseBytes);
  109.  
  110. };
  111. while (true)
  112. {
  113. channel.BasicConsume(queueName, false, consumer);
  114. }
  115.  
  116. }
  117.  
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement