DAS_2024_1/aleikin_artem_lab_4/Publisher/Publisher.cs

32 lines
971 B
C#
Raw Normal View History

2024-11-20 04:43:04 +04:00
using RabbitMQ.Client;
using System.Text;
var factory = new ConnectionFactory
{
HostName = "rabbitmq",
UserName = "admin",
Password = "admin"
};
using var connection = await factory.CreateConnectionAsync();
Console.WriteLine("Connection established.");
using var channel = await connection.CreateChannelAsync();
Console.WriteLine("Channel created.");
await channel.ExchangeDeclareAsync(exchange: "logs_exchange", type: ExchangeType.Fanout);
while (true)
{
var message = $"Event: {GenerateRandomEvent()}";
var body = Encoding.UTF8.GetBytes(message);
await channel.BasicPublishAsync(exchange: "logs_exchange", routingKey: string.Empty, body: body);
Console.WriteLine($"[Publisher] Sent: {message}");
await Task.Delay(1000);
}
static string GenerateRandomEvent()
{
var events = new[] { "Order Received", "User Message", "Create Report" };
return events[new Random().Next(events.Length)] + " #" + new Random().Next(0, 99);
}