mirror of
https://github.com/Telecominfraproject/wlan-cloud-lib-cppkafka.git
synced 2025-11-05 04:57:48 +00:00
Add Consumer::poll
This commit is contained in:
@@ -6,9 +6,14 @@
|
||||
using std::vector;
|
||||
using std::string;
|
||||
|
||||
using std::chrono::milliseconds;
|
||||
|
||||
namespace cppkafka {
|
||||
|
||||
Consumer::Consumer(const Configuration& config) {
|
||||
const milliseconds Consumer::DEFAULT_TIMEOUT{1000};
|
||||
|
||||
Consumer::Consumer(const Configuration& config)
|
||||
: timeout_ms_(DEFAULT_TIMEOUT) {
|
||||
char error_buffer[512];
|
||||
rd_kafka_t* ptr = rd_kafka_new(RD_KAFKA_CONSUMER, config.get_handle(),
|
||||
error_buffer, sizeof(error_buffer));
|
||||
@@ -18,6 +23,10 @@ Consumer::Consumer(const Configuration& config) {
|
||||
set_handle(ptr);
|
||||
}
|
||||
|
||||
void Consumer::set_timeout(const std::chrono::milliseconds timeout) {
|
||||
timeout_ms_ = timeout;
|
||||
}
|
||||
|
||||
void Consumer::subscribe(const vector<string>& topics) {
|
||||
TopicPartitionList list(topics.begin(), topics.end());
|
||||
rd_kafka_resp_err_t error = rd_kafka_subscribe(get_handle(), list.get_handle());
|
||||
@@ -36,6 +45,11 @@ void Consumer::assign(const TopicPartitionList& topic_partitions) {
|
||||
check_error(error);
|
||||
}
|
||||
|
||||
Message Consumer::poll() {
|
||||
rd_kafka_message_t* message = rd_kafka_consumer_poll(get_handle(), timeout_ms_.count());
|
||||
return Message(message);
|
||||
}
|
||||
|
||||
void Consumer::check_error(rd_kafka_resp_err_t error) {
|
||||
if (error != RD_KAFKA_RESP_ERR_NO_ERROR) {
|
||||
throw HandleException(error);
|
||||
|
||||
Reference in New Issue
Block a user