【问题标题】:PHP AMQP consumer does not respond after a whilePHP AMQP 消费者在一段时间后没有响应
【发布时间】:2016-04-08 14:58:53
【问题描述】:

我在这里遇到了一个 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 */ }
    }
}

【问题讨论】:

    标签: php rabbitmq amqp silex php-amqplib


    【解决方案1】:

    我不能确定 PHP,但我遇到了 Python/kombu 的类似问题。纯粹的 puython amqplib 从来没有做过心跳,即使我给了它这样做的指令。当我转而使用 librabbitmq(它包含 rabbitmq-c)作为替代品时,心跳不再是一个问题,我的消费者也不再挂断我了。

    【讨论】:

    • 抱歉,回答迟了。我尝试将心跳与 phpamqp-lib 一起使用,但它没有按预期工作。现在我尝试连接超时,但这也有一些副作用,比如僵尸连接。我使用 supervisord 进行流程管理,如果工人死亡,它会启动新流程。 rabbitmq 上的连接仍然存在!?
    猜你喜欢
    • 2023-03-16
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多