I have a web service interface that abstracts a RabbitMQ server (don't ask me why, I know it's an unnecessary step, but I have to). That is, I poll messages from the queue through a web service call, not directly over amqp
.
Consuming via basic.consumer
blocks the execution thread till there are messages in the queue. This makes the web service not return.
Code for illustration:
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare(QUEUE_NAME, false, true, false, false);
$ret = array('body' => '');
$callback = function($msg) use ($channel, &$ret) {
$ret['body'] = $msg->body;
/*
Here I would basic.cancel the consumer if there were no messages in the queue
*/
};
$channel->basic_consume(QUEUE_NAME, 'tag', false, true, false, false, $callback);
if (count($channel->callbacks)) {
$channel->wait(); // blocks here...
}
return $ret;