我需要创建一个简单的队列管理器来将数字从发送者传递给消费者。 RabbitMQ 提供的 Hello World 教程几乎涵盖了其中的 70%。
但我需要将队列更改为不永远等待传入消息。或者在一定数量的消息后停止等待。我阅读并尝试了其他帖子中的一些解决方案,但它不起作用。
rabbitmq AMQP::consume() - 未定义的方法。还有另一种方法,wait_frame,但它是 protected 。
其他帖子是用 python 写的,我不明白。
<?php
require_once __DIR__ . '/vendor/autoload.php';
require 'config.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
function recieveQueue($queueName){
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
// try{
// $connection->wait_frame(10);
// }catch(AMQPConnectionException $e){
// echo "asdasd";
// }
$channel = $connection->channel();
$channel->queue_declare($queueName, false, false, false, false);
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
$callback = function($msg) {
echo " [x] Received ", $msg->body, "\n";
};
// $tag = uniqid() . microtime(true);
// $queue->consume($callback, $flags, $tag);
$channel->basic_consume($queueName, '', false, true, false, false, $callback);
// $channel->cancel($tag);
while(count($channel->callbacks)) {
$channel->wait();
}
echo "\nfinish";
}
recieveQueue('vtiger');
?>
最佳答案
修改while循环中的wait():
$timeout = 55;
while(count($channel->callbacks)) {
$channel->wait(null, false, $timeout);
}
关于PHP RabbitMQ setTimeout 或其他停止等待队列的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33930923/