Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. void WorkWithRMQ(bool isReceiver)
  2. {
  3. string Exchange = "my.special";
  4. string Queue = "my.special.outq2";
  5. string RoutingKey = "my.special.outq2";
  6.  
  7. var factory = new ConnectionFactory()
  8. {
  9. HostName = "RabbitMQ.server",
  10. Port = 15672,
  11. UserName = "userName",
  12. Password = "p@ssWorD",
  13. VirtualHost = "someVhost",
  14. Protocol = Protocols.AMQP_0_9_1,
  15. };
  16.  
  17. using (var connection = factory.CreateConnection())
  18.  
  19. using (var channel = connection.CreateModel())
  20. {
  21. channel.ExchangeDeclare(Exchange, ExchangeType.Topic, true);
  22. channel.QueueDeclare(Queue, true, false, false, new Dictionary<string, object> { { "x-ha-policy", "all" } });
  23. channel.QueueBind(Queue, Exchange, RoutingKey);
  24. //channel.BasicReturn += channel_BasicReturn;
  25.  
  26. if (isReceiver)
  27. {
  28. Console.WriteLine("Receiver started: \r\n");
  29. var consumer = new EventingBasicConsumer(channel);
  30. consumer.Received += (model, ea) =>
  31. {
  32. var body = ea.Body;
  33. var message = Encoding.UTF8.GetString(body);
  34. Console.WriteLine(" [x] Received {0}", message);
  35. };
  36. channel.BasicConsume(queue: Queue, noAck: true, consumer: consumer);
  37. }
  38. else
  39. {
  40. Console.WriteLine("Type your message to Send: \r\n");
  41. string message = Console.ReadLine().Trim();
  42. var body = Encoding.UTF8.GetBytes(message);
  43. channel.BasicPublish(exchange: Exchange,
  44. routingKey: RoutingKey,
  45. basicProperties: null,
  46. body: body);
  47. }
  48. Console.WriteLine(" Press [enter] to exit.");
  49. Console.ReadLine();
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement