dkx/google-pubsub-subscriber

此包已被废弃,不再维护。未建议替代包。

Symfony 控制台命令用于 Google PubSub

2.2.0 2019-12-18 17:26 UTC

This package is auto-updated.

Last update: 2024-01-19 00:53:32 UTC


README

Symfony 控制台命令用于 Google PubSub

安装

$ composer require dkx/google-pubsub-subscriber

用法

<?php

use DKX\GooglePubSubSubscriber\SubscriberCommand;
use DKX\GooglePubSubSubscriber\SubscriptionInterface;
use DKX\GooglePubSubSubscriber\SubscriptionsManager;
use Google\Cloud\PubSub\PubSubClient;
use Symfony\Component\Console\Application;

final class TestSubscription implements SubscriptionInterface
{
    public function getConsoleName() : string
    {
        return 'my-test';    
    }

    public function processMessage(array $data) : bool
    {
        var_dump($data);
        return true;
    }
}

$client = new PubSubClient();

$manager = new SubscriptionsManager();
$manager->addSubscription(new TestSubscription());

$command = new SubscriberCommand($client, $manager);

$application = new Application();
$application->add($command);
$application->run();

并运行

$ php index.php pubsub:subscribe my-test my-app.prod.test --max-seconds 500 --max-messages 100
  • my-test:来自您订阅的 getConsoleName() 方法的字符串
  • my-app.prod.test:在 Google Cloud Pub/Sub 中现有订阅的名称

独立使用

<?php

use DKX\GooglePubSubSubscriber\Subscriber;
use Google\Cloud\PubSub\PubSubClient;

$client = new PubSubClient();

$subscriber = new Subscriber($client, 'my-app.prod.test');
$subscriber->setMaxSeconds(500);
$subscriber->setMaxMessages(100);

$subscriber->subscribe(function (array $data): bool {
    var_dump($data);
    return true;
});