DAS_2024_1/yakovleva_yulia_lab_4/NewTask/NewTask.cs

32 lines
998 B
C#
Raw Permalink Normal View History

2024-10-18 16:11:22 +04:00
using System.Text;
using RabbitMQ.Client;
var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
2024-10-18 16:24:18 +04:00
channel.QueueDeclare(queue: "task_queue",
durable: true,
2024-10-18 16:11:22 +04:00
exclusive: false,
autoDelete: false,
arguments: null);
var message = GetMessage(args);
var body = Encoding.UTF8.GetBytes(message);
2024-10-18 16:24:18 +04:00
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
2024-10-18 16:11:22 +04:00
channel.BasicPublish(exchange: string.Empty,
2024-10-18 16:24:18 +04:00
routingKey: "task_queue",
basicProperties: properties,
2024-10-18 16:11:22 +04:00
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!");
}