35 lines
1.0 KiB
C#
35 lines
1.0 KiB
C#
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 = $"queue{rand.Next()}";
|
|
|
|
channel.QueueDeclare(queue: queueName);
|
|
channel.QueueBind(queue: queueName,
|
|
exchange: "reports",
|
|
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);
|
|
|
|
var reportNumber = message.Split('#')[1];
|
|
Thread.Sleep(2500);
|
|
Console.WriteLine($" [x] Отчет #{reportNumber} создан.");
|
|
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
|
|
};
|
|
channel.BasicConsume(queue: queueName,
|
|
autoAck: false,
|
|
consumer: consumer);
|
|
|
|
Console.WriteLine(" Press [enter] to exit.");
|
|
Console.ReadLine(); |