distributed-computing/tasks/kazakov-ev/lab_4/MainTask/Consumer1/Consumer1.cs
2023-12-18 12:22:22 +03:00

31 lines
1001 B
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();
channel.QueueDeclare(queue: "mail");
channel.QueueBind(queue: "mail",
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);
string outputText = $"Письмо было отправлено почтой {message}";
Console.WriteLine($" [x] Сделано. {outputText}");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: "mail",
autoAck: false,
consumer: consumer);
Console.WriteLine(" Нажми [enter] чтобы выйти.");
Console.ReadLine();