This commit is contained in:
2020-10-10 11:33:11 +08:00
parent bfc507782d
commit 4bb06ef42d
+24 -26
View File
@@ -38,7 +38,7 @@ class Kafka extends \Snowflake\Process\Process
$config->setTopics($kafka['topics']);
$this->channel = new Channel(1000);
$this->initCoroutine();
go([$this, 'listener']);
}
@@ -59,35 +59,33 @@ class Kafka extends \Snowflake\Process\Process
/**
*
* 监听通道数据传递
*/
public function initCoroutine()
protected function listener()
{
go(function () {
$group = new WaitGroup();
while ([$topic, $part, $message] = $this->channel->pop()) {
try {
$namespace = 'App\\Kafka\\' . ucfirst($topic) . 'Consumer';
if (!class_exists($namespace)) {
return;
}
$class = Snowflake::createObject($namespace);
if (!($class instanceof ConsumerInterface)) {
continue;
}
$group->add();
go(function () use ($group, $class, $topic, $part, $message) {
defer(function () use ($group) {
$group->done();
});
$class->onHandler(new Struct($topic, $part, $message));
});
} catch (\Throwable $exception) {
$this->application->error($exception->getMessage());
$group = new WaitGroup();
while ([$topic, $part, $message] = $this->channel->pop()) {
try {
$namespace = 'App\\Kafka\\' . ucfirst($topic) . 'Consumer';
if (!class_exists($namespace)) {
return;
}
$class = Snowflake::createObject($namespace);
if (!($class instanceof ConsumerInterface)) {
continue;
}
$group->add();
go(function () use ($group, $class, $topic, $part, $message) {
defer(function () use ($group) {
$group->done();
});
$class->onHandler(new Struct($topic, $part, $message));
});
} catch (\Throwable $exception) {
$this->application->error($exception->getMessage());
}
$group->wait();
});
}
$group->wait();
}