mfontanini / cppkafka

Modern C++ Apache Kafka client library (wrapper for librdkafka)
BSD 2-Clause "Simplified" License
587 stars 207 forks source link

create BufferedProducer<std::string> producer globally #299

Open barzan-hayati opened 1 year ago

barzan-hayati commented 1 year ago

I want to define and create connection once. So I define these variables out of main

MessageBuilder *builder_appearance;
Configuration *config_appearance;
BufferedProducer<std::string> *buffered_producer_appearance;

In a function I set parameters to these objects:


int create_json_connection()
{
    int partition_value = -1;

    po::options_description options("Options");
    options.add_options()
            ("help,h",      "produce this help message")
            ("brokers,b",   po::value<string>(&brokers)->required(),
             "the kafka broker list")
            ("topic,t",     po::value<string>(&topic_name)->required(),
             "the topic in which to write to")
            ("partition,p", po::value<int>(&partition_value),
             "the partition to write into (unassigned if not provided)");

    po::variables_map vm;

    int argc = 5;
    const char* argv[argc];
    argv[0] = argv0.c_str();
    argv[1] = "-b";
    argv[2] = brokers.c_str();
    argv[3] = "-t";
    argv[4] = topic_name.c_str();

    try
    {
        po::store(po::command_line_parser(argc, argv).options(options).run(), vm);
        po::notify(vm);
    }
    catch (exception& ex)
    {
        cout << "\033[1;31m Error parsing options: \033[0m" << ex.what() << endl << endl;
        cout << options << endl;
        return 0;
    }
    builder_appearance->topic(topic_name);
    if (partition_value != -1)
    {
        builder_appearance->partition(partition_value);
    }

    config_appearance->set_default_topic_configuration({
                                                           { "metadata.broker.list", brokers },
                                                           { "statistics.interval.ms", 60000 }
                                                       });
}

Now I want to set Configuration to buffered_producer_appearance:

buffered_producer_appearance(*config_appearance);

But it gave me this error:


error: ‘buffered_producer_appearance’ cannot be used as a function
     buffered_producer_appearance(*config_appearance);
                                                    ^

So I should define producer1 as follows but it's not ideal for me: BufferedProducer<std::string> producer1(*config_appearance); How could I resolve this. Thanks in advance.