distributed-computing/tasks/mutriskov-ds/lab_4/MainTask/Consumer-2/Program.cs

32 lines
1.0 KiB
C#
Raw Normal View History

2024-01-08 23:04:03 +04:00
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.QueueDeclare("queue2");
channel.QueueBind(queue: "queue2",
exchange: "reports",
routingKey: string.Empty);
Console.WriteLine(" [*] Ожидание сообщения");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
byte[] body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
var reportNumber = message.Split('#')[1];
Console.WriteLine($" [x] Отчет #{reportNumber} создан.");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: "queue2",
autoAck: true,
consumer: consumer);
Console.WriteLine(" Нажмите [enter] чтобы выйти.");
Console.ReadLine();