yenta
yenta

Reputation: 1372

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

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

Upvotes: 14

Views: 18175

Answers (2)

cahit beyaz
cahit beyaz

Reputation: 5117

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

Upvotes: 12

tchrikch
tchrikch

Reputation: 2468

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.

Upvotes: 3

Related Questions