150 lines
4.3 KiB
Markdown
150 lines
4.3 KiB
Markdown
# Отчёт по лабораторной работе №4
|
||
|
||
Выполнил: студент гр. ИСЭбд-41 Михайлов Ю.С.
|
||
|
||
## Прохождение туториалов
|
||
|
||
Установил RabbitMQ, Erlang и зашел в брокер под гостем по адресу http://localhost:15672/#/
|
||
|
||
Туториал 1:
|
||

|
||

|
||

|
||
|
||
Туториал 2:
|
||

|
||

|
||

|
||

|
||
|
||
Туториал 3:
|
||

|
||

|
||

|
||
|
||
## Разработка демонстрационных приложений
|
||
|
||
Предметная область - оформление заявлений на стипендию в университете. Разработано три приложения согласно предметной области.
|
||
|
||
1. Publisher:
|
||
```
|
||
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: "report", type: ExchangeType.Fanout);
|
||
Random rand = new Random();
|
||
foreach (var item in Enumerable.Range(0, 1000))
|
||
{
|
||
var message = rand.Next().ToString();
|
||
|
||
var body = Encoding.UTF8.GetBytes(message);
|
||
channel.BasicPublish(exchange: "report",
|
||
routingKey: string.Empty,
|
||
basicProperties: null,
|
||
body: body);
|
||
|
||
Console.WriteLine($" [x] Поступило заявление {message}");
|
||
await Task.Delay(500);
|
||
}
|
||
|
||
Console.WriteLine(" Press [enter] to exit.");
|
||
Console.ReadLine();
|
||
|
||
static string GetMessage(string[] args)
|
||
{
|
||
return ((args.Length > 0) ? string.Join(" ", args) : "info: Принято!");
|
||
}
|
||
```
|
||
2. Consumer 1:
|
||
```
|
||
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: "accepted");
|
||
channel.QueueBind(queue: "accepted",
|
||
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);
|
||
|
||
string outputText = $"Заявление принято к обработке {message}";
|
||
Console.WriteLine($" [x] Done. {outputText}");
|
||
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
|
||
};
|
||
channel.BasicConsume(queue: "accepted",
|
||
autoAck: false,
|
||
consumer: consumer);
|
||
|
||
Console.WriteLine(" Press [enter] to exit.");
|
||
Console.ReadLine();
|
||
```
|
||
|
||
3. Consumer 3:
|
||
```
|
||
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();
|
||
```
|
||
|
||
## Результаты выполнения работы
|
||
|
||
Запуск каждой программы:
|
||

|
||

|
||

|
||
|
||
Результаты обработки:
|
||

|
||

|
||
|
||
Вывод: Consumer_2 нагружает меньше памяти, чем Consumer_1 и принимает сообщения гораздо быстрее, тем самым не позволяя очереди накапливать огромное количество сообщений
|