Newer
Older
ZXSSCJ / PhaseCompAcq / common / utils / QKafkaUtil.cpp
TAN YUE on 30 Sep 2021 851 bytes 20210930 初始提交
#include "QKafkaUtil.h"
#include <iostream>

QKafkaUtil::QKafkaUtil(QObject *parent) : QObject(parent)
{
    this->conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
}

void QKafkaUtil::setBrokers(QString brokers)
{
    this->brokers = brokers;
}
void QKafkaUtil::setTopic(QString topic)
{
    this->topic = topic;
}


int QKafkaUtil::initKafkaConf()
{
    int result;
    result = this->conf->set("bootstrap.servers", this->brokers.toStdString(), errStr);

    if (result != RdKafka::Conf::CONF_OK)
    {
        std::cerr << errStr << std::endl;

        return RdKafka::Conf::CONF_INVALID; // -1
    }

    this->producer = RdKafka::Producer::create(this->conf, errStr);
    if (producer == 0)
    {
        std::cerr << "Failed to create producer: " << errStr << std::endl;
        return -2;
    }

    result = 1;
    return result;
}