我想从工作人员的回调中记录工作状态,并在左侧队列中包含一些消息。
到目前为止,我发现的唯一解决方案是获取queue_declare
结果数组的第二个成员,但这应该在每次工作人员启动时调用一次,并且我需要更新每条新消息的信息。
UPD:基于IMSoP 答案的解决方案:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('test1');
echo "[*] Waiting for messages. To exit press CTRL+C\n";
$callback = function ($msg) use ($channel) {
list (, $cn) = $channel->queue_declare('test1', true);
echo ' [x] Received ', $msg->body, " $cn left";
for ($i = 0; $i < $msg->body; ++$i) {
sleep(1);
echo '.';
}
echo "\n";
};
$channel->basic_qos(null, 1, null);
$channel->basic_consume('test1', '', false, true, false, false, $callback);
while (count($channel->callbacks)) {
$channel->wait();
}
出于某种原因,总是给出 0 作为消息计数。