distributed-computing/tasks/mikhailov-ys/lab_4/Main/Consumer2/Program.cs

37 lines
1.1 KiB
C#
Raw Normal View History

2024-01-06 23:45:37 +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();
Random rand = new Random();
string queueName = $"denied{rand.Next()}";
channel.QueueDeclare(queue: queueName);
channel.QueueBind(queue: queueName,
exchange: "report",
routingKey: string.Empty);
Console.WriteLine(" [*] Waiting for messages.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += async (model, ea) =>
{
byte[] body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
int waitTime = rand.Next(20, 90);
Thread.Sleep(waitTime * 100);
string outputText = $"Заявление обработано {message} за {waitTime} минут";
Console.WriteLine($" [x] Done. {outputText}");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: queueName,
autoAck: false,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();