linux下kafka客户端,Qt在linux下实现kafka客户端开发(三)

#include bool g_bLoadedCppLib = false;

//typedef Conf *(*rd_conf_create_decl) (Conf::ConfType type);

ConsumerKafka::ConsumerKafka()

{

}

bool ConsumerKafka::LoadCppLiberary(const QString& dllName)

{

QLibrary lib(dllName);

if( !lib.load() )

{

//qDebug() << "加载dll失败" + dllName;

qDebug() << QString("加载dll失败") + dllName + "\n error:" + lib.errorString();

return false;

}

else

{

qDebug() << QString("加载dll成功!") + dllName;

g_bLoadedCppLib = true;

//rd_conf_create_decl thtf_conf_create = (rd_conf_create_decl)lib.resolve("Conf::create");

//global_conf = thtf_conf_create(RdKafka::Conf::CONF_GLOBAL);

//global_conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);

}

return true;

}

int ConsumerKafka::InitKafka(int _partition, string broker, string _topic)

{

if( !g_bLoadedCppLib )

return -1;

//global_conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);

//topic_conf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);

return 0;

}

int ConsumerKafka::init_kafka(int _partition, string broker, string _topic)

{

global_conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);

topic_conf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);

brokers = broker;

partition = _partition;

topic_name = _topic;

start_offset = RdKafka::Topic::OFFSET_BEGINNING;

global_conf->set("metadata.broker.list", brokers, errstr);

MyEventCb ex_event_cb;

global_conf->set("event_cb", &ex_event_cb, errstr);

/*

* Create consumer using accumulated global configuration.

*/

consumer = RdKafka::Consumer::create(global_conf, errstr);

if (!consumer) {

std::cerr << "Failed to create consumer: " << errstr << std::endl;

exit(1);

}

/* Create topic */

topic = RdKafka::Topic::create(consumer, topic_name, topic_conf, errstr);

if (!topic) {

std::cerr << "Failed to create topic: " << errstr << std::endl;

exit(1);

}

}

void ConsumerKafka::destroy()

{

consumer->stop(topic, partition);

consumer->poll(1000);

delete topic;

delete consumer;

}

int ConsumerKafka::pull_data_from_kafka()

{

RdKafka::ErrorCode resp = consumer->start(topic, partition, start_offset);

if (resp != RdKafka::ERR_NO_ERROR) {

std::cerr << "Failed to start consumer: " <<

RdKafka::err2str(resp) << std::endl;

exit(1);

}

/*

* Consume messages

*/

MyConsumeCb ex_consume_cb;

int use_ccb = 0;

m_bRun = true;

QElapsedTimer t;

t.start();

//qDebug() << t.elapsed() << endl;

while(m_bRun)

{

QCoreApplication::processEvents();

if (use_ccb) {

// consumer->consume_callback(topic, partition, 1000,

// &ex_consume_cb, &use_ccb);

} else {

RdKafka::Message *msg = consumer->consume(topic, partition, 1000);

msg_consume(msg, NULL);

delete msg;

}

consumer->poll(0);

}

}

void ConsumerKafka::pull_data_stop()

{

m_bRun = false;

}

void msg_consume(RdKafka::Message* message, void* opaque) {

switch (message->err()) {

case RdKafka::ERR__TIMED_OUT:

break;

case RdKafka::ERR_NO_ERROR:

/* Real message */

std::cout << "Read msg at offset " << message->offset() << std::endl;

if (message->key()) {

std::cout << "Key: " << *message->key() << std::endl;

}

cout << static_cast(message->payload()) << endl;

break;

case RdKafka::ERR__PARTITION_EOF:

cout << "reach last message" << endl;

/* Last message */

if (exit_eof) {

run = false;

}

break;

case RdKafka::ERR__UNKNOWN_TOPIC:

case RdKafka::ERR__UNKNOWN_PARTITION:

std::cerr << "Consume failed: " << message->errstr() << std::endl;

run = false;

break;

default:

/* Errors */

std::cerr << "Consume failed: " << message->errstr() << std::endl;

run = false;

}

}

//调用运行

int main(int argc, char **argv) {

// Process kill signal, quit from the loop

signal(SIGINT, sigterm);

signal(SIGTERM, sigterm);

ConsummerKafka test;

test.init_kafka(0, "localhost", "Hello-Kafka");

test.pull_data_from_kafka();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值