如何在RabbitMQ回调中检索多条消息?

时间:2018-02-05 11:31:19

标签: php symfony rabbitmq message-queue

我在symfony框架中使用rabbitmq来发布和使用队列数据。我成功地将数据推入队列,但在读取数据时我遇到了问题。单个消息我可以轻松阅读,但多条消息回调有问题。

我创建了一个使用某种服务来消耗队列的设计。

QueueService.php

class QueueService
{
protected $logger;
public function addInQueue($class, $data, $event,$isEnabled)
{
    $publishData = $this->getPublishData($class, $data, $event,$isEnabled);
    $this->queue->publish($publishData);
}

public function fetchInQueue()
{
    $this->logger->info(' fetchInQueue >>>>>>>>');
    $res = $this->queue->consume();
    $this->logger->info('received message >>>>>>>>'.print_r($res,true));
}
}

AMQPHelper.php

class AMQPHelper {

protected $conConfig;
protected $queueConfig;
protected $logger;
private $response;

public function __construct($conConfig, $queueConfig)
{
    $this->conConfig = $conConfig;
    $this->queueConfig = $queueConfig;
    $loggerHelper = new LoggerHelper();
    $this->logger = $loggerHelper->init('amqp-helper');
}
public function consume()
{

    $connection = new AMQPConnection(
        $this->conConfig['host'],
        $this->conConfig['port'],
        $this->conConfig['username'],
        $this->conConfig['password'],
        $this->conConfig['vhost']
    );
    $channel = $connection->channel();


    $channel->exchange_declare(
        $this->queueConfig['exchange'],
        'direct',
        false,
        true,
        false
    );
    $channel->queue_declare(
        $this->queueConfig['exchange'],
        false,
        true,
        false,
        false
    );
    $channel->queue_bind(
        $this->queueConfig['exchange'],
        $this->queueConfig['exchange'],
        $this->queueConfig['routing']
    );

    //Consume
    $channel->basic_qos(null, 1, null);
    $channel->basic_consume(
        $this->queueConfig['exchange'],
        $this->queueConfig['routing'],
        false,
        false,
        false,
        false,
        array($this,'getQueueMsg')
    );

    while(count($channel->callbacks)) {
        $this->logger->info('  messages'.print_r($this->response,1));
        $this->logger->info('Waiting for incoming messages');
        $channel->wait();
    }

    $connection->close();
    $channel->close();

    return $this->response;
}

public function getQueueMsg(AMQPMessage $msg)
{
    $this->logger->info('Message body', print_r($msg, 1));
    $data = json_decode($msg->body, true);
    $this->response = $data;
}

我已经编写了一个控制台命令来使用QueueService函数。我希望控制台命令中的所有队列返回数据,因为在获取所有队列数据之后我想根据数据重新运行一些控制台命令。

ConsumeFailureCommand.php

class ConsumeFailureCommand extends BaseCommand{

protected function configure()
{
    $this
        ->setName('consume-failure')
        ->setDescription('notification message consumer from RabbitMQ')
        ->addOption(
            'wait',
            null,
            InputOption::VALUE_REQUIRED,
            'Wait for next message',
            1
        );
}

protected function execute(InputInterface $input, OutputInterface $output){
    $queueMng = $this->getContainer()->get('service_queue_management.queue_service');
    $this->setInput($input);
    $this->setOutput($output);
    $data = $queueMng->fetchInQueue();
    $this->logInfo(print_r($data,1));
}

}

所以队列将如何将所有数据返回给服务。它将每次返回单个消息,但我想要所有数据的多维数组。

0 个答案:

没有答案