Advertisement
Guest User

Untitled

a guest
Sep 12th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. static void Main()
  2. {
  3. using (var connection = new ConnectionFactory().CreateConnection())
  4. {
  5. using (var channel1 = connection.CreateModel())
  6. {
  7. // queue1 -> produce click to queue2
  8. // click -> exchange
  9. // queue2 -> consume click from queue1
  10. channel1.ExchangeDeclare("exchange", ExchangeType.Direct, autoDelete: true);
  11. channel1.QueueDeclare("queue1");
  12. channel1.QueueBind("queue1", "exchange", "queue1");
  13. channel1.QueueDeclare("queue2");
  14. channel1.QueueBind("queue2", "exchange", "queue2");
  15.  
  16. var consumer1 = new EventingBasicConsumer(channel1);
  17. consumer1.Received += (sender, args) =>
  18. {
  19. try
  20. {
  21. using (var channel2 = connection.CreateModel())
  22. {
  23. Console.WriteLine("Click requested");
  24. channel2.BasicPublish("exchange", "queue2");
  25. }
  26. }
  27. catch (TimeoutException e)
  28. {
  29. Console.WriteLine(e);
  30. throw;
  31. }
  32. };
  33. channel1.BasicConsume(
  34. "queue1",
  35. autoAck: true,
  36. consumer1);
  37.  
  38. var consumer2 = new EventingBasicConsumer(channel1);
  39. consumer2.Received += (sender, args) =>
  40. {
  41. Console.WriteLine("Click processed");
  42. };
  43. channel1.BasicConsume(
  44. "queue2",
  45. autoAck: true,
  46. consumer2);
  47.  
  48. channel1.BasicPublish(
  49. "exchange",
  50. "queue1");
  51.  
  52. Console.WriteLine("Publish to queue1. No timeout");
  53.  
  54. Console.ReadLine();
  55. }
  56. }
  57.  
  58. // ReSharper disable once FunctionNeverReturns
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement