private function getProducer() { if (!$this->producer) { $this->producer = \Kafka\Produce::getInstance($this->zkHosts, $this->zkTimeout); } return $this->producer; }
/** * Get the producer object from kafka-php. * @return Produce */ protected function getKafkaProducer() { if (!$this->producer) { $this->producer = Produce::getInstance(null, null, $this->config->get('KafkaEventHost')); } return $this->producer; }
/** * KafkaPHPProducerAdapter constructor * * @param $zookeeperHost * @param int $zookeeperPort * @param int $zookeeperTimeout */ public function __construct($zookeeperHost, $zookeeperPort = 2181, $zookeeperTimeout = 3000) { $this->connection = Produce::getInstance("{$zookeeperHost}:{$zookeeperPort}", $zookeeperTimeout); }
<?php require 'autoloader.php'; while (1) { $part = mt_rand(0, 9); $produce = \Kafka\Produce::getInstance('192.168.1.115:2181'); $produce->setRequireAck(-1); $produce->setMessages('test_conn2', $part, array('test11111110099090')); $result = $produce->send(); // var_dump($result); usleep(10000); }
/** * Constructs the necessary support objects and returns a KafkaHandler * instance. * * @param string[] $kafkaServers * @param array $options * @param int $level The minimum logging level at which this handle will be triggered * @param bool $bubble Whether the messages that are handled can bubble the stack or not * @return KafkaHandler */ public static function factory($kafkaServers, array $options = [], $level = Logger::DEBUG, $bubble = true) { $metadata = new MetaDataFromKafka($kafkaServers); $produce = new Produce($metadata); if (isset($options['sendTimeout'])) { $timeOut = $options['sendTimeout']; $produce->getClient()->setStreamOption('SendTimeoutSec', 0); $produce->getClient()->setStreamOption('SendTimeoutUSec', intval($timeOut * 1000000)); } if (isset($options['recvTimeout'])) { $timeOut = $options['recvTimeout']; $produce->getClient()->setStreamOption('RecvTimeoutSec', 0); $produce->getClient()->setStreamOption('RecvTimeoutUSec', intval($timeOut * 1000000)); } if (isset($options['logExceptions']) && is_string($options['logExceptions'])) { $options['logExceptions'] = LoggerFactory::getInstance($options['logExceptions']); } if (isset($options['requireAck'])) { $produce->setRequireAck($options['requireAck']); } return new self($produce, $options, $level, $bubble); }
<?php require 'autoloader.php'; while (1) { $part = mt_rand(0, 1); $produce = \Kafka\Produce::getInstance('127.0.0.1:2181', 3000); // get available partitions $partitions = $produce->getAvailablePartitions('test'); var_dump($partitions); // send message $produce->setRequireAck(-1); $produce->setMessages('test', 0, array('test11111110099090')); $produce->setMessages('test', 1, array('test11111110099090')); $result = $produce->send(); var_dump($result); usleep(10000); }