drrkgbm6851 2016-07-21 09:46
浏览 24
已采纳

Kafka - 如何收看新消息

I have PHP application that consumes Kafka messages. The problem is how to know that there are new messages in Kafka? First solution is to create consumer in PHP then run it in a loop to check new messages. Something like this

<?php

namespace MyAppBundle\Command;

use MyAppBundle\EventSourcing\EventSerializer\JSONEventSerializer;
use MyAppBundle\Service\EventProjectorService;
use MyAppBundle\Service\KafkaService;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\RuntimeException;

class EventCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('events:fetch');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        /** @var KafkaService $kafkaService */
        $kafkaService = $this->getContainer()->get('store_locator.kafka_service');
        /** @var EventProjectorService $eventProjector */
        $eventProjector = $this->getContainer()->get('store_locator.event_projector');

        while(1){
          $messages = $kafkaService->fetchEvents();

          foreach ($messages as $message) {
              $eventProjector->aggregate($message);
          }
        }
        $output->writeln("Finish");
    }
}

But I don't like it... Is there any other way?

If no better way, how to keep it running? For instance when something fail.

  • 写回答

1条回答 默认 最新

  • dongzhui4927 2016-07-22 13:29
    关注

    As far as I know there are no better ways than to loop infinitely and keep checking for new messages. A common approach is to have the task kill itself after a set amount of time or number of iterations and then use something like supervisord to detect the death and resurrect the consumer in order to prevent it eating up all your resources.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog