I'm using https://github.com/videlalvaro/php-amqplib to do some rabbitmq work:
I'm trying to create a blocking version of basic_get, (or a version of basic_consume that I can call repeatedly and only get one msg each time) that will block until a message is ready and then return it instead of returning null if none is on the queue.
When I try to grab a single message with basic_consume things get gummed up and I end up with a bunch of "not ready" but unacked messages. (If I only grab one msg this way, it works every time, if I try to get 2 messages, it gets hung up sometimes and works others)
class Foo {
...
private function blockingGet() {
/*
queue: Queue from where to get the messages
consumer_tag: Consumer identifier
no_local: Don't receive messages published by this consumer.
no_ack: Tells the server if the consumer will acknowledge the messages.
exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
nowait:
callback: A PHP Callback
*/
$this->ch->basic_consume($this->queueName, "consumer_".$this->consumerNum++, false, false, false, false, function($msg) {
$this->msgCache = json_decode($msg->body);
$this->ch->basic_ack($msg->delivery_info['delivery_tag']);
$this->ch->basic_cancel($msg->delivery_info['consumer_tag']);
});
while (count($this->ch->callbacks)) {
$this->ch->wait();
}
return $this->msgCache;
}
}
$q = new Foo();
for ($i = 0; $i < 5; $i++) {
print $q->blockingGet();
}