diff --git a/tasks/mutriskov-ds/lab_4/MainTask/Consumer-1/Program.cs b/tasks/mutriskov-ds/lab_4/MainTask/Consumer-1/Program.cs index 43ca21a..d2d0f8e 100644 --- a/tasks/mutriskov-ds/lab_4/MainTask/Consumer-1/Program.cs +++ b/tasks/mutriskov-ds/lab_4/MainTask/Consumer-1/Program.cs @@ -6,31 +6,29 @@ var factory = new ConnectionFactory { HostName = "localhost" }; using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); -channel.ExchangeDeclare(exchange: "reports", type: ExchangeType.Fanout); +Random rand = new Random(); +string queueName = $"queue1"; -channel.QueueDeclare("queue1"); -channel.QueueBind(queue: "queue1", - exchange: "reports", - routingKey: string.Empty); +channel.QueueDeclare(queue: queueName); +channel.QueueBind(queue: queueName, + exchange: "reports", + routingKey: string.Empty); -Console.WriteLine(" [*] Ожидание сообщения"); +Console.WriteLine(" [*] Waiting for messages."); var consumer = new EventingBasicConsumer(channel); -consumer.Received += (model, ea) => +consumer.Received += async (model, ea) => { byte[] body = ea.Body.ToArray(); var message = Encoding.UTF8.GetString(body); - Thread.Sleep(2500); - var reportNumber = message.Split('#')[1]; Console.WriteLine($" [x] Отчет #{reportNumber} создан."); - channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); }; -channel.BasicConsume(queue: "queue1", - autoAck: true, - consumer: consumer); +channel.BasicConsume(queue: queueName, + autoAck: false, + consumer: consumer); -Console.WriteLine(" Нажмите [enter] чтобы выйти."); +Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); \ No newline at end of file diff --git a/tasks/mutriskov-ds/lab_4/MainTask/Consumer-2/Program.cs b/tasks/mutriskov-ds/lab_4/MainTask/Consumer-2/Program.cs index 08214e0..d77e5e6 100644 --- a/tasks/mutriskov-ds/lab_4/MainTask/Consumer-2/Program.cs +++ b/tasks/mutriskov-ds/lab_4/MainTask/Consumer-2/Program.cs @@ -6,27 +6,30 @@ var factory = new ConnectionFactory { HostName = "localhost" }; using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); -channel.QueueDeclare("queue2"); -channel.QueueBind(queue: "queue2", - exchange: "reports", - routingKey: string.Empty); +Random rand = new Random(); +string queueName = $"queue{rand.Next()}"; -Console.WriteLine(" [*] Ожидание сообщения"); +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 += (model, ea) => +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: "queue2", - autoAck: true, - consumer: consumer); +channel.BasicConsume(queue: queueName, + autoAck: false, + consumer: consumer); -Console.WriteLine(" Нажмите [enter] чтобы выйти."); +Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); \ No newline at end of file diff --git a/tasks/mutriskov-ds/lab_4/MainTask/Publisher/Program.cs b/tasks/mutriskov-ds/lab_4/MainTask/Publisher/Program.cs index aac9292..6612a66 100644 --- a/tasks/mutriskov-ds/lab_4/MainTask/Publisher/Program.cs +++ b/tasks/mutriskov-ds/lab_4/MainTask/Publisher/Program.cs @@ -5,17 +5,21 @@ var factory = new ConnectionFactory { HostName = "localhost" }; using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); -channel.ExchangeDeclare("reports", ExchangeType.Fanout); - -var rand = new Random(); - -while (true) +channel.ExchangeDeclare(exchange: "reports", type: ExchangeType.Fanout); +Random rand = new Random(); +foreach (var item in Enumerable.Range(0, 1000)) { var message = $"Необходимо создать отчет #{rand.Next()}"; + var body = Encoding.UTF8.GetBytes(message); + channel.BasicPublish(exchange: "reports", + routingKey: string.Empty, + basicProperties: null, + body: body); - channel.BasicPublish("reports", string.Empty, null, body); - Console.WriteLine($" [x] Отправлено \"{message}\""); + Console.WriteLine($" [x] Поступила работа {message}"); + await Task.Delay(1000); +} - Thread.Sleep(1000); -} \ No newline at end of file +Console.WriteLine(" Press [enter] to exit."); +Console.ReadLine(); diff --git a/tasks/mutriskov-ds/lab_4/README.md b/tasks/mutriskov-ds/lab_4/README.md new file mode 100644 index 0000000..2323995 --- /dev/null +++ b/tasks/mutriskov-ds/lab_4/README.md @@ -0,0 +1,149 @@ +# Отчет по лабораторной работе №4 + +Выполнил студент гр. ИСЭбд-41 Мутрисков Д.С. + +## Прохождение tutorial + +Установил rabbitMQ server, erlang и зашел в брокер под гостем по http://localhost:15672/#/ + +Туториал 1: + +![](images/tut1.png) + +Туториал 2: + +![](images/tut2.png) + +Туториал 3: + +![](images/tut3.png) + +## Разработка демонстрационных приложений + +Предметная область: Офис. Производитель - начальник, который посылает задачу на составление отчета, Потребители - работники офиса. + +1. Publisher + +```cs +using System.Text; +using RabbitMQ.Client; + +var factory = new ConnectionFactory { HostName = "localhost" }; +using var connection = factory.CreateConnection(); +using var channel = connection.CreateModel(); + +channel.ExchangeDeclare(exchange: "reports", type: ExchangeType.Fanout); +Random rand = new Random(); +foreach (var item in Enumerable.Range(0, 1000)) +{ + var message = $"Необходимо создать отчет #{rand.Next()}"; + + var body = Encoding.UTF8.GetBytes(message); + channel.BasicPublish(exchange: "reports", + routingKey: string.Empty, + basicProperties: null, + body: body); + + Console.WriteLine($" [x] Поступила работа {message}"); + await Task.Delay(1000); +} + +Console.WriteLine(" Press [enter] to exit."); +Console.ReadLine(); + +``` + +2. Consumer 1. + +```cs +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 = $"queue1"; + +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]; + 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(); +``` + +3. Consumer 2. + +```cs +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(); +``` + +## Результаты выполнения лабораторной работы + +Выполнение с одним потребителем с задержкой в выполении работы. + +![](images/c1-1.png) + +Очередь с работником без задержки в выполенении работы обрабатывается моментально и несет минимальную нагрузку. +Очередь работника с задержкой в выполении работы довольно быстро загружается. + +Пример с двумя потребителями-курьерами + +![](images/c1-2.png) + +Очередь самовывоза не изменилась. +На очереди курьеров закономерно тратится в 2 раза больше времени \ No newline at end of file diff --git a/tasks/mutriskov-ds/lab_4/images/c1-1.png b/tasks/mutriskov-ds/lab_4/images/c1-1.png new file mode 100644 index 0000000..dd952e6 Binary files /dev/null and b/tasks/mutriskov-ds/lab_4/images/c1-1.png differ diff --git a/tasks/mutriskov-ds/lab_4/images/c1-2.png b/tasks/mutriskov-ds/lab_4/images/c1-2.png new file mode 100644 index 0000000..b07abca Binary files /dev/null and b/tasks/mutriskov-ds/lab_4/images/c1-2.png differ