Ian Jowett
Ian Jowett

Reputation: 324

Limit how many RabbitMQ messages I get from the server

I have written a wrapper around RabbitMQ, and everything is working fine, too well actually, I am receiving messages quicker than I can process them. How do I limit how many messages I get from the queue or better, only consume and process one at a time?

public void Consume()
{
  if (_channel != null)
   {
     // setup a listener for new messages
     var consumer = new EventingBasicConsumer(_channel);

     consumer.Received += (model, ea) =>
     {
       var body = ea.Body.ToArray();
       var message = Encoding.UTF8.GetString(body);
       var evt = new MessageEventArgs(body, message);

       OnMessageReceived(evt);
      };
     _channel.BasicConsume(queue: _queue, autoAck: true, consumer: consumer);
    }
}

Upvotes: 0

Views: 1069

Answers (1)

itshagunrathore
itshagunrathore

Reputation: 157

To limit the messages consumed

//limit to 5 messages
channel.BasicQos(0, 5, false);

After this you can call the BasicConsume method with noAck parameter to false.

channel.BasicConsume(queue: _queue, autoAck: false, consumer: consumer);

Upvotes: 2

Related Questions