Sobes.tech
Junior — Middle

Implementovali ste výmenu dát s RabbitMQ vo svojich projektoch?

sobes.tech AI

Odpoveď od AI

Áno, v projektoch v C# som použil RabbitMQ na organizáciu výmeny správ medzi službami. Zvyčajne som používal knižnicu RabbitMQ.Client na pripojenie a prácu s brokerom.

Príklad jednoduchého odosielateľa správ:

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);

    string message = "Hello RabbitMQ!";
    var body = Encoding.UTF8.GetBytes(message);

    var properties = channel.CreateBasicProperties();
    properties.Persistent = true;

    channel.BasicPublish(exchange: "", routingKey: "task_queue", basicProperties: properties, body: body);
    Console.WriteLine(" [x] Sent {0}", message);
}

Pre príjemcu som implementoval spracovanie správ s potvrdením (ack), aby som zabezpečil spoľahlivosť doručenia a spracovania.