[rabbitmq-discuss] Is there a way to receive multiple message using a single synchronous call ?

Matthias Radestock matthias at rabbitmq.com
Sun Jun 16 22:25:34 BST 2013


On 10/06/13 17:06, Vikram Viswanathan wrote:
> I have another question. If I use basic.consume, register a callback,
> then is there a way where I can can cancel the consumer after N message
> is received ?

1) Set the basic.qos prefetch to N.
2) Create the consumer with basic.consume.
3) As you receive the messages, do not acknowledge them one by one. 
Instead wait for them all to arrive.
4) Cancel the consumer with basic.cancel.
5) Send a single ack with the 'multiple' flag set.

> I tried calling Channel.basicCancel(consumerTag) in the callback, but
> this cancels the consumer only after all messages in the queue is
> received.

That would be because you didn't set the basic.qos prefetch first.

> I do not want to make basic.get() with a timeout because this
> will cause my thread to block for the duration

That wouldn't work anyway since basic.get will not wait for messages to 
arrive in the queue; it returns basic.get-empty instead when the queue 
is empty.



More information about the rabbitmq-discuss mailing list