Reputation: 423
I'm working on .net core project which send thousand messages to Rabbitmq. Number of messages is between 10K to 500K.
I declare a loop where I call BasicPublish
method of RabbitMQ.Client library to send my message. I need to verify the message was sent and received by the broker and according to the result I update a database.
I thought to use _channel.BasicAcks += _channel_BasicAcks
, _channel.ConfirmSelect();
and _channel.WaitForConfirmsOrDie();
to trigger event _channel_BasicAcks
to know if broker received message.
Example of code :
_channel.BasicAcks += _channel_BasicAcks;
_channel.BasicNacks += _channel_BasicNacks;
_channel.QueueDeclare(queue: "myQueue",
durable: true,
exclusive: false,
autoDelete: false,
arguments: arguments);
_channel.ConfirmSelect();
for (int i = 0; i < 500000; i++)
{
_channel.BasicPublish("exchange", "routingKey", null, Encoding.UTF8.GetBytes("some data"));
_channel.WaitForConfirmsOrDie();
}
private void _channel_BasicAcks(object? sender, BasicAckEventArgs e)
{
//some code here....
}
Is it the best way to achieve this or there is another solution for large volume of messages ?
Upvotes: 1
Views: 863
Reputation: 9627
WaitForConfirmsOrDie
is synchronous and slow. You should instead keep track of outstanding confirmations (via a concurrent hash table, for instance) and deal with them in your _channel_BasicAcks
and _channel_BasicNacks
callbacks.
If you receive a Nack, you should re-publish the message. See the caveat in the tutorial to which I linked below.
If you receive an Ack, you know that message has been confirmed and you can remove it from your tracking data structure. Be sure to check to see if RabbitMQ acked multiple messages.
We have example code for this:
https://www.rabbitmq.com/tutorials/tutorial-seven-dotnet.html
Upvotes: 2