1

I am fairly new to using the rdkafkacpp.h Kafka library for C++.

I referred to a few available online resources, on how to set up a Kafka consumer on Windows, using the standard Kafka environment setup .bat files provided in: https://kafka.apache.org/quickstart (I just played around and was able to test that a message send from the producer terminal came up on the consumer terminal)

I have read through basic theory on Kafka and glanced through the documentation at: https://docs.confluent.io/4.0.0/clients/librdkafka

After this basic reading, I tried writing up sample code using the inbuilt library functions, as shown below. However, I receive a runtime exception at the line of code where I am trying to 'set the bootstrap-server' property of 'conf'. The exception is an access violation exception. "Exception thrown at 0x00007FFB878EC3F9 (msvcr120.dll) in mykafka.exe: 0xC0000005: Access violation reading location 0x0000008E5A900000."

I suspect the order of Kafka consumer 'implementation' using the library functions has missed some steps, or has to be re-ordered.

I am trying to keep the implementation simple, just a single producer on my local machine (localhost:9092), and just this single consumer (mykafka.exe). Also, a topic "quickstart-events" had been started on a terminal.

Any help is greatly appreciated!

P.S: Visual Studio 2019 is used for this code dev.

#include <iostream>
#include "..\include\librdkafka\rdkafkacpp.h"
#include <process.h> // to use exit()

using namespace std;
using namespace RdKafka;

int configAsKafkaConsumer()
{
    string host = "localhost:9092";
    string errstr;

    /* Set properties */
    cout << "Inside configAsKafkaConsumer()" << endl;

    // Create configuration objects
    RdKafka::Conf* conf = NULL;
    conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);

    if (conf != NULL)
    {
        cout << "conf != NULL" << endl;
    }

    // THIS IS WHERE I'M GETTING THE RUNTIME EXCEPTION!!
    if (conf->set("bootstrap.servers", "localhost:9092", errstr) != RdKafka::Conf::CONF_OK)
    {
        cerr << "Failed to set config of broker: " << errstr << endl;
        exit(1);
    }

    if (conf->set("client.id", host, errstr) != RdKafka::Conf::CONF_OK) 
    {
        cout << "client.id:" << endl;
        exit(1);
    }

    if (conf->set("group.id", "foo", errstr) != RdKafka::Conf::CONF_OK)
    {
        cout << "group.id:" << endl;
        exit(1);
    }

    // Create a consumer handle
    Consumer* ConsumerHandle = RdKafka::Consumer::create(conf, errstr);

    string errstr;
    string prodTopic = "quickstart-events"; 

    cout << "Creating topic handle" << endl;

    RdKafka::Conf* tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);

    // Create topic handle.
    RdKafka::Topic* topic = RdKafka::Topic::create(ConsumerHandle, prodTopic,
        tconf, errstr);

    if (!topic) 
    {
        std::cerr << "Failed to create topic: " << errstr << std::endl;
        exit(1);
    }

    cout << "Starting the consumer handle" << endl;
    ConsumerHandle->start(topic, 1, 0);

    cout << "Consuming the message" << endl;
    Message* msg = ConsumerHandle->consume(topic, 1, 10000);

    cout << "Message is: " << msg->payload() << endl;

}
Manasa
  • 43
  • 9
  • 1
    I don't know what `RdKafka::Conf::create`'s contract is, but not checking that the pointer you got from it is valid, is a smell. The C++ bindings are still very much full of C-ism. So I doubt there'd be an exception upon failure. It's likely the pointer creation failed. – StoryTeller - Unslander Monica Nov 19 '20 at 09:21
  • Thanks @ StoryTeller - Unslander Monica. But, I've added that check to see if a memory address is returned, and it did return; thus making it sure that *conf is pointing to a memory address. – Manasa Nov 19 '20 at 09:44
  • @Manasa How did you solve this issue? – Ahmad Jun 23 '21 at 12:34
  • Any solution for this yet? – Soham Mar 07 '22 at 20:16

1 Answers1

-2

Instead of

RdKafka::Conf* conf = NULL;
    conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);

Try this:

RdKafka::Conf *conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);

Hope this will resolve the issue.

Soham
  • 218
  • 2
  • 6
  • 15
  • 1
    How can this make a difference in his above code? This can not possibly make any difference at all. I would also change the code as you advised, however it will not solve the OP's issue. – user826955 Apr 19 '22 at 06:10