Guest User

Untitled

a guest
Oct 11th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. services.AddSingleton<CitiesFrameworkCore.RabbitMq.RabbitMQConsumer>();
  2.  
  3. public void ProcessMessages()
  4. {
  5. using (_connection = _factory.CreateConnection())
  6. {
  7. using (var channel = _connection.CreateModel())
  8. {
  9. Console.WriteLine("Listening for Topic <cities.createCity>");
  10. Console.WriteLine("-----------------------------------------");
  11. Console.WriteLine();
  12.  
  13. channel.ExchangeDeclare(ExchangeName, "topic");
  14. channel.QueueDeclare(CitiesTopicQueName, true, false, false, null);
  15.  
  16. channel.QueueBind(CitiesTopicQueName, ExchangeName,
  17. "cities.createCity");
  18.  
  19. var consumer = new AsyncEventingBasicConsumer(channel);
  20. consumer.Received += Consumer_Received;
  21. channel.BasicConsume(CitiesTopicQueName, true, consumer);
  22. }
  23. }
  24.  
  25.  
  26. rabbitConsumer.CreateConnection();
  27. rabbitConsumer.ProcessMessages();
  28.  
  29. var sender = new RabbitMQClient();
  30. var obj = new CityDto();
  31. sender.SendCity(obj);
  32.  
  33. public class RabbitMQClient
  34. {
  35. private static ConnectionFactory _factory;
  36. private static IConnection _connection;
  37. private static IModel _model;
  38.  
  39. private const string ExchangeName = "Topic_Exchange";
  40. private const string CitiesTopicQueName = "CitiesTopicTopic_Queue";
  41. //private const string PurchaseOrderQueueName = "PurchaseOrderTopic_Queue";
  42. private const string AllQueueName = "AllTopic_Queue";
  43.  
  44. public RabbitMQClient()
  45. {
  46. CreateConnection();
  47. }
  48.  
  49. private static void CreateConnection()
  50. {
  51. _factory = new ConnectionFactory
  52. {
  53. HostName = "localhost",
  54. UserName = "guest",
  55. Password = "guest"
  56. };
  57.  
  58. _connection = _factory.CreateConnection();
  59. _model = _connection.CreateModel();
  60. _model.ExchangeDeclare(ExchangeName, "topic");
  61.  
  62. _model.QueueDeclare(CitiesTopicQueName, true, false, false, null);
  63. _model.QueueDeclare(AllQueueName, true, false, false, null);
  64.  
  65. _model.QueueBind(CitiesTopicQueName, ExchangeName, "cities.createCity");
  66.  
  67. _model.QueueBind(AllQueueName, ExchangeName, "cities.*");
  68. }
  69.  
  70. public void Close()
  71. {
  72. _connection.Close();
  73. }
  74.  
  75. public void SendCity(CityDto city)
  76. {
  77. SendMessage(city.Serialize(), "cities.createCity");
  78. Console.WriteLine("Message SendCity Last Line");
  79. }
  80.  
  81. public void SendMessage(byte[] message, string routingKey)
  82. {
  83. _model.BasicPublish(ExchangeName, routingKey, null, message);
  84. }
  85. }
Add Comment
Please, Sign In to add comment