Rabbitmq retrieve multiple messages using single synchronous call
You can use a QueueingConsumer
implementation of Consumer
interface which allows you to retrieve several messages in a single request.
QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
channel.basicConsume(plugin.getQueueName(), false, queueingConsumer);
for(int i = 0; i < 10; i++){
QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery(100);//read timeout in ms
if(delivery == null){
break;
}
}
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 basic.consume which will push the messages to the client avoiding multiple round trips. acks
are asynchronous so your client won't be waiting for the server to respond. basic.consume
also has the benefit of allowing RabbitMQ to redeliver the message if the client disconnects, something that basic.get
cannot do. This can be turned off as well setting no-ack
to true
.
Setting basic.qos
prefetch-count
will set the number of messages to push to the client at any time. If there isn't a message waiting on the client side (which would return immediately) client libraries tend to block with an optional timeout.