我正在使用https://github.com/videlalvaro/php-amqplib做一些rabbitmq工作:
我试图创建一个basic_get的阻止版本,(或者我可以重复调用的basic_consume版本,每次只获得一个msg),它将阻塞,直到消息准备就绪,然后返回而不是如果队列中没有,则返回null。
当我尝试使用basic_consume抓住一条消息时,事情就会被搞砸了,我最终得到了一堆"没有准备好"但没有打包的消息。 (如果我这样只抓一个消息,它每次都有效,如果我试图收到2条消息,它会被挂起并且有时会工作)
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();
}
答案 0 :(得分:0)
通过将收到的消息保存在传递给?- courseAvg([9,9,8],N).
N = 8.75.
?- courseAvg([9,8,8],N).
N = 8.5.
?- courseAvg([9,7,8],N).
N = 8.25.
的回调参数的闭包中,然后在$channel->basic_consume()
之后处理它,我已经实现了类似于此后的内容。如果收到消息(或者如果设置了超时参数并且达到超时),则$channel->wait()
将返回控制。尝试类似下面的内容:
wait()