BewareMyPower commented on issue #12564:
URL: https://github.com/apache/pulsar/issues/12564#issuecomment-957046945


   > the client and producer of C + + client do not support defining variables 
as class members
   
   Why not? Please paste the compilation error. I guess it's because `Client` 
doesn't have a default constructor. Since when a `Client` is constructed, it 
doesn't start any network connection. So you can pass an empty string.
   
   Here is an example.
   
   ```c++
   #include <atomic>
   #include <chrono>
   #include <iostream>
   #include <stdexcept>
   #include <thread>
   
   #include <pulsar/Client.h>
   
   using namespace pulsar;
   
   class Demo {
      public:
       void init(const std::string& service_url, const std::string& topic) {
           client_ = Client(service_url);
           const auto result = client_.createProducer(topic, producer_);
           if (result != ResultOk) {
               throw std::runtime_error("Failed to create producer: " + 
std::string(strResult(result)));
           }
       }
   
       void send(const std::string& data) {
           const auto msg = MessageBuilder().setContent(data).build();
           pending_messages_++;
           producer_.sendAsync(msg, [this, data](Result result, const 
MessageId& id) {
               pending_messages_--;
               if (result == ResultOk) {
                   std::cout << "Send " << data << " to " << id << std::endl;
               } else {
                   std::cerr << "Failed to send " << data << ": " << result << 
std::endl;
               }
           });
       }
   
       void wait() {
           while (pending_messages_ > 0) {
               std::this_thread::sleep_for(std::chrono::milliseconds(1));
           }
       }
   
      private:
       Client client_{""};
       Producer producer_;
       std::atomic_int pending_messages_{0};
   };
   
   int main(int argc, char* argv[]) {
       Demo demo;
       demo.init("pulsar://localhost:6650", "my-topic");
       for (int i = 0; i < 10; i++) {
           demo.send("msg-" + std::to_string(i));
       }
       demo.wait();
       return 0;
   }
   ```
   
   And the output is:
   
   ```
   2021-11-02 10:39:12.018 INFO  [0x10aceee00] ClientConnection:181 | [<none> 
-> pulsar://localhost:6650] Create ClientConnection, timeout=10000
   2021-11-02 10:39:12.018 INFO  [0x10aceee00] ConnectionPool:96 | Created 
connection for pulsar://localhost:6650
   2021-11-02 10:39:12.020 WARN  [0x70000bbc2000] ClientConnection:428 | 
[<none> -> pulsar://localhost:6650] Failed to establish connection: Connection 
refused
   2021-11-02 10:39:12.020 INFO  [0x70000bbc2000] ClientConnection:367 | 
[127.0.0.1:64484 -> 127.0.0.1:6650] Connected to broker
   2021-11-02 10:39:12.023 INFO  [0x70000bbc2000] HandlerBase:64 | 
[persistent://public/default/my-topic, ] Getting connection from pool
   2021-11-02 10:39:12.032 INFO  [0x70000bbc2000] ProducerImpl:188 | 
[persistent://public/default/my-topic, ] Created producer on broker 
[127.0.0.1:64484 -> 127.0.0.1:6650] 
   Send msg-0 to (11,0,-1,0)
   Send msg-1 to (11,0,-1,1)
   Send msg-2 to (11,0,-1,2)
   Send msg-3 to (11,0,-1,3)
   Send msg-4 to (11,0,-1,4)
   Send msg-5 to (11,0,-1,5)
   Send msg-6 to (11,0,-1,6)
   Send msg-7 to (11,0,-1,7)
   Send msg-8 to (11,0,-1,8)
   Send msg-9 to (11,0,-1,9)
   2021-11-02 10:39:12.047 INFO  [0x10aceee00] ClientConnection:1542 | 
[127.0.0.1:64484 -> 127.0.0.1:6650] Connection closed
   2021-11-02 10:39:12.047 INFO  [0x70000bbc2000] ProducerImpl:588 | Producer - 
[persistent://public/default/my-topic, standalone-0-1] , [batchMessageContainer 
= { BatchMessageContainer [size = 0] [bytes = 0] [maxSize = 1000] [maxBytes = 
131072] [topicName = persistent://public/default/my-topic] 
[numberOfBatchesSent_ = 1] [averageBatchSize_ = 10] }]
   2021-11-02 10:39:12.047 INFO  [0x70000bbc2000] ClientConnection:255 | 
[127.0.0.1:64484 -> 127.0.0.1:6650] Destroyed connection
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to