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

34 lines
1002 B
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();
2024-01-09 00:42:05 +04:00
Random rand = new Random();
string queueName = $"queue1";
2024-01-08 23:04:03 +04:00
2024-01-09 00:42:05 +04:00
channel.QueueDeclare(queue: queueName);
channel.QueueBind(queue: queueName,
exchange: "reports",
routingKey: string.Empty);
2024-01-08 23:04:03 +04:00
2024-01-09 00:42:05 +04:00
Console.WriteLine(" [*] Waiting for messages.");
2024-01-08 23:04:03 +04:00
var consumer = new EventingBasicConsumer(channel);
2024-01-09 00:42:05 +04:00
consumer.Received += async (model, ea) =>
2024-01-08 23:04:03 +04:00
{
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);
};
2024-01-09 00:42:05 +04:00
channel.BasicConsume(queue: queueName,
autoAck: false,
consumer: consumer);
2024-01-08 23:04:03 +04:00
2024-01-09 00:42:05 +04:00
Console.WriteLine(" Press [enter] to exit.");
2024-01-08 23:04:03 +04:00
Console.ReadLine();