This commit is contained in:
2020-10-26 17:08:52 +08:00
parent f73afb6209
commit ad0d48f775
+26 -6
View File
@@ -22,24 +22,44 @@ use function Amp\stop;
class Kafka extends \Snowflake\Process\Process class Kafka extends \Snowflake\Process\Process
{ {
protected Channel $channel; /** @var Channel */
protected $channel;
/**
* @throws ConfigException
*/
// public function initConfig()
// {
// $kafka = SConfig::get('kafka');
// $config = ConsumerConfig::getInstance();
// $config->setMetadataRefreshIntervalMs(
// $kafka['metadataRefreshIntervalMs'] ?? 1000
// );
// $config->setMetadataBrokerList($kafka['brokers']);
// $config->setGroupId($kafka['groupId']);
// $config->setBrokerVersion($kafka['version']);
// $config->setTopics($kafka['topics']);
// $this->channelListener($kafka);
//
// return [new Consumer(), $kafka];
// }
/** /**
* @param Process $process * @param Process $process
* @throws ConfigException * @throws ConfigException
* @throws \RdKafka\Exception
* @throws \Exception * @throws \Exception
*/ */
public function onHandler(Process $process) public function onHandler(Process $process)
{ {
$this->channelListener(); $this->channelListener();
[$config, $conf] = $this->kafkaConfig(); [$config, $conf] = $this->kafkaConfig();
$consumer = new \RdKafka\Consumer($config); $consumer = new KafkaConsumer($config);
$consumer->addBrokers($conf['brokers']); $consumer->subscribe($conf['topics']);
$topic = $consumer->newTopic("test");
while (true) { while (true) {
$message = $topic->consume(0, 1000); $message = $consumer->consume($conf['metadataRefreshIntervalMs'] ?? 1000);
if (empty($message)) { if (empty($message)) {
continue; continue;
} }