Skip to content

Commit 57634fa

Browse files
authored
Fix cpp client examples and use g++ build project (#24)
Signed-off-by: xiaolong.ran <[email protected]>
1 parent b6125b4 commit 57634fa

File tree

4 files changed

+13
-56
lines changed

4 files changed

+13
-56
lines changed

cloud/cpp/CMakeLists.txt

Lines changed: 0 additions & 36 deletions
This file was deleted.

cloud/cpp/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ Produce message to and consume message from a Pulsar cluster using [Apache pulsa
44

55
# Prerequisites
66

7+
Pulsar releases are available in the Homebrew core repository. You can install the C++ client library with the following command. The package is installed with the library and headers.
78

9+
```shell script
10+
$ brew install libpulsar
11+
```
812

913
# Example
1014

@@ -15,20 +19,20 @@ The consumer will receive the message from the `topic-1` and `ack` the receipt o
1519
1. Run the consumer, and start to receiving the message from `topic-1`:
1620

1721
```bash
18-
$ cmake . && make && make install
22+
$ g++ --std=c++11 SampleConsumer.cc -o SampleConsumer -I/usr/local/Cellar/libpulsar/{PULSAR_VERSION}/include -lpulsar -L/usr/local/Cellar/libpulsar/{PULSAR_VERSION}/lib
1923
$ ./SampleConsumer
2024
```
2125

2226
Output:
2327

2428
```text
25-
Received message with payload 'content'
29+
Received: Message(prod=standalone-1-2, seq=0, publish_time=1596467242114, payload_size=7, msg_id=(71,0,-1,0), props={}) with payload 'content'
2630
```
2731

2832
2. Run the producer and publish messages to the `topic-1`:
2933

3034
```bash
31-
$ cmake . && make && make install
35+
$ g++ --std=c++11 SampleProducer.cc -o SampleProducer -I/usr/local/Cellar/libpulsar/{PULSAR_VERSION}/include -lpulsar -L/usr/local/Cellar/libpulsar/{PULSAR_VERSION}/lib
3236
$ ./SampleProducer
3337
```
3438

cloud/cpp/SampleConsumer.cc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,8 @@
1818
* under the License.
1919
*/
2020
#include <iostream>
21-
2221
#include <pulsar/Client.h>
2322

24-
#include <lib/LogUtils.h>
25-
26-
DECLARE_LOG_OBJECT()
27-
2823
using namespace pulsar;
2924

3025
int main() {
@@ -39,17 +34,16 @@ int main() {
3934
Client client("pulsar+ssl://mhlcluster.mhltest.us-east4.streamnative.test.g.sn2.dev:6651", config);
4035

4136
Consumer consumer;
42-
Result result = client.subscribe("persistent://public/default/topic-1", "consumer-1", consumer);
37+
Result result = client.subscribe("persistent://public/default/my-topic", "consumer-1", consumer);
4338
if (result != ResultOk) {
44-
LOG_ERROR("Failed to subscribe: " << result);
39+
std::cout << "Failed to subscribe: " << result << "\n";
4540
return -1;
4641
}
4742

4843
Message msg;
49-
5044
while (true) {
5145
consumer.receive(msg);
52-
LOG_INFO("Received: " << msg << " with payload '" << msg.getDataAsString() << "'");
46+
std::cout << "Received: " << msg << " with payload '" << msg.getDataAsString() << "'" << "\n";
5347

5448
consumer.acknowledge(msg);
5549
}

cloud/cpp/SampleProducer.cc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@
1717
* under the License.
1818
*/
1919
#include <iostream>
20-
2120
#include <pulsar/Client.h>
2221

23-
#include <lib/LogUtils.h>
24-
25-
DECLARE_LOG_OBJECT()
26-
2722
using namespace pulsar;
2823

2924
int main() {
@@ -38,16 +33,16 @@ int main() {
3833
Client client("pulsar+ssl://mhlcluster.mhltest.us-east4.streamnative.test.g.sn2.dev:6651", config);
3934

4035
Producer producer;
41-
Result result = client.createProducer("persistent://public/default/topic-1", producer);
36+
Result result = client.createProducer("persistent://public/default/my-topic", producer);
4237
if (result != ResultOk) {
43-
LOG_ERROR("Error creating producer: " << result);
38+
std::cout << "Error creating producer: " << result << "\n";
4439
return -1;
4540
}
4641

4742
// Send synchronously
4843
Message msg = MessageBuilder().setContent("content").build();
4944
Result res = producer.send(msg);
50-
LOG_INFO("Message sent: " << res);
45+
std::cout << "Message sent: " << res << "\n";
5146

5247
client.close();
5348
}

0 commit comments

Comments
 (0)