Rabbitmq retrieve multiple messages using single synchronous call

后端 未结 4 1281
灰色年华
灰色年华 2021-02-13 19:04

Is there a way to receive multiple message using a single synchronous call ?

When I know that there are N messages( N could be a small value less than 10) in the queue,

4条回答
  •  终归单人心
    2021-02-13 19:34

    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;
        }
     }
    

提交回复
热议问题