/** * fetch message to broker * * @access public * @return bool|Protocol\Fetch\Topic */ public function fetch() { $data = $this->_formatPayload(); if (empty($data)) { return false; } $streams = array(); foreach ($data as $host => $requestData) { $connArr = $this->client->getStream($host); $conn = $connArr['stream']; $encoder = new Protocol\Encoder($conn); $encoder->fetchRequest($requestData); $streams[$connArr['key']] = $conn; } $fetch = new Protocol\Fetch\Topic($streams, $data); // register fetch helper $freeStream = new Protocol\Fetch\Helper\FreeStream($this->client); $freeStream->setStreams($streams); Protocol\Fetch\Helper\Helper::registerHelper('freeStream', $freeStream); // register partition commit offset $commitOffset = new Protocol\Fetch\Helper\CommitOffset($this->client); $commitOffset->setGroup($this->group); Protocol\Fetch\Helper\Helper::registerHelper('commitOffset', $commitOffset); $updateConsumer = new Protocol\Fetch\Helper\Consumer($this); Protocol\Fetch\Helper\Helper::registerHelper('updateConsumer', $updateConsumer); return $fetch; }
private function loadTopicDetail(array $topics) { if ($this->client === null) { throw new Exception('client was not provided'); } $response = null; foreach ($this->hostList as $host) { try { $response = null; $stream = $this->client->getStream($host); $conn = $stream['stream']; $encoder = new Protocol\Encoder($conn); $encoder->metadataRequest($topics); $decoder = new Protocol\Decoder($conn); $response = $decoder->metadataResponse(); $this->client->freeStream($stream['key']); break; } catch (Exception $e) { // keep trying } } if ($response) { // Merge arrays using "+" operator to preserve key (which are broker IDs) // instead of array_merge (which reindex numeric keys) $this->brokers = $response['brokers'] + $this->brokers; $this->topics = array_merge($response['topics'], $this->topics); } else { throw new Exception('Could not connect to any kafka brokers'); } }
/** * send message to broker * * @access public * @return bool|array */ public function send() { $data = $this->_formatPayload(); if (empty($data)) { return false; } $responseData = array(); foreach ($data as $host => $requestData) { $stream = $this->client->getStream($host); $conn = $stream['stream']; $encoder = new Protocol\Encoder($conn); $encoder->produceRequest($requestData); if ((int) $this->requiredAck !== 0) { // get broker response $decoder = new Protocol\Decoder($conn); $response = $decoder->produceResponse(); foreach ($response as $topicName => $info) { if (!isset($responseData[$topicName])) { $responseData[$topicName] = $info; } else { $responseData[$topicName] = array_merge($info, $responseData[$topicName]); } } } $this->client->freeStream($stream['key']); } $this->payload = array(); return $responseData; }