Add Consumer::poll

This commit is contained in:
Matias Fontanini
2016-05-21 19:51:50 -07:00
parent 4f50122393
commit 1c51bb72f4
4 changed files with 31 additions and 3 deletions

View File

@@ -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);