DAS_2023_1/almukhammetov_bulat_lab_4/RabbitMQ/RabbitMQConsumer1/Program.cs

34 lines
1.1 KiB
C#
Raw Normal View History

2024-01-06 18:37:17 +04:00
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
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 = "myQueue";
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);
// Имитация обработки сообщения
Thread.Sleep(3000); // Задержка 2-3 секунды
};
channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);
Console.WriteLine(" Нажмите [enter] для выхода.");
Console.ReadLine();
}