RabbitMQ和Yii控制台回调错误

时间:2012-02-07 03:57:12

标签: php yii rabbitmq

我试图让RabbitMQ使用Yii控制台来发送交易电子邮件,但是我遇到了让PHP-AMQPLib库在Yii中工作的问题。我的代码如下:

<?php
class RabbitMqCommand extends CConsoleCommand {

    public function actionSendMail() {
        require_once ('/htdocs/code/wwwroot/protected/components/php-amqplib/amqp.inc');

        $conn = new AMQPConnection ( 'localhost', 5672, 'guest', '123456', '/' );
        $ch = $conn->channel ();

        $ch->queue_declare ( 'msgs', false, true, false, false );

        $ch->exchange_declare ( 'router', 'direct', false, true, false );

        $ch->queue_bind ( 'msgs', 'router');

        $ch->basic_consume ( 'msgs', 'consumer', false, false, false, false, 'processMessage' );

            // Loop as long as the channel has callbacks registered
        while ( count ( $ch->callbacks ) ) {
            $ch->wait ();
        }

        $ch->close();
        $conn->close();


    }

    public function processMessage($msg) 
        {
           //process and send email
        }

当我尝试从命令行执行此代码时,如下所示:

php -q /htdocs/code/wwwroot/protected/yiic RabbitMQ SendMail

我收到以下错误消息:

PHP Error[2]: call_user_func() expects parameter 1 to be a valid callback, function 'processMessage' not found or invalid function name
    in file /htdocs/code/wwwroot/protected/components/php-amqplib/amqp.inc at line 1390
#0 /htdocs/code/wwwroot/protected/components/php-amqplib/amqp.inc(1390): call_user_func()
#1 unknown(0): AMQPChannel->basic_deliver()
#2 /htdocs/code/wwwroot/protected/components/php-amqplib/amqp.inc(167): call_user_func()
#3 /htdocs/code/wwwroot/protected/components/php-amqplib/amqp.inc(275): AMQPChannel->dispatch()
#4 /htdocs/code/wwwroot/protected/commands/RabbitMqCommand.php(29): AMQPChannel->wait()
#5 unknown(0): RabbitMqCommand->actionSendMail()
#6 /htdocs/code/framework/console/CConsoleCommand.php(135): ReflectionMethod->invokeArgs()
#7 /htdocs/code/framework/console/CConsoleCommandRunner.php(63): RabbitMqCommand->run()
#8 /htdocs/code/framework/console/CConsoleApplication.php(88): CConsoleCommandRunner->run()
#9 /htdocs/code/framework/base/CApplication.php(158): CConsoleApplication->processRequest()
#10 /htdocs/code/framework/yiic.php(33): CConsoleApplication->run()
#11 /htdocs/code/wwwroot/protected/yiic.php(7): require_once()
#12 /htdocs/code/wwwroot/protected/yiic(4): require_once()

知道我可能做错了吗?

2 个答案:

答案 0 :(得分:3)

发生此错误是因为processMessage是对象的实例方法,但basic_consume的参数应该是全局PHP方法。

请参阅此主题以获取解决方案:PHP Call a instance method with call_user_func within the same class

答案 1 :(得分:0)

请更改此行:

 $ch->basic_consume ( 'msgs', 'consumer', false, false, false, false, 'processMessage' );

对此:

$channel->basic_consume('msgs', 'consumer', false, false, false, false, [new RabbitMqCommand,'process_message']);