producer = app(KafkaProducer::class, [ 'conf' => $this->getConf($this->config->getProducerOptions()), ]); $this->dispatcher = App::make(Dispatcher::class); $this->pendingMessages = []; } public function __destruct() { if ($this->async) { $this->flush(); } } /** {@inheritDoc} */ public function produce(ProducerMessage $message): bool { $this->dispatcher->dispatch(new PublishingMessage($message)); $topic = $this->producer->newTopic($message->getTopicName()); $message = clone $message; $message = $this->serializer->serialize($message); $this->produceMessage($topic, $message); if ($this->flushCallback) { $this->pendingMessages[] = $message; } $this->producer->poll(0); if ($this->async) { return true; } return $this->flush(); } /** * @throws CouldNotPublishMessage * @throws Exception */ public function flush(): mixed { // Here we define the flush callback that is called shutting down a consumer. // This is called after every single message sent using Producer::send $flush = function () { $sleepMilliseconds = config('kafka.flush_retry_sleep_in_ms', 100); $retries = $this->config->flushRetries ?? config('kafka.flush_retries', 10); $timeout = $this->config->flushTimeoutInMs ?? config('kafka.flush_timeout_in_ms', 1000); try { return retry($retries, function () use ($timeout) { $result = $this->producer->flush($timeout); if ($result === RD_KAFKA_RESP_ERR_NO_ERROR) { $this->runFlushCallback(); return true; } $message = rd_kafka_err2str($result); throw CouldNotPublishMessage::withMessage($message, $result); }, $sleepMilliseconds); } catch (CouldNotPublishMessage $exception) { $this->dispatcher->dispatch(new \Junges\Kafka\Events\CouldNotPublishMessage( $exception->getKafkaErrorCode(), $exception->getMessage(), $exception, )); throw $exception; } }; return $flush(); } /** Set the Kafka Configuration. */ private function getConf(array $options): Conf { $conf = new Conf; foreach ($options as $key => $value) { $conf->set($key, (string) $value); } foreach ($this->config->getConfigCallbacks() as $method => $callback) { $conf->{$method}($callback); } return $conf; } private function produceMessage(ProducerTopic $topic, ProducerMessage $message): void { $topic->producev( partition: $message->getPartition(), msgflags: RD_KAFKA_MSG_F_BLOCK, payload: $message->getBody(), key: $message->getKey(), headers: $message->getHeaders() ); $this->dispatcher->dispatch(new MessagePublished($message)); } private function runFlushCallback(): void { if ($this->pendingMessages === []) { return; } if ($this->flushCallback !== null) { ($this->flushCallback)($this->pendingMessages); } $this->pendingMessages = []; } }