/** * Add message * * Method that will build a message given the payload, will randomly * decide which is the partition where we are going to send the * message and will send it. * * @param String $topic * @param String $payload * @param Partitioner|NULL $partitioner */ public final function addMessage($topic, $payload, $key = null) { if (!array_key_exists($topic, $this->topicMetadata)) { if ($this->metadata !== null && $this->metadata->needsRefereshing()) { $this->refreshMetadata(); } } if (!array_key_exists($topic, $this->topicMetadata)) { throw new \Kafka\Exception\TopicUnavailable("Kafka topic `{$topic}` not available"); } //invoke partitioner $numPartitions = count($this->topicMetadata[$topic]); $i = $this->partitioner->partition($key, $numPartitions); if (!is_integer($i) || $i < 0 || $i > $numPartitions - 1) { throw new \Kafka\Exception("Partitioner must return 0 <= integer < {$numPartitions}, returned {$i}"); } $partitionInfo = $this->topicMetadata[$topic][$i]; $brokerId = $partitionInfo['broker']; $partition = $partitionInfo['partition']; // build the message $message = new Message($topic, $partition, $payload, $this->compression); // get the actual producer we will add the mesasge if (!isset($this->producerList[$brokerId])) { $producer = $this->getProducerByBrokerId($brokerId); $this->producerList[$brokerId] = $producer; } else { $producer = $this->producerList[$brokerId]; } $producer->add($message); }
/** * Get Kafka by Broker Id. * * @param String $brokerId * * @return Kafka */ private function getKafkaByBrokerId($brokerId) { // check if it exists, and if it doesn't, create it if (!isset($this->brokerList[$brokerId])) { $broker = $this->metadata->getBrokerInfo($brokerId); // instantiate the kafka broker representation $kafka = new Kafka($broker['host'], $broker['port']); // add the kafka bronker to the list $this->brokerList[$brokerId] = $kafka; } return $this->brokerList[$brokerId]; }