Advertisement
Guest User

Untitled

a guest
May 6th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. // producent
  2.  
  3. using RabbitMQ.Client;
  4. using RabbitMQ.Client.Framing;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace rabbitmq
  12. {
  13.  
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. var factory = new ConnectionFactory()
  19. {
  20. UserName = "dahjdrhx",
  21. Password = "ng-DairYiYJDTY9JyemWFB9By5WqxXcA",
  22. HostName = "eagle.rmq.cloudamqp.com",
  23. VirtualHost = "dahjdrhx"
  24. };
  25.  
  26. using (var connection = factory.CreateConnection())
  27. using (var channel = connection.CreateModel())
  28. {
  29. //topic
  30. channel.ExchangeDeclare("top_rout", "topic");
  31. string message = "Jestem sobie topic i sobie rozmawiam na podany temat";
  32. var body = Encoding.UTF8.GetBytes(message);
  33. channel.BasicPublish("top_rout", "lazy.pink.rabbit", null, body);
  34.  
  35. //topic
  36. //headers
  37. /*BasicProperties props = new BasicProperties();
  38. props.Headers = new Dictionary<string, object>
  39. {
  40. {"part","first" },
  41. {"number", 42 }
  42. };
  43. channel.ExchangeDeclare("head_rout", "headers");
  44. string message = "Jestem sobie header i dobrze strzelam z glowki";
  45. var body = Encoding.UTF8.GetBytes(message);
  46. channel.BasicPublish("head_rout", "", props, body);*/
  47. //headers
  48. //s/r m
  49. Console.ReadKey();
  50. }
  51.  
  52. }
  53. }
  54. }
  55.  
  56.  
  57. // klient
  58.  
  59.  
  60. using RabbitMQ.Client;
  61. using RabbitMQ.Client.Events;
  62. using System;
  63. using System.Collections.Generic;
  64. using System.Linq;
  65. using System.Text;
  66. using System.Threading.Tasks;
  67.  
  68. namespace next
  69. {
  70. class MyConsumer : DefaultBasicConsumer
  71. {
  72. public MyConsumer(IModel model) : base(model)
  73. {
  74.  
  75. }
  76. //public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
  77.  
  78. public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
  79. {
  80. var message = Encoding.UTF8.GetString(body);
  81. Console.WriteLine(message);
  82. }
  83.  
  84. }
  85. class Program
  86. {
  87. static void Main(string[] args)
  88. {
  89. var factory = new ConnectionFactory()
  90. {
  91. UserName = "dahjdrhx",
  92. Password = "ng-DairYiYJDTY9JyemWFB9By5WqxXcA",
  93. HostName = "eagle.rmq.cloudamqp.com",
  94. VirtualHost = "dahjdrhx"
  95. };
  96. using (var connection = factory.CreateConnection())
  97. using (var channel = connection.CreateModel())
  98. {
  99. var consumer = new MyConsumer(channel);
  100.  
  101.  
  102. //topic
  103. channel.ExchangeDeclare("top_rout", "topic");
  104. var queueName = channel.QueueDeclare().QueueName;
  105. channel.QueueBind(queueName, "top_rout", "lazy.pink.rabbit");
  106. //topic
  107.  
  108.  
  109. //header
  110. /* var queueName = channel.QueueDeclare().QueueName;
  111. var headers = new Dictionary<string, object>
  112. {
  113. {"part", "first"},
  114. {"number", 42 }
  115. };
  116. channel.QueueBind(queueName, "head_rout", "", headers);*/
  117. //header
  118.  
  119. channel.BasicConsume("message_queue", true, consumer);
  120. //Console.WriteLine(consumer);
  121. Console.ReadKey();
  122. }
  123. }
  124.  
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement