PHP RabbitMQ setTimeout 或其他停止等待队列的选项
PHP RabbitMQ setTimeout or other option to stop waiting for queue
我需要创建一个简单的队列管理器来将数字从发送者传递给消费者。 RabbitMQ 提供的 Hello World 教程几乎涵盖了其中的 70%
但我需要将队列更改为不永远等待传入消息。或者在一定数量的消息后停止等待。我阅读并尝试了其他 post 的一些解决方案,但它不起作用。
rabbitmq AMQP::consume() - 未定义的方法。还有另一种方法,wait_frame 但它是受保护的。
和其他 post 在 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);
}
这就是我向队列发出信号以停止使用传入消息的方式。
然而,这可能不是正确的做法,因为它给出了一个错误,而不是正确地出现。
如果有更好的答案,请提出更好的答案。
$callback = function($msg) {
echo " [x] Received ", $msg->body, "\n";
// if queue recieve 'stop', stop consume anymore messages
if ($msg->body == 'stop'){
$channel->basic_cancel($queueName);
}
};
$channel->basic_consume($queueName, '', false, true, false, false, $callback);
$timeout = 10;
while(count($channel->callbacks)) {
// $channel->wait(null, false, $timeout);
$channel->wait();
}
等待函数 只适用于套接字,我们必须捕获异常:
$timeout = 5;
while (count($channel->callbacks)) {
try{
$channel->wait(null, false , $timeout);
}catch(\PhpAmqpLib\Exception\AMQPTimeoutException $e){
$channel->close();
$connection->close();
exit;
}
}
我需要创建一个简单的队列管理器来将数字从发送者传递给消费者。 RabbitMQ 提供的 Hello World 教程几乎涵盖了其中的 70%
但我需要将队列更改为不永远等待传入消息。或者在一定数量的消息后停止等待。我阅读并尝试了其他 post 的一些解决方案,但它不起作用。
rabbitmq AMQP::consume() - 未定义的方法。还有另一种方法,wait_frame 但它是受保护的。
和其他 post 在 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);
}
这就是我向队列发出信号以停止使用传入消息的方式。
然而,这可能不是正确的做法,因为它给出了一个错误,而不是正确地出现。
如果有更好的答案,请提出更好的答案。
$callback = function($msg) {
echo " [x] Received ", $msg->body, "\n";
// if queue recieve 'stop', stop consume anymore messages
if ($msg->body == 'stop'){
$channel->basic_cancel($queueName);
}
};
$channel->basic_consume($queueName, '', false, true, false, false, $callback);
$timeout = 10;
while(count($channel->callbacks)) {
// $channel->wait(null, false, $timeout);
$channel->wait();
}
等待函数 只适用于套接字,我们必须捕获异常:
$timeout = 5;
while (count($channel->callbacks)) {
try{
$channel->wait(null, false , $timeout);
}catch(\PhpAmqpLib\Exception\AMQPTimeoutException $e){
$channel->close();
$connection->close();
exit;
}
}