From a830cb219873674cc1b90caf2b415851851c891b Mon Sep 17 00:00:00 2001 From: Zara28 Date: Mon, 23 Sep 2024 22:42:39 +0400 Subject: [PATCH] =?UTF-8?q?=D1=82=D1=83=D1=82=D0=BE=D1=80=D0=B8=D0=B0?= =?UTF-8?q?=D0=BB=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 9 +++++ dozorova_alena_lab_4/EmitLog/EmitLog..csproj | 15 +++++++ dozorova_alena_lab_4/EmitLog/Program.cs | 24 ++++++++++++ dozorova_alena_lab_4/NewTask/NewTask.csproj | 14 +++++++ dozorova_alena_lab_4/NewTask/Program.cs | 32 +++++++++++++++ dozorova_alena_lab_4/ReceiveLogs/Program.cs | 2 + .../ReceiveLogs/ReceiveLogs.csproj | 14 +++++++ dozorova_alena_lab_4/Worker/Program.cs | 39 +++++++++++++++++++ dozorova_alena_lab_4/Worker/Worker.csproj | 14 +++++++ 9 files changed, 163 insertions(+) create mode 100644 dozorova_alena_lab_4/EmitLog/EmitLog..csproj create mode 100644 dozorova_alena_lab_4/EmitLog/Program.cs create mode 100644 dozorova_alena_lab_4/NewTask/NewTask.csproj create mode 100644 dozorova_alena_lab_4/NewTask/Program.cs create mode 100644 dozorova_alena_lab_4/ReceiveLogs/Program.cs create mode 100644 dozorova_alena_lab_4/ReceiveLogs/ReceiveLogs.csproj create mode 100644 dozorova_alena_lab_4/Worker/Program.cs create mode 100644 dozorova_alena_lab_4/Worker/Worker.csproj diff --git a/.gitignore b/.gitignore index ea4af46..628dfee 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,12 @@ /dozorova_alena_lab_4/Receive/obj /dozorova_alena_lab_4/Send/bin /dozorova_alena_lab_4/Send/obj +/dozorova_alena_lab_4/EmitLog/.vs +/dozorova_alena_lab_4/EmitLog/obj +/dozorova_alena_lab_4/NewTask/.vs +/dozorova_alena_lab_4/NewTask/bin +/dozorova_alena_lab_4/NewTask/obj +/dozorova_alena_lab_4/ReceiveLogs/obj +/dozorova_alena_lab_4/Worker/.vs +/dozorova_alena_lab_4/Worker/bin +/dozorova_alena_lab_4/Worker/obj diff --git a/dozorova_alena_lab_4/EmitLog/EmitLog..csproj b/dozorova_alena_lab_4/EmitLog/EmitLog..csproj new file mode 100644 index 0000000..efea465 --- /dev/null +++ b/dozorova_alena_lab_4/EmitLog/EmitLog..csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + EmitLog_ + enable + enable + + + + + + + diff --git a/dozorova_alena_lab_4/EmitLog/Program.cs b/dozorova_alena_lab_4/EmitLog/Program.cs new file mode 100644 index 0000000..73120f4 --- /dev/null +++ b/dozorova_alena_lab_4/EmitLog/Program.cs @@ -0,0 +1,24 @@ +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: "logs", type: ExchangeType.Fanout); + +var message = GetMessage(args); +var body = Encoding.UTF8.GetBytes(message); +channel.BasicPublish(exchange: "logs", + routingKey: string.Empty, + basicProperties: null, + body: body); +Console.WriteLine($" [x] Sent {message}"); + +Console.WriteLine(" Press [enter] to exit."); +Console.ReadLine(); + +static string GetMessage(string[] args) +{ + return ((args.Length > 0) ? string.Join(" ", args) : "info: Hello World!"); +} \ No newline at end of file diff --git a/dozorova_alena_lab_4/NewTask/NewTask.csproj b/dozorova_alena_lab_4/NewTask/NewTask.csproj new file mode 100644 index 0000000..f72406b --- /dev/null +++ b/dozorova_alena_lab_4/NewTask/NewTask.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/dozorova_alena_lab_4/NewTask/Program.cs b/dozorova_alena_lab_4/NewTask/Program.cs new file mode 100644 index 0000000..0928eb7 --- /dev/null +++ b/dozorova_alena_lab_4/NewTask/Program.cs @@ -0,0 +1,32 @@ +using System.Text; +using RabbitMQ.Client; + +var factory = new ConnectionFactory { HostName = "localhost" }; +using var connection = factory.CreateConnection(); +using var channel = connection.CreateModel(); + +channel.QueueDeclare(queue: "task_queue", + durable: true, + exclusive: false, + autoDelete: false, + arguments: null); + +var message = GetMessage(args); +var body = Encoding.UTF8.GetBytes(message); + +var properties = channel.CreateBasicProperties(); +properties.Persistent = true; + +channel.BasicPublish(exchange: string.Empty, + routingKey: "task_queue", + basicProperties: properties, + body: body); +Console.WriteLine($" [x] Sent {message}"); + +Console.WriteLine(" Press [enter] to exit."); +Console.ReadLine(); + +static string GetMessage(string[] args) +{ + return ((args.Length > 0) ? string.Join(" ", args) : "Hello World!"); +} \ No newline at end of file diff --git a/dozorova_alena_lab_4/ReceiveLogs/Program.cs b/dozorova_alena_lab_4/ReceiveLogs/Program.cs new file mode 100644 index 0000000..3751555 --- /dev/null +++ b/dozorova_alena_lab_4/ReceiveLogs/Program.cs @@ -0,0 +1,2 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); diff --git a/dozorova_alena_lab_4/ReceiveLogs/ReceiveLogs.csproj b/dozorova_alena_lab_4/ReceiveLogs/ReceiveLogs.csproj new file mode 100644 index 0000000..f72406b --- /dev/null +++ b/dozorova_alena_lab_4/ReceiveLogs/ReceiveLogs.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/dozorova_alena_lab_4/Worker/Program.cs b/dozorova_alena_lab_4/Worker/Program.cs new file mode 100644 index 0000000..d8b86be --- /dev/null +++ b/dozorova_alena_lab_4/Worker/Program.cs @@ -0,0 +1,39 @@ +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: "task_queue", + durable: true, + exclusive: false, + autoDelete: false, + arguments: null); + +channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); + +Console.WriteLine(" [*] Waiting for messages."); + +var consumer = new EventingBasicConsumer(channel); +consumer.Received += (model, ea) => +{ + byte[] body = ea.Body.ToArray(); + var message = Encoding.UTF8.GetString(body); + Console.WriteLine($" [x] Received {message}"); + + int dots = message.Split('.').Length - 1; + Thread.Sleep(dots * 1000); + + Console.WriteLine(" [x] Done"); + + // here channel could also be accessed as ((EventingBasicConsumer)sender).Model + channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); +}; +channel.BasicConsume(queue: "task_queue", + autoAck: false, + consumer: consumer); + +Console.WriteLine(" Press [enter] to exit."); +Console.ReadLine(); \ No newline at end of file diff --git a/dozorova_alena_lab_4/Worker/Worker.csproj b/dozorova_alena_lab_4/Worker/Worker.csproj new file mode 100644 index 0000000..f72406b --- /dev/null +++ b/dozorova_alena_lab_4/Worker/Worker.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + +