php-amqplib loop while there are messages only

807 Views Asked by At

There is a demo consumer:

It loops as long as the channel has callbacks registered

while (count($ch->callbacks)) {
  $ch->wait();
}

The thing is that I need to get not more than 100 messages from the queue for example. If there are only 80 for example it should return just 80 and exit loop.

Thanks

1

There are 1 best solutions below

1
On

Would the following code work for you ?

$callbacks = count($ch->callbacks);

for ($i = 0, $count = $callbacks < 100 ? $callbacks : 100; $i < $count; $i++) {
    $ch->wait();
}