Junior — Middle
Have you implemented data exchange using RabbitMQ in your projects?
sobes.tech AI
Answer from AI
Yes, in my C# projects, I have used RabbitMQ to organize message exchange between services. I usually used the RabbitMQ.Client library for connecting and working with the broker.
Here's an example of a simple message sender:
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);
}
For the consumer, I implemented message processing with acknowledgment (ack) to ensure reliable delivery and processing.