PHP AMQP消费者在一段时间后没有回应

时间:2016-04-08 14:58:54

标签: php rabbitmq amqp silex php-amqplib

我在这里遇到一个问题,一个php amqp消费者,一段时间后退出工作。下面你可以看到我的silex命令。我还尝试使用heartbeat和keepalive配置来处理断开的网络连接,但它没有改变。消费者没有从队列中读取消息的原因是什么?脚本没有退出,它似乎正在睡觉。

<?php

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Knp\Command\Command as BaseCommand;
use PhpAmqpLib\Message\AMQPMessage;

class RequestWorkerCommand extends BaseCommand
{
    protected function configure()
    {
        $this->setName('queue:worker');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $app = $this->getSilexApplication();
        $amqp = $app['amqp.connection']; /* @var $amqp \PhpAmqpLib\Connection\AMQPStreamConnection */
        $channel = $amqp->channel();

        $callback = function($message) use ($input, $output) {
            return call_user_func_array([$this, 'processMessage'], [$message, $input, $output]);
        };

        $channel->queue_declare('myqueue', false, true, false, false);
        $channel->basic_qos(null, 1, null);
        $channel->basic_consume('myqueue', '', false, false, false, false, $callback);

        while(count($channel->callbacks)) {
            $output->writeln('Waiting for incoming price requests');
            $channel->wait();
        }
    }

    protected function processMessage(AMQPMessage $message, InputInterface $input, OutputInterface $output)
    {
        $app = $this->getSilexApplication();

        try {
            $data = json_decode($message->body, true);
            $request = Request::createFromArray($data); /* create object from data */
            $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
            $app['distributor']->distribute($request); /* process message */
        } catch (\Exception $e) { /* handle error */ }
    }
}

1 个答案:

答案 0 :(得分:0)

我无法确定PHP,但我遇到了类似Python / kombu的问题。纯粹的puython amqplib从来没有做过心跳,即使我给它指示这样做。当我切换到使用librabbitmq(包裹rabbitmq-c)作为替代品时,心跳停止成为一个问题,我的消费者也不再挂在我身上。

相关问题