Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- services.AddSingleton<CitiesFrameworkCore.RabbitMq.RabbitMQConsumer>();
- public void ProcessMessages()
- {
- using (_connection = _factory.CreateConnection())
- {
- using (var channel = _connection.CreateModel())
- {
- Console.WriteLine("Listening for Topic <cities.createCity>");
- Console.WriteLine("-----------------------------------------");
- Console.WriteLine();
- channel.ExchangeDeclare(ExchangeName, "topic");
- channel.QueueDeclare(CitiesTopicQueName, true, false, false, null);
- channel.QueueBind(CitiesTopicQueName, ExchangeName,
- "cities.createCity");
- var consumer = new AsyncEventingBasicConsumer(channel);
- consumer.Received += Consumer_Received;
- channel.BasicConsume(CitiesTopicQueName, true, consumer);
- }
- }
- rabbitConsumer.CreateConnection();
- rabbitConsumer.ProcessMessages();
- var sender = new RabbitMQClient();
- var obj = new CityDto();
- sender.SendCity(obj);
- public class RabbitMQClient
- {
- private static ConnectionFactory _factory;
- private static IConnection _connection;
- private static IModel _model;
- private const string ExchangeName = "Topic_Exchange";
- private const string CitiesTopicQueName = "CitiesTopicTopic_Queue";
- //private const string PurchaseOrderQueueName = "PurchaseOrderTopic_Queue";
- private const string AllQueueName = "AllTopic_Queue";
- public RabbitMQClient()
- {
- CreateConnection();
- }
- private static void CreateConnection()
- {
- _factory = new ConnectionFactory
- {
- HostName = "localhost",
- UserName = "guest",
- Password = "guest"
- };
- _connection = _factory.CreateConnection();
- _model = _connection.CreateModel();
- _model.ExchangeDeclare(ExchangeName, "topic");
- _model.QueueDeclare(CitiesTopicQueName, true, false, false, null);
- _model.QueueDeclare(AllQueueName, true, false, false, null);
- _model.QueueBind(CitiesTopicQueName, ExchangeName, "cities.createCity");
- _model.QueueBind(AllQueueName, ExchangeName, "cities.*");
- }
- public void Close()
- {
- _connection.Close();
- }
- public void SendCity(CityDto city)
- {
- SendMessage(city.Serialize(), "cities.createCity");
- Console.WriteLine("Message SendCity Last Line");
- }
- public void SendMessage(byte[] message, string routingKey)
- {
- _model.BasicPublish(ExchangeName, routingKey, null, message);
- }
- }
Add Comment
Please, Sign In to add comment