jed*_*edi 9 php amqp wait rabbitmq php-amqplib
我需要创建一个简单的队列管理器来将数字从发件人传递给消费者.RabbitMQ提供的Hello World教程几乎覆盖了70%.
但我需要将队列更改为永远不会等待传入的消息.或者在一定数量的消息后停止等待.我阅读并尝试了其他帖子中的一些解决方案,但它不起作用.
rabbitmq AMQP :: consume() - 未定义的方法.还有另一种方法,wait_frame但它受到保护.
和其他帖子是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');
?>
Run Code Online (Sandbox Code Playgroud)
修改while循环中的wait():
$timeout = 55;
while(count($channel->callbacks)) {
$channel->wait(null, false, $timeout);
}
Run Code Online (Sandbox Code Playgroud)
wait函数只适用于套接字,我们必须捕获异常:
$timeout = 5;
while (count($channel->callbacks)) {
try{
$channel->wait(null, false , $timeout);
}catch(\PhpAmqpLib\Exception\AMQPTimeoutException $e){
$channel->close();
$connection->close();
exit;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6243 次 |
| 最近记录: |