DAS_2023_1/almukhammetov_bulat_lab_4/RabbitMQ/RabbitMQConsumer2/Program.cs
2024-01-06 18:37:17 +04:00

33 lines
1.2 KiB
C#

using RabbitMQ.Client.Events;
using RabbitMQ.Client;
using System.Text;
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange: "logs", type: ExchangeType.Fanout);
// Определение другого имени очереди
var queueName = "myQueue2";
channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
channel.QueueBind(queue: queueName, exchange: "logs", routingKey: "");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Доставлено {0}", message);
// Здесь нет задержки, сообщение обрабатывается моментально
};
channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);
Console.WriteLine(" Нажмитие [enter] для выхода.");
Console.ReadLine();
}