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


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()

    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');

          $messages = $kafkaService->fetchEvents();

          foreach ($messages as $message) {

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.

    点赞 打赏 评论

相关推荐 更多相似问题