Rabbitmq retrieve multiple messages using single synchronous call using .NET

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.


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


Comments

  1. Erik

    • 2018/12/11

    You can retrieve as many messages as you want using the BasicQoS.PrefetchCount : var model = _rabbitConnection.

  2. Cannon

    • 2017/3/6

    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)

  3. Judah

    • 2017/8/11

    RabbitMQ's basic.get doesn't support multiple messages unfortunately as seen in the docs. The preferred method to retrieve multiple messages is to use 

  4. Mathew

    • 2016/3/2

    Rabbitmq consume multiple messages. Rabbitmq retrieve multiple messages using single synchronous call , round trips. acks are asynchronous so your client won't be waiting for the server to respond. RabbitMQ's basic.get doesn't support multiple messages unfortunately as seen in the docs.

  5. Mac

    • 2018/1/8

    Callback queue. In general doing RPC over RabbitMQ is easy. A client sends a request message and a server replies with a response message. In order to receive 

  6. Guillermo

    • 2015/6/19

    Rabbitmq consume multiple messages. Rabbitmq retrieve multiple messages using single synchronous call , round trips. acks are asynchronous so your client won't be waiting for the server to respond. RabbitMQ's basic.get doesn't support multiple messages unfortunately as seen in the docs.

  7. Hughes

    • 2017/11/29

    NET client, first instantiate a ConnectionFactory and configure it to use It is also possible to retrieve individual messages on demand ("pull API" 

  8. Dean

    • 2020/9/3

    Overview. This guide covers RabbitMQ .NET/C# client and its public API. It assumes that the most recent major version of the client is used and the reader is familiar with the basics. Key sections of the guide are: Dependencies. Important interfaces and classes in the public API. Connecting to RabbitMQ. Connection and Channel Lifespan.

  9. Perez

    • 2020/6/20

    With so many patterns available for implementing messaging in your applications, it's only appropriate Get RPC request, RabbitMQ replies with a Basic.

  10. Eddie

    • 2015/2/4

    RabbitMQ speaks multiple protocols. This tutorial uses AMQP 0-9-1, which is an open, general-purpose protocol for messaging. There are a number of clients for RabbitMQ in many different languages. We'll use the .NET client provided by RabbitMQ. The client supports .NET Core as well as .NET Framework 4.5.1+.

  11. Kamryn

    • 2015/10/25

    Ideally, you should have one connection per process, and then use one Having many messages in a queue places a heavy load on RAM usage.

  12. Leonardo

    • 2017/9/22

    It’s quite common to do some sort of I/O operation (e.g. REST call) whenever a message is consumed by a RabbitMQ client. This should be done asynchronously, but it’s not as simple as changing the event handling code to async void. In “The Dangers of async void Event Handlers“, I explained how making an event … Continue reading Asynchronous RabbitMQ Consumers in .NET →

  13. Royce

    • 2019/11/3

    It also takes time to sync messages between nodes in the cluster after a restart. Use Quorum Queues. Perhaps one of the most significant changes 

  14. Greco

    • 2015/11/8

    The first thing we need to do in order to use RabbitMQ is to setup an exchange. The exchange is where we are going to send our messages. In order to setup and exchange, we need to first connect to our server. We can connect to the server by using the “RabbitMQ.Client” namespace and setting up a ConnectionFactory class:

  15. Zeke

    • 2019/6/19

    NET using Apache Qpid in addition to RabbitMQ. each call to createConnection() creates a new connection (or retrieves an idle one from the cache).

  16. Izaiah

    • 2016/6/11

    NET. 53. 5.1. Apache Qpid Messaging API. 53. 5.2. AMQP.NET Lite All other message attributes can be retrieved using the usual getter methods.

  17. Theodore

    • 2019/3/8

    The RabbitMQ component allows you to produce and consume messages from RabbitMQ instances. Using the RabbitMQ AMQP client, this component offers a pure 

Comments are closed.

Recent Posts