Pulsar C++ client

You can use Pulsar C++ client to create Pulsar producers and consumers in C++.

All the methods in producer, consumer, and reader of a C++ client are thread-safe.

Supported platforms

Pulsar C++ client is supported on Linux ,MacOS and Windows platforms.

Doxygen-generated API docs for the C++ client are available here.

Linux

note

You can choose one of the following installation methods based on your needs: Compilation, Install RPM or Install Debian.

Compilation

System requirements

You need to install the following components before using the C++ client:

  1. Clone the Pulsar repository.
  1. $ git clone https://github.com/apache/pulsar
  1. Install all necessary dependencies.
  1. $ apt-get install cmake libssl-dev libcurl4-openssl-dev liblog4cxx-dev \
  2. libprotobuf-dev protobuf-compiler libboost-all-dev google-mock libgtest-dev libjsoncpp-dev
  1. Compile and install Google Test.
  1. # libgtest-dev version is 1.18.0 or above
  2. $ cd /usr/src/googletest
  3. $ sudo cmake .
  4. $ sudo make
  5. $ sudo cp ./googlemock/libgmock.a ./googlemock/gtest/libgtest.a /usr/lib/
  6. # less than 1.18.0
  7. $ cd /usr/src/gtest
  8. $ sudo cmake .
  9. $ sudo make
  10. $ sudo cp libgtest.a /usr/lib
  11. $ cd /usr/src/gmock
  12. $ sudo cmake .
  13. $ sudo make
  14. $ sudo cp libgmock.a /usr/lib
  1. Compile the Pulsar client library for C++ inside the Pulsar repository.
  1. $ cd pulsar-client-cpp
  2. $ cmake .
  3. $ make

After you install the components successfully, the files libpulsar.so and libpulsar.a are in the lib folder of the repository. The tools perfProducer and perfConsumer are in the perf directory.

Install Dependencies

Since 2.1.0 release, Pulsar ships pre-built RPM and Debian packages. You can download and install those packages directly.

After you download and install RPM or DEB, the libpulsar.so, libpulsarnossl.so, libpulsar.a, and libpulsarwithdeps.a libraries are in your /usr/lib directory.

By default, they are built in code path ${PULSAR_HOME}/pulsar-client-cpp. You can build with the command below.

cmake . -DBUILD_TESTS=OFF -DLINK_STATIC=ON && make pulsarShared pulsarSharedNossl pulsarStatic pulsarStaticWithDeps -j 3.

These libraries rely on some other libraries. If you want to get detailed version of dependencies, see RPM or DEB files.

  1. libpulsar.so is a shared library, containing statically linked boost and openssl. It also dynamically links all other necessary libraries. You can use this Pulsar library with the command below.
  1. g++ --std=c++11 PulsarTest.cpp -o test /usr/lib/libpulsar.so -I/usr/local/ssl/include
  1. libpulsarnossl.so is a shared library, similar to libpulsar.so except that the libraries openssl and crypto are dynamically linked. You can use this Pulsar library with the command below.
  1. g++ --std=c++11 PulsarTest.cpp -o test /usr/lib/libpulsarnossl.so -lssl -lcrypto -I/usr/local/ssl/include -L/usr/local/ssl/lib
  1. libpulsar.a is a static library. You need to load dependencies before using this library. You can use this Pulsar library with the command below.
  1. g++ --std=c++11 PulsarTest.cpp -o test /usr/lib/libpulsar.a -lssl -lcrypto -ldl -lpthread -I/usr/local/ssl/include -L/usr/local/ssl/lib -lboost_system -lboost_regex -lcurl -lprotobuf -lzstd -lz
  1. libpulsarwithdeps.a is a static library, based on libpulsar.a. It is archived in the dependencies of libboost_regex, libboost_system, libcurl, libprotobuf, libzstd and libz. You can use this Pulsar library with the command below.
  1. g++ --std=c++11 PulsarTest.cpp -o test /usr/lib/libpulsarwithdeps.a -lssl -lcrypto -ldl -lpthread -I/usr/local/ssl/include -L/usr/local/ssl/lib

The libpulsarwithdeps.a does not include library openssl related libraries libssl and libcrypto, because these two libraries are related to security. It is more reasonable and easier to use the versions provided by the local system to handle security issues and upgrade libraries.

Install RPM

  1. Download a RPM package from the links in the table.
LinkCrypto files
clientasc, sha512
client-debuginfoasc, sha512
client-develasc, sha512
  1. Install the package using the following command.
  1. $ rpm -ivh apache-pulsar-client*.rpm

After you install RPM successfully, Pulsar libraries are in the /usr/lib directory, for example:

  1. lrwxrwxrwx 1 root root 18 Dec 30 22:21 libpulsar.so -> libpulsar.so.2.9.1
  2. lrwxrwxrwx 1 root root 23 Dec 30 22:21 libpulsarnossl.so -> libpulsarnossl.so.2.9.1
note

If you get the error that libpulsar.so: cannot open shared object file: No such file or directory when starting Pulsar client, you may need to run ldconfig first.

  1. Install the GCC and g++ using the following command, otherwise errors would occur in installing Node.js.
  1. $ sudo yum -y install gcc automake autoconf libtool make
  2. $ sudo yum -y install gcc-c++

Install Debian

  1. Download a Debian package from the links in the table.
LinkCrypto files
clientasc, sha512
client-develasc, sha512
  1. Install the package using the following command.
  1. $ apt install ./apache-pulsar-client*.deb

After you install DEB successfully, Pulsar libraries are in the /usr/lib directory.

Build

If you want to build RPM and Debian packages from the latest master, follow the instructions below. You should run all the instructions at the root directory of your cloned Pulsar repository.

There are recipes that build RPM and Debian packages containing a statically linked libpulsar.so / libpulsarnossl.so / libpulsar.a / libpulsarwithdeps.a with all required dependencies.

To build the C++ library packages, you need to build the Java packages first.

  1. mvn install -DskipTests

RPM

To build the RPM inside a Docker container, use the command below. The RPMs are in the pulsar-client-cpp/pkg/rpm/RPMS/x86_64/ path.

  1. pulsar-client-cpp/pkg/rpm/docker-build-rpm.sh
Package nameContent
pulsar-clientShared library libpulsar.so and libpulsarnossl.so
pulsar-client-develStatic library libpulsar.a, libpulsarwithdeps.aand C++ and C headers
pulsar-client-debuginfoDebug symbols for libpulsar.so

Debian

To build Debian packages, enter the following command.

  1. pulsar-client-cpp/pkg/deb/docker-build-deb.sh

Debian packages are created in the pulsar-client-cpp/pkg/deb/BUILD/DEB/ path.

Package nameContent
pulsar-clientShared library libpulsar.so and libpulsarnossl.so
pulsar-client-devStatic library libpulsar.a, libpulsarwithdeps.a and C++ and C headers

MacOS

Compilation

  1. Clone the Pulsar repository.
  1. $ git clone https://github.com/apache/pulsar
  1. Install all necessary dependencies.
  1. # OpenSSL installation
  2. $ brew install openssl
  3. $ export OPENSSL_INCLUDE_DIR=/usr/local/opt/openssl/include/
  4. $ export OPENSSL_ROOT_DIR=/usr/local/opt/openssl/
  5. # Protocol Buffers installation
  6. $ brew install protobuf boost boost-python log4cxx
  7. # If you are using python3, you need to install boost-python3
  8. # Google Test installation
  9. $ git clone https://github.com/google/googletest.git
  10. $ cd googletest
  11. $ cmake .
  12. $ make install
  1. Compile the Pulsar client library in the repository that you cloned.
  1. $ cd pulsar-client-cpp
  2. $ cmake .
  3. $ make

Install libpulsar

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.

  1. brew install libpulsar

Windows (64-bit)

Compilation

  1. Clone the Pulsar repository.
  1. $ git clone https://github.com/apache/pulsar
  1. Install all necessary dependencies.
  1. cd ${PULSAR_HOME}/pulsar-client-cpp
  2. vcpkg install --feature-flags=manifests --triplet x64-windows
  1. Build C++ libraries.
  1. cmake -B ./build -A x64 -DBUILD_PYTHON_WRAPPER=OFF -DBUILD_TESTS=OFF -DVCPKG_TRIPLET=x64-windows -DCMAKE_BUILD_TYPE=Release -S .
  2. cmake --build ./build --config Release

NOTE

  1. For Windows 32-bit, you need to use -A Win32 and -DVCPKG_TRIPLET=x86-windows.
  2. For MSVC Debug mode, you need to replace Release with Debug for both CMAKE_BUILD_TYPE variable and --config option.
  1. Client libraries are available in the following places.
  1. ${PULSAR_HOME}/pulsar-client-cpp/build/lib/Release/pulsar.lib
  2. ${PULSAR_HOME}/pulsar-client-cpp/build/lib/Release/pulsar.dll

Connection URLs

To connect Pulsar using client libraries, you need to specify a Pulsar protocol URL.

Pulsar protocol URLs are assigned to specific clusters, you can use the Pulsar URI scheme. The default port is 6650. The following is an example for localhost.

  1. pulsar://localhost:6650

In a Pulsar cluster in production, the URL looks as follows.

  1. pulsar://pulsar.us-west.example.com:6650

If you use TLS authentication, you need to add ssl, and the default port is 6651. The following is an example.

  1. pulsar+ssl://pulsar.us-west.example.com:6651

Create a producer

To use Pulsar as a producer, you need to create a producer on the C++ client. There are two main ways of using a producer:

Simple blocking example

This example sends 100 messages using the blocking style. While simple, it does not produce high throughput as it waits for each ack to come back before sending the next message.

  1. #include <pulsar/Client.h>
  2. #include <thread>
  3. using namespace pulsar;
  4. int main() {
  5. Client client("pulsar://localhost:6650");
  6. Result result = client.createProducer("persistent://public/default/my-topic", producer);
  7. if (result != ResultOk) {
  8. std::cout << "Error creating producer: " << result << std::endl;
  9. return -1;
  10. }
  11. // Send 100 messages synchronously
  12. int ctr = 0;
  13. while (ctr < 100) {
  14. std::string content = "msg" + std::to_string(ctr);
  15. Message msg = MessageBuilder().setContent(content).setProperty("x", "1").build();
  16. Result result = producer.send(msg);
  17. if (result != ResultOk) {
  18. std::cout << "The message " << content << " could not be sent, received code: " << result << std::endl;
  19. } else {
  20. std::cout << "The message " << content << " sent successfully" << std::endl;
  21. }
  22. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  23. ctr++;
  24. }
  25. std::cout << "Finished producing synchronously!" << std::endl;
  26. client.close();
  27. return 0;
  28. }

Non-blocking example

This example sends 100 messages using the non-blocking style calling sendAsync instead of send. This allows the producer to have multiple messages inflight at a time which increases throughput.

The producer configuration blockIfQueueFull is useful here to avoid ResultProducerQueueIsFull errors when the internal queue for outgoing send requests becomes full. Once the internal queue is full, sendAsync becomes blocking which can make your code simpler.

Without this configuration, the result code ResultProducerQueueIsFull is passed to the callback. You must decide how to deal with that (retry, discard etc).

  1. #include <pulsar/Client.h>
  2. #include <thread>
  3. using namespace pulsar;
  4. std::atomic<uint32_t> acksReceived;
  5. void callback(Result code, const MessageId& msgId, std::string msgContent) {
  6. // message processing logic here
  7. std::cout << "Received ack for msg: " << msgContent << " with code: "
  8. << code << " -- MsgID: " << msgId << std::endl;
  9. acksReceived++;
  10. }
  11. int main() {
  12. Client client("pulsar://localhost:6650");
  13. ProducerConfiguration producerConf;
  14. producerConf.setBlockIfQueueFull(true);
  15. Producer producer;
  16. Result result = client.createProducer("persistent://public/default/my-topic",
  17. producerConf, producer);
  18. if (result != ResultOk) {
  19. std::cout << "Error creating producer: " << result << std::endl;
  20. return -1;
  21. }
  22. // Send 100 messages asynchronously
  23. int ctr = 0;
  24. while (ctr < 100) {
  25. std::string content = "msg" + std::to_string(ctr);
  26. Message msg = MessageBuilder().setContent(content).setProperty("x", "1").build();
  27. producer.sendAsync(msg, std::bind(callback,
  28. std::placeholders::_1, std::placeholders::_2, content));
  29. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  30. ctr++;
  31. }
  32. // wait for 100 messages to be acked
  33. while (acksReceived < 100) {
  34. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  35. }
  36. std::cout << "Finished producing asynchronously!" << std::endl;
  37. client.close();
  38. return 0;
  39. }

Partitioned topics and lazy producers

When scaling out a Pulsar topic, you may configure a topic to have hundreds of partitions. Likewise, you may have also scaled out your producers so there are hundreds or even thousands of producers. This can put some strain on the Pulsar brokers as when you create a producer on a partitioned topic, internally it creates one internal producer per partition which involves communications to the brokers for each one. So for a topic with 1000 partitions and 1000 producers, it ends up creating 1,000,000 internal producers across the producer applications, each of which has to communicate with a broker to find out which broker it should connect to and then perform the connection handshake.

You can reduce the load caused by this combination of a large number of partitions and many producers by doing the following:

  • use SinglePartition partition routing mode (this ensures that all messages are only sent to a single, randomly selected partition)
  • use non-keyed messages (when messages are keyed, routing is based on the hash of the key and so messages will end up being sent to multiple partitions)
  • use lazy producers (this ensures that an internal producer is only created on demand when a message needs to be routed to a partition)

With our example above, that reduces the number of internal producers spread out over the 1000 producer apps from 1,000,000 to just 1000.

Note that there can be extra latency for the first message sent. If you set a low send timeout, this timeout could be reached if the initial connection handshake is slow to complete.

  1. ProducerConfiguration producerConf;
  2. producerConf.setPartitionsRoutingMode(ProducerConfiguration::UseSinglePartition);
  3. producerConf.setLazyStartPartitionedProducers(true);

Enable chunking

Message chunking enables Pulsar to process large payload messages by splitting the message into chunks at the producer side and aggregating chunked messages at the consumer side.

The message chunking feature is OFF by default. The following is an example about how to enable message chunking when creating a producer.

  1. ProducerConfiguration conf;
  2. conf.setBatchingEnabled(false);
  3. conf.setChunkingEnabled(true);
  4. Producer producer;
  5. client.createProducer("my-topic", conf, producer);

Note: To enable chunking, you need to disable batching (setBatchingEnabled\=false) concurrently.

Create a consumer

To use Pulsar as a consumer, you need to create a consumer on the C++ client. There are two main ways of using the consumer:

Blocking example

The benefit of this approach is that it is the simplest code. Simply keeps calling receive(msg) which blocks until a message is received.

This example starts a subscription at the earliest offset and consumes 100 messages.

  1. #include <pulsar/Client.h>
  2. using namespace pulsar;
  3. int main() {
  4. Client client("pulsar://localhost:6650");
  5. Consumer consumer;
  6. ConsumerConfiguration config;
  7. config.setSubscriptionInitialPosition(InitialPositionEarliest);
  8. Result result = client.subscribe("persistent://public/default/my-topic", "consumer-1", config, consumer);
  9. if (result != ResultOk) {
  10. std::cout << "Failed to subscribe: " << result << std::endl;
  11. return -1;
  12. }
  13. Message msg;
  14. int ctr = 0;
  15. // consume 100 messages
  16. while (ctr < 100) {
  17. consumer.receive(msg);
  18. std::cout << "Received: " << msg
  19. << " with payload '" << msg.getDataAsString() << "'" << std::endl;
  20. consumer.acknowledge(msg);
  21. ctr++;
  22. }
  23. std::cout << "Finished consuming synchronously!" << std::endl;
  24. client.close();
  25. return 0;
  26. }

Consumer with a message listener

You can avoid running a loop with blocking calls with an event based style by using a message listener which is invoked for each message that is received.

This example starts a subscription at the earliest offset and consumes 100 messages.

  1. #include <pulsar/Client.h>
  2. #include <atomic>
  3. #include <thread>
  4. using namespace pulsar;
  5. std::atomic<uint32_t> messagesReceived;
  6. void handleAckComplete(Result res) {
  7. std::cout << "Ack res: " << res << std::endl;
  8. }
  9. void listener(Consumer consumer, const Message& msg) {
  10. std::cout << "Got message " << msg << " with content '" << msg.getDataAsString() << "'" << std::endl;
  11. messagesReceived++;
  12. consumer.acknowledgeAsync(msg.getMessageId(), handleAckComplete);
  13. }
  14. int main() {
  15. Client client("pulsar://localhost:6650");
  16. Consumer consumer;
  17. ConsumerConfiguration config;
  18. config.setMessageListener(listener);
  19. config.setSubscriptionInitialPosition(InitialPositionEarliest);
  20. Result result = client.subscribe("persistent://public/default/my-topic", "consumer-1", config, consumer);
  21. if (result != ResultOk) {
  22. std::cout << "Failed to subscribe: " << result << std::endl;
  23. return -1;
  24. }
  25. // wait for 100 messages to be consumed
  26. while (messagesReceived < 100) {
  27. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  28. }
  29. std::cout << "Finished consuming asynchronously!" << std::endl;
  30. client.close();
  31. return 0;
  32. }

Configure chunking

You can limit the maximum number of chunked messages a consumer maintains concurrently by configuring the setMaxPendingChunkedMessage and setAutoAckOldestChunkedMessageOnQueueFull parameters. When the threshold is reached, the consumer drops pending messages by silently acknowledging them or asking the broker to redeliver them later.

The following is an example of how to configure message chunking.

  1. ConsumerConfiguration conf;
  2. conf.setAutoAckOldestChunkedMessageOnQueueFull(true);
  3. conf.setMaxPendingChunkedMessage(100);
  4. Consumer consumer;
  5. client.subscribe("my-topic", "my-sub", conf, consumer);

Enable authentication in connection URLs

If you use TLS authentication when connecting to Pulsar, you need to add ssl in the connection URLs, and the default port is 6651. The following is an example.

  1. ClientConfiguration config = ClientConfiguration();
  2. config.setUseTls(true);
  3. config.setTlsTrustCertsFilePath("/path/to/cacert.pem");
  4. config.setTlsAllowInsecureConnection(false);
  5. config.setAuth(pulsar::AuthTls::create(
  6. "/path/to/client-cert.pem", "/path/to/client-key.pem"););
  7. Client client("pulsar+ssl://my-broker.com:6651", config);

For complete examples, refer to C++ client examples.

Schema

This section describes some examples about schema. For more information about schema, see Pulsar schema.

Avro schema

  • The following example shows how to create a producer with an Avro schema.

    1. static const std::string exampleSchema =
    2. "{\"type\":\"record\",\"name\":\"Example\",\"namespace\":\"test\","
    3. "\"fields\":[{\"name\":\"a\",\"type\":\"int\"},{\"name\":\"b\",\"type\":\"int\"}]}";
    4. Producer producer;
    5. ProducerConfiguration producerConf;
    6. producerConf.setSchema(SchemaInfo(AVRO, "Avro", exampleSchema));
    7. client.createProducer("topic-avro", producerConf, producer);
  • The following example shows how to create a consumer with an Avro schema.

    1. static const std::string exampleSchema =
    2. "{\"type\":\"record\",\"name\":\"Example\",\"namespace\":\"test\","
    3. "\"fields\":[{\"name\":\"a\",\"type\":\"int\"},{\"name\":\"b\",\"type\":\"int\"}]}";
    4. ConsumerConfiguration consumerConf;
    5. Consumer consumer;
    6. consumerConf.setSchema(SchemaInfo(AVRO, "Avro", exampleSchema));
    7. client.subscribe("topic-avro", "sub-2", consumerConf, consumer)

ProtobufNative schema

The following example shows how to create a producer and a consumer with a ProtobufNative schema. ​

  1. Generate the User class using Protobuf3.

    note

    You need to use Protobuf3 or later versions.

  1. syntax = "proto3";
  2. message User {
  3. string name = 1;
  4. int32 age = 2;
  5. }

​ 2. Include the ProtobufNativeSchema.h in your source code. Ensure the Protobuf dependency has been added to your project. ​

  1. #include <pulsar/ProtobufNativeSchema.h>

​ 3. Create a producer to send a User instance. ​

  1. ProducerConfiguration producerConf;
  2. producerConf.setSchema(createProtobufNativeSchema(User::GetDescriptor()));
  3. Producer producer;
  4. client.createProducer("topic-protobuf", producerConf, producer);
  5. User user;
  6. user.set_name("my-name");
  7. user.set_age(10);
  8. std::string content;
  9. user.SerializeToString(&content);
  10. producer.send(MessageBuilder().setContent(content).build());

​ 4. Create a consumer to receive a User instance. ​

  1. ConsumerConfiguration consumerConf;
  2. consumerConf.setSchema(createProtobufNativeSchema(User::GetDescriptor()));
  3. consumerConf.setSubscriptionInitialPosition(InitialPositionEarliest);
  4. Consumer consumer;
  5. client.subscribe("topic-protobuf", "my-sub", consumerConf, consumer);
  6. Message msg;
  7. consumer.receive(msg);
  8. User user2;
  9. user2.ParseFromArray(msg.getData(), msg.getLength());