You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2020/08/04 05:01:50 UTC

[GitHub] [pulsar] BewareMyPower edited a comment on issue #7726: Consumer can't receive message from topic

BewareMyPower edited a comment on issue #7726:
URL: https://github.com/apache/pulsar/issues/7726#issuecomment-668380283


   I wrote a simple C++ consumer but could not reproduce the problem:
   
   ```c++
   // SampleConsumer.cc in examples directory
   #include <iostream>
   #include <pulsar/Client.h>
   
   using namespace std;
   using namespace pulsar;
   
   int main(int argc, char* argv[]) {
       std::string topic = "ParTopic";  // a topic with 4 partitions
       std::vector<std::string> partitions;
       for (int i = 1; i < argc; i++) {
           partitions.emplace_back(topic + "-partition-" + argv[i]);
       }
       size_t index = partitions.size();
   
       Client client("pulsar://localhost:6650");
   
       ConsumerConfiguration consumerConfig;
       consumerConfig.setConsumerType(ConsumerKeyShared);
   
       std::string subName = "SubscriptionName";
       Consumer consumer;
       if (partitions.empty()) {
           client.subscribe(topic, subName, consumerConfig, consumer);
       } else {
           client.subscribe(partitions, subName, consumerConfig, consumer);
       }
       Message msg;
   
       while (true) {
           Result result = consumer.receive(msg);
           cout << "[" << index << "] " << msg.getDataAsString() << ", key: " << msg.getPartitionKey()
                << ", id: " << msg.getMessageId() << endl;
           consumer.acknowledge(msg);
       }
       client.close();
   }
   ```
   
   First run a producer, and a consumer subscribing the partitioned topic (C1):
   
   ```
   $ ./SampleConsumer
   ```
   
   Then run a consumer subscribing two partitions (C2), eg, 0 and 2:
   
   ```
   $ ./SampleConsumer 2 3
   ```
   
   The example log of two cases are:
   
   #### partition 0 and 1
   
   C1:
   
   ```
   [0] 11bbbb, key: 20000, id: (42970,476,3,0)
   [0] 11aaaa, key: 10000, id: (43000,950,1,0)
   ```
   
   C2:
   
   ```
   [2] 11dddd, key: 40000, id: (43001,472,-1,0)
   [2] 11cccc, key: 30000, id: (43000,949,-1,0)
   ```
   
   #### partition 2 and 3
   
   C1:
   
   ```
   [0] 0cccc, key: 30000, id: (43000,955,1,0)
   [0] 0aaaa, key: 10000, id: (43000,956,1,0)
   [0] 0dddd, key: 40000, id: (43001,475,0,0)
   ```
   
   C2:
   
   ```
   [2] 0bbbb, key: 20000, id: (42970,479,-1,0)
   ```


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org