туториал 2
This commit is contained in:
parent
9d0fa199f7
commit
a830cb2198
9
.gitignore
vendored
9
.gitignore
vendored
@ -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
|
||||
|
15
dozorova_alena_lab_4/EmitLog/EmitLog..csproj
Normal file
15
dozorova_alena_lab_4/EmitLog/EmitLog..csproj
Normal file
@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>EmitLog_</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
24
dozorova_alena_lab_4/EmitLog/Program.cs
Normal file
24
dozorova_alena_lab_4/EmitLog/Program.cs
Normal file
@ -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!");
|
||||
}
|
14
dozorova_alena_lab_4/NewTask/NewTask.csproj
Normal file
14
dozorova_alena_lab_4/NewTask/NewTask.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
32
dozorova_alena_lab_4/NewTask/Program.cs
Normal file
32
dozorova_alena_lab_4/NewTask/Program.cs
Normal file
@ -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!");
|
||||
}
|
2
dozorova_alena_lab_4/ReceiveLogs/Program.cs
Normal file
2
dozorova_alena_lab_4/ReceiveLogs/Program.cs
Normal file
@ -0,0 +1,2 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
14
dozorova_alena_lab_4/ReceiveLogs/ReceiveLogs.csproj
Normal file
14
dozorova_alena_lab_4/ReceiveLogs/ReceiveLogs.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
39
dozorova_alena_lab_4/Worker/Program.cs
Normal file
39
dozorova_alena_lab_4/Worker/Program.cs
Normal file
@ -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();
|
14
dozorova_alena_lab_4/Worker/Worker.csproj
Normal file
14
dozorova_alena_lab_4/Worker/Worker.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user