zampotex
zampotex

Reputation: 327

Rabbitmq retrieve multiple messages using single synchronous call using .NET

Is there a way to receive multiple message using a single synchronous call using .NET?
I've seen question and I've found java class com.rabbitmq.client.QueueingConsumer, but I haven't found such client class in .NET namespaces (RabbitMQ.Client, RabbitMQ.Client.Events)

Upvotes: 7

Views: 5229

Answers (2)

zampotex
zampotex

Reputation: 327

Thanks for answers, but I've found the class which I looked for: RabbitMQ.Client.QueueingBasicConsumer
Simple implementation is:

IEnumerable<T> Get(int maxBatchCount, int getMessageTimeout, int getBatchTimeout)
{
    var result = new List<T>();
    var startTime = DateTime.Now;
    while (result.Count < maxBatchCount)
    {
        var deliverEventArgs = new BasicDeliverEventArgs();
        if ((_consumer as QueueingBasicConsumer).Queue.Dequeue(GetMessageTimeout, out deliverEventArgs))
        {
            var entry = ContractSerializer.Deserialize<T>(deliverEventArgs.Body);
            result.Add(entry);
            _queue.Channel.BasicAck(deliverEventArgs.DeliveryTag, false);
        }
        else
            break;

        if ((DateTime.Now - startTime) >= TimeSpan.FromMilliseconds(getBatchTimeout))
            break;
    }
    return result;
}

Well, of course, you can use Environment.TickCount instead of DateTime.Now

Upvotes: 1

jhilden
jhilden

Reputation: 12449

You can retrieve as many messages as you want using the BasicQoS.PrefetchCount:

var model = _rabbitConnection.CreateModel();
// Configure the Quality of service for the model. Below is how what each setting means.
// BasicQos(0="Dont send me a new message untill I’ve finshed",  _fetchSize = "Send me N messages at a time", false ="Apply to this Model only")
model.BasicQos(0, fetchSize, false);

Note: if you set fetchSize = 20 then it will retrieve the first 20 messages that are currently in the queue. But, once the queue has been emptied it won't wait for 20 messages to build up in the queue, it will start consuming them as fast as possible, grabbing up to 20 at a time.

Hopefully that makes sense.

Upvotes: 4

Related Questions