Reputation: 324
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
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