forked from confluentinc/librdkafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdkafkacpp.h
66 lines (48 loc) · 1.57 KB
/
rdkafkacpp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// rdkafka.h
#ifndef RD_KAFKACPP_H
#define RD_KAFKACPP_H
extern "C"{
#include "../rdkafka.h"
}
namespace rdkafka{
class Kafka{
public:
/// @warning Make sure SIGPIPE is either ignored or handled by the calling application before.
/// Make sure test if handle has created successfuly using hasHandle().
inline Kafka():rk(NULL){};
inline ~Kafka();
/** Socket hangups are gracefully handled in librdkafka on socket error
* without the use of signals, so SIGPIPE should be ignored by the calling
* program. */
static void ignore_sigpipe(){signal(SIGPIPE, SIG_IGN);}
bool setHandle(rd_kafka_type_t type,const char * broker, const rd_kafka_conf_t *conf);
bool hasHandle(){return rk!=NULL;}
/*
void setTopic(const char * newtopic);
const char * getTopic()const{return topic;}
*/
int produce(char *topic, uint32_t partition,int msgflags, char *payload, size_t len);
private:
rd_kafka_t *rk;
};
Kafka::~Kafka(){
if(rk){
/* Wait for messaging to finish. */
while (rd_kafka_outq_len(rk) > 0)
usleep(50000);
/* Since there is no ack for produce messages in 0.7
* we wait some more for any packets to be sent.
* This is fixed in protocol version 0.8 */
usleep(500000);
/* Destroy the handle */
rd_kafka_destroy(rk);
}
}
bool Kafka::setHandle(rd_kafka_type_t type,const char * broker,const rd_kafka_conf_t *conf){
return (rk = rd_kafka_new(type, broker, conf))!=NULL;
}
int Kafka::produce(char *topic, uint32_t partition,int msgflags, char *payload, size_t len){
return rd_kafka_produce(rk, topic, partition, msgflags, payload, len);
}
}
#endif // RD_KAFKACPP_H