Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to publish multiple messages at once using the RabbitMQ client for C#?

Tags:

c#

rabbitmq

Right now, our publishing code for large amounts of messages looks like so:

foreach (var message in messages)
{
    publisher.Publish(message);
}

Does there exist the ability to send more than one message over the channel at once?

publisher.Publish(messages);

or as so if we chunk

var chunks = messages.Chunk(100);
foreach (var chunk in chunks)
{
    publisher.Publish(chunk);
}
like image 755
yenta Avatar asked Feb 11 '15 09:02

yenta


2 Answers

With current version of RabbitMq(3.8.2) you can send batch messages as below for c# client sdk:

basicPublishBatch = channel.CreateBasicPublishBatch();
basicPublishBatch.Add("exchange", "routeKey", false, null, new byte[]{1});
basicPublishBatch.Add("exchange", "routeKey", false, null, new byte[]{1});
basicPublishBatch.Publish();

Check this PR: https://github.com/rabbitmq/rabbitmq-dotnet-client/pull/368

like image 109
cahit beyaz Avatar answered Oct 08 '22 06:10

cahit beyaz


For RabbitMQ the AMQP protocol is asynchronous for produce and consume operations so it is not clear how to benefit from batch consumer endpoint given out of the box.

What you can do is to create endpoints for chunked messages and process them inside workflow if you can speed up operations. So one solution would be for example to have batching component included before publisher class and send custom messages.

like image 20
tchrikch Avatar answered Oct 08 '22 04:10

tchrikch