38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
|
using System.Text;
|
|||
|
using RabbitMQ.Client;
|
|||
|
using RabbitMQ.Client.Events;
|
|||
|
|
|||
|
Random rand = new Random();
|
|||
|
var factory = new ConnectionFactory { HostName = "localhost" };
|
|||
|
using var connection = factory.CreateConnection();
|
|||
|
using var channel = connection.CreateModel();
|
|||
|
|
|||
|
string queueName = $"courier{rand.Next()}";
|
|||
|
|
|||
|
channel.QueueDeclare(queue: queueName);
|
|||
|
channel.QueueBind(queue: queueName,
|
|||
|
exchange: "post",
|
|||
|
routingKey: string.Empty);
|
|||
|
|
|||
|
Console.WriteLine(" [*] Ожидание сообщения.");
|
|||
|
|
|||
|
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 = $"Курьер с письмом потратил {waitTime} минут и доставил письмо {message}";
|
|||
|
Console.WriteLine($" [x] Сделано. {outputText}");
|
|||
|
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
|
|||
|
};
|
|||
|
channel.BasicConsume(queue: queueName,
|
|||
|
autoAck: false,
|
|||
|
consumer: consumer);
|
|||
|
|
|||
|
Console.WriteLine(" Нажми [enter] чтобы выйти.");
|
|||
|
Console.ReadLine();
|