TDMQ for Apache Pulsar

TDMQ for Apache Pulsar
SDK Documentation
Product Documentation©2013-2022 Tencent Cloud. All rights reserved.

Page 1 of 69

TDMQ for Apache Pulsar

Copyright Notice©2013-2024 Tencent Cloud. All rights reserved.
Copyright in this document is exclusively owned by Tencent Cloud. You must not reproduce, modify, copy or distributein any way, in whole or in part, the contents of this document without Tencent Cloud's the prior written consent.
Trademark Notice

All trademarks associated with Tencent Cloud and its services are owned by Tencent Cloud Computing (Beijing)
Company Limited and its affiliated companies. Trademarks of third parties referred to in this document are owned bytheir respective proprietors.
Service Statement
This document is intended to provide users with general information about Tencent Cloud's products and services
only and does not form part of Tencent Cloud's terms and conditions. Tencent Cloud's products or services are
subject to change. Specific products and services and the standards applicable to them are exclusively provided for in
Tencent Cloud's applicable terms and conditions.

©2013-2022 Tencent Cloud. All rights reserved.

Page 2 of 69

TDMQ for Apache Pulsar

Contents
SDK Documentation
SDK Overview
Apache Pulsar TCP Protocol
Spring Boot Starter
SDK for JavaSDK for Go
SDK for C++
SDK for Python
SDK for Node.js©2013-2022 Tencent Cloud. All rights reserved.

Page 3 of 69

TDMQ for Apache Pulsar

SDK Documentation
SDK Overview：

Last updated 2024-06-28 11:33:56
TDMQ for Apache Pulsar supports TCP protocol (Pulsar Community Edition) and HTTP protocol. The following are
the supported multi-language SDKs:
Note:
In order to be more consistent with the Pulsar open-source community, we have stopped feature updates for theTencent Cloud SDK since April 30, 2021. We recommend that you use the Apache Pulsar SDK for TDMQ for Apache
Pulsar.
Protocol type

SDK language
Go SDK
Java SDK

TCP protocol (Pulsar Community
Edition)C++ SDK
Python SDK
Node.js SDK
Go SDK
Java SDKHTTP protocol

C++ SDK
Python SDKPHP SDK©2013-2022 Tencent Cloud. All rights reserved.

Page 4 of 69

TDMQ for Apache Pulsar

Apache Pulsar TCP Protocol
Spring Boot Starter：

Last updated 2024-06-28 11:33:56

Overview
This document describes how to use Spring Boot Starter to send and receive messages and helps you better
understand the message sending and receiving processes.

Prerequisites
You have created the required resources.
You have installed JDK 1.8 or later
You have installed Maven 2.5 or later
You have downloaded the demo

Directions
Step 1. Add dependencies
Import Pulsar Starter dependencies to the project.

©2013-2022 Tencent Cloud. All rights reserved.

Page 5 of 69

TDMQ for Apache Pulsar<dependency><groupId>io.github.majusko</groupId><artifactId>pulsar-java-spring-boot-starter</artifactId><version>1.0.7</version></dependency><!-- https://mvnrepository.com/artifact/io.projectreactor/reactor-core --><dependency><groupId>io.projectreactor</groupId><artifactId>reactor-core</artifactId><version>3.4.11</version></dependency>©2013-2022 Tencent Cloud. All rights reserved.

Page 6 of 69

TDMQ for Apache Pulsar

Step 2. Prepare configurations
Add Pulsar configuration information to the configuration file.

pulsar:# Namespace namenamespace: namespace_java# Service access address
service-url: http://pulsar-xxx.tdmq.ap-gz.public.tencenttdmq.com:8080©2013-2022 Tencent Cloud. All rights reserved.

Page 7 of 69

TDMQ for Apache Pulsar# Role token
token-auth-value: eyJrZXlJZC....
# Cluster nametenant: pulsar-xxx

Parameter

Description

namespaceNamespace name, which can be copied on the Namespace page in the console.
Cluster access address, which can be viewed and copied on the Cluster page in the console.

service-url

Role token, which can be copied in the **Token** column on the Role Management page.

token-authvalue

tenantCluster ID, which can be obtained on the Cluster page in the console.

Step 3. Produce messages
1. Configure the producer.

©2013-2022 Tencent Cloud. All rights reserved.

Page 8 of 69

TDMQ for Apache Pulsar@Configuration
public class ProducerConfiguration {@Beanpublic ProducerFactory producerFactory() {return new ProducerFactory()// topic1.addProducer("topic1")// topic2.addProducer("topic2");}©2013-2022 Tencent Cloud. All rights reserved.

Page 9 of 69

TDMQ for Apache Pulsar}

2. Inject the producer.

@Autowired
private PulsarTemplate<byte[]> defaultProducer;

3. Send messages.

©2013-2022 Tencent Cloud. All rights reserved.

Page 10 of 69

TDMQ for Apache Pulsar// Send the messages
defaultProducer.send("topic2", ("Hello pulsar client, this is a order message.").ge

Note:
The topic that sends messages is the one declared in the producer configuration.
The type of PulsarTemplate must be the same as that of the sent message.
When you send a message to the specified topic, the message type must be the same as that bound to the topic in the
producer factory configuration.

©2013-2022 Tencent Cloud. All rights reserved.

Page 11 of 69

TDMQ for Apache Pulsar

Step 4. Consume messages
Configure the consumer.

@PulsarConsumer(topic = "topic1", // Name of the subscribed topic
subscriptionName = "sub_topic1", // Subscription nameserialization = Serialization.JSON, // Serialization method
subscriptionType = SubscriptionType.Shared, // Subscription mode, w
consumerName = "firstTopicConsumer", // Consumer namemaxRedeliverCount = 3, // Maximum number of retries
deadLetterTopic = "sub_topic1-DLQ" // Dead letter topic name)©2013-2022 Tencent Cloud. All rights reserved.

Page 12 of 69

TDMQ for Apache Pulsar

public void topicConsume(byte[] msg) {// TODO process your message
System.out.println("Received a new message. content: [" + new String(msg) + "]"// If the consumption fails, throw an exception, so that the message will enter}

Step 5. Query messages
Log in to the console and enter the Message Query page to view the message trace after running the demo.

The message trace is as follows:©2013-2022 Tencent Cloud. All rights reserved.

Page 13 of 69

TDMQ for Apache Pulsar

Note:
The above is a simple configuration for using TDMQ for Apache Pulsar through Spring Boot Starter. For more
information, see Demo or Spring Boot Starter for Apache Pulsar.

©2013-2022 Tencent Cloud. All rights reserved.

Page 14 of 69

TDMQ for Apache Pulsar

SDK for Java：

Last updated 2024-06-28 11:33:56

Scenarios
This document describes how to use open-source SDK to send and receive messages by using the SDK for Java as
an example and helps you better understand the message sending and receiving processes.

Prerequisites
You have created the required resources.
You have installed JDK 1.8 or later
You have installed Maven 2.5 or later
You have downloaded the demo

Directions
1. Introduce dependencies in a Java project and add the following dependencies to the pom.xml file. This
document uses a Maven project as an example.

©2013-2022 Tencent Cloud. All rights reserved.

Page 15 of 69

TDMQ for Apache Pulsar<dependency><groupId>org.apache.pulsar</groupId><artifactId>pulsar-client</artifactId><version>2.7.2</version></dependency>

Note:
SDK 2.7.2 or later is recommended.

©2013-2022 Tencent Cloud. All rights reserved.

Page 16 of 69

TDMQ for Apache Pulsar

SDK 2.7.4 or later is recommended if you use the batch message sending and receiving feature ( BatchReceive )of the client.
2. Create a Pulsar client.

PulsarClient pulsarClient = PulsarClient.builder()// Service access address.serviceUrl(SERVICE_URL)// Role token.authentication(AuthenticationFactory.token(AUTHENTICATION)).bui©2013-2022 Tencent Cloud. All rights reserved.

Page 17 of 69

TDMQ for Apache Pulsar

Parameter

Description
Cluster access address, which can be viewed and copied on the Cluster page in the consol

SERVICE_URL

Role token, which can be copied in the **Token** column on the Role Management page.

AUTHENTICATION

3. Create a producer.

©2013-2022 Tencent Cloud. All rights reserved.

Page 18 of 69

TDMQ for Apache Pulsar// Create a producer of the `byte[]` type
Producer<byte[]> producer = pulsarClient.newProducer()// Complete path of the topic in the format of `persistent://cluster (tenant.topic("persistent://pulsar-xxx/sdk_java/topic1").create();Note:
You need to enter the complete path of the topic name, i.e., persistent://clusterid/namespace/Topic ,where the clusterid/namespace/topic part can be copied directly from the Topic page in the console.
4. Send the message.

©2013-2022 Tencent Cloud. All rights reserved.

Page 19 of 69

TDMQ for Apache Pulsar// Send the message
MessageId msgId = producer.newMessage()// Message content.value("this is a new message.".getBytes(StandardCharsets.UTF_8))// Business key.key("youKey")// Business parameter.property("mykey", "myvalue").send();

5. Release the resources.

©2013-2022 Tencent Cloud. All rights reserved.

Page 20 of 69

TDMQ for Apache Pulsar// Disable the producer
producer.close();// Disable the clientpulsarClient.close();6. Create a consumer.

©2013-2022 Tencent Cloud. All rights reserved.

Page 21 of 69

TDMQ for Apache Pulsar// Create a consumer of the `byte[]` type (default type)
Consumer<byte[]> consumer = pulsarClient.newConsumer()// Complete path of the topic in the format of `persistent://cluster (tenant.topic("persistent://pulsar-xxx/sdk_java/topic1")// You need to create a subscription on the topic details page in the consol.subscriptionName("sub_topic1")// Declare the exclusive mode as the consumption mode
.subscriptionType(SubscriptionType.Exclusive)// Configure consumption starting at the earliest offset; otherwise, histori.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)// Subscription©2013-2022 Tencent Cloud. All rights reserved.

Page 22 of 69

TDMQ for Apache Pulsar.subscribe();Note:
You need to enter the complete path of the topic name, i.e., persistent://clusterid/namespace/Topic ,where the clusterid/namespace/topic part can be copied directly from the Topic page in the console.

You need to enter the subscription name in the subscriptionName parameter, which can be viewed on theConsumption Management page.
7. Consume the message.

©2013-2022 Tencent Cloud. All rights reserved.

Page 23 of 69

TDMQ for Apache Pulsar// Receive a message corresponding to the current offset
Message<byte[]> msg = consumer.receive();
MessageId msgId = msg.getMessageId();
String value = new String(msg.getValue());
System.out.println("receive msg " + msgId + ",value:" + value);// Messages must be acknowledged after being received; otherwise, the offset wil
consumer.acknowledge(msg);

8. Use the listener for consumption.

©2013-2022 Tencent Cloud. All rights reserved.

Page 24 of 69

TDMQ for Apache Pulsar// Message listener
MessageListener<byte[]> myMessageListener = (consumer, msg) -> {try {System.out.println("Message received: " + new String(msg.getData()));// Return `ack` as the acknowledgement
consumer.acknowledge(msg);} catch (Exception e){// Return `nack` if the consumption fails
consumer.negativeAcknowledge(msg);}};©2013-2022 Tencent Cloud. All rights reserved.

Page 25 of 69

TDMQ for Apache Pulsar

pulsarClient.newConsumer()// Complete path of the topic in the format of `persistent://cluster (tenant.topic("persistent://pulsar-mmqwr5xx9n7g/sdk_java/topic1")// You need to create a subscription on the topic details page in the consol.subscriptionName("sub_topic1")// Declare the exclusive mode as the consumption mode
.subscriptionType(SubscriptionType.Exclusive)// Set the listener.messageListener(myMessageListener)// Configure consumption starting at the earliest offset; otherwise, histori.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).subscribe();9. Log in to the TDMQ for Apache Pulsar console, click Topic > Topic Name to enter the Consumption
Management page, and click the triangle below a subscription name to view the production and consumption
records.

Note:
The above is a brief introduction to the way of publishing and subscribing to messages. For more operations, see
Demo or Pulsar Java client.

©2013-2022 Tencent Cloud. All rights reserved.

Page 26 of 69

TDMQ for Apache Pulsar

SDK for Go：

Last updated 2024-06-28 11:33:56

Overview
This document describes how to use open-source SDK to send and receive messages by using the SDK for Go as an
example and helps you better understand the message sending and receiving processes.

Prerequisites
You have created the required resources as instructed in Resource Creation and Preparation.
You have installed Go.
You have downloaded the demo at here.

Directions
1. Import the pulsar-client-go library in the client environment.
1.1 Run the following command in the client environment to download the dependency package of the Pulsar client.

©2013-2022 Tencent Cloud. All rights reserved.

Page 27 of 69

TDMQ for Apache Pulsar

go get -u "github.com/apache/pulsar-client-go/pulsar"
1.2 After the installation is completed, use the following code to import the client into your Go project file.

©2013-2022 Tencent Cloud. All rights reserved.

Page 28 of 69

TDMQ for Apache Pulsar

import "github.com/apache/pulsar-client-go/pulsar"
2. Create a Pulsar client.

©2013-2022 Tencent Cloud. All rights reserved.

Page 29 of 69

TDMQ for Apache Pulsar// Create a Pulsar client
client, err := pulsar.NewClient(pulsar.ClientOptions{// Service access address
URL: serviceUrl,// Authorize the role token
Authentication:
pulsar.NewAuthenticationToken(authentication),
OperationTimeout: 30 * time.Second,
ConnectionTimeout: 30 * time.Second,})
if err != nil{©2013-2022 Tencent Cloud. All rights reserved.

Page 30 of 69

TDMQ for Apache Pulsar

log.Fatalf("Could not instantiate Pulsar client: %v", err)}
defer client.Close()

Parameter

Description
Cluster access address, which can be viewed and copied on the Cluster Management page in theserviceUrlRole token, which can be copied in the Token column on the Role Management page.
Authentication

3. Create a producer.

©2013-2022 Tencent Cloud. All rights reserved.

Page 31 of 69

TDMQ for Apache Pulsar// Create a producer with the client
producer, err := client.CreateProducer(pulsar.ProducerOptions{// Complete path of the topic in the format of `persistent://cluster (tenant)Topic: "persistent://pulsar-mmqwr5xx9n7g/sdk_go/topic1",})
if err != nil{log.Fatal(err)}defer producer.Close()©2013-2022 Tencent Cloud. All rights reserved.

Page 32 of 69

TDMQ for Apache Pulsar

Note:
You need to enter the complete path of the topic name, i.e., persistent://clusterid/namespace/Topic ,where the clusterid/namespace/topic part can be copied directly from the Topic Management page in the
console.
4. Send a message.

// Send the message_, err = producer.Send(context.Background(), &pulsar.ProducerMessage{// Message content
Payload: []byte("hello go client, this is a message."),©2013-2022 Tencent Cloud. All rights reserved.

Page 33 of 69

TDMQ for Apache Pulsar// Business key
Key: "yourKey",// Business parameter
Properties: map[string]string{"key": "value"},})
5. Create a consumer.

// Create a consumer with the client
consumer, err := client.Subscribe(pulsar.ConsumerOptions{// Complete path of the topic in the format of `persistent://cluster (tenant)Topic:"persistent://pulsar-mmqwr5xx9n7g/sdk_go/topic1",©2013-2022 Tencent Cloud. All rights reserved.

Page 34 of 69

TDMQ for Apache Pulsar// Subscription nameSubscriptionName: "topic1_sub",// Subscription modeType:pulsar.Shared,})
if err != nil{log.Fatal(err)}defer consumer.Close()
Note:
You need to enter the complete path of the topic name, i.e., persistent://clusterid/namespace/Topic ,where the clusterid/namespace/topic part can be copied directly from the Topic Management page in the
console.

You need to enter the subscription name in the subscriptionName parameter, which can be viewed on theConsumption Management page.
6. Consume a message.

©2013-2022 Tencent Cloud. All rights reserved.

Page 35 of 69

TDMQ for Apache Pulsar// Obtain the message
msg, err := consumer.Receive(context.Background())
if err != nil{log.Fatal(err)}// Simulate business processing
fmt.Printf("Received message msgId: %#v -- content: '%s'\\n",msg.ID(), string(msg.Payload()))// If the consumption is successful, return `ack`; otherwise, return `nack` or `Re
consumer.Ack(msg)©2013-2022 Tencent Cloud. All rights reserved.

Page 36 of 69

TDMQ for Apache Pulsar

7. Log in to the TDMQ for Apache Pulsar console, click Topic > Topic Name to enter the consumption management
page, and click the triangle below a subscription name to view the production and consumption records.

Note:
The above is a brief introduction to the way of publishing and subscribing to messages. For more operations, see
Demo or Pulsar Go client.

Customizing Log File Output
Use Cases
As many users don’t customize the logging library when using the Pulsar SDK for Go, logs are output toos.Stderr by default, as shown below:©2013-2022 Tencent Cloud. All rights reserved.

Page 37 of 69

TDMQ for Apache Pulsar// It's recommended to make this a global instance called `log`.
func New() *Logger {return &Logger{
Out:
os.Stderr, // Default output address
Formatter:new(TextFormatter),Hooks:
make(LevelHooks),
Level:InfoLevel,
ExitFunc:
os.Exit,
ReportCaller: false,}}©2013-2022 Tencent Cloud. All rights reserved.

Page 38 of 69

TDMQ for Apache Pulsar

Generally, log information is output to os.Stderr . If you don’t specify a custom logging library, the SDK for Go
logs and business logs will be mixed, making it difficult for troubleshooting.

SolutionWith the logger API exposed on the client by the SDK for Go, you can customize the log output format andlocation and use logging libraries such as logrus and zap . Related parameters are as follows:
1. Implement the log.Logger API provided by the Pulsar SDK for Go by customizing log lib .

// ClientOptions is used to construct a Pulsar Client instance.

©2013-2022 Tencent Cloud. All rights reserved.

Page 39 of 69

TDMQ for Apache Pulsar

type ClientOptions struct {// Configure the logger used by the client.
// By default, a wrapped logrus.StandardLogger will be used, namely,// log.NewLoggerWithLogrus(logrus.StandardLogger())// FIXME: use `logger` as internal field name instead of `log` as it's more idi
Logger log.Logger}
When using the SDK for Go, you can customize the logger API to customize log lib so that you can redirect
logs to a specified location. Taking logrus as an example, the demo below shows you how to customize log
lib to output the SDK for Go logs to a specified file.

©2013-2022 Tencent Cloud. All rights reserved.

Page 40 of 69

TDMQ for Apache Pulsar

package main
import(
"fmt"
"io"
"os"
"github.com/apache/pulsar-client-go/pulsar/log""github.com/sirupsen/logrus")// logrusWrapper implements Logger interface// based on underlying logrus.FieldLogger
type logrusWrapper struct {l logrus.FieldLogger}// NewLoggerWithLogrus creates a new logger which wraps// the given logrus.Logger
func NewLoggerWithLogrus(logger *logrus.Logger, outputPath string) log.Logger {writer1 := os.Stdout
writer2, err := os.OpenFile(outputPath, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil{logrus.Error("create file log.txt failed: %v", err)}
logger.SetOutput(io.MultiWriter(writer1, writer2))
return &logrusWrapper{
l: logger,}}
func (l *logrusWrapper) SubLogger(fs log.Fields) log.Logger {return &logrusWrapper{l: l.l.WithFields(logrus.Fields(fs)),}}
func (l *logrusWrapper) WithFields(fs log.Fields) log.Entry {return logrusEntry{
e: l.l.WithFields(logrus.Fields(fs)),}}
func (l *logrusWrapper) WithField(name string, value interface{}) log.Entry {return logrusEntry{©2013-2022 Tencent Cloud. All rights reserved.

Page 41 of 69

TDMQ for Apache Pulsar

e: l.l.WithField(name, value),}}func (l *logrusWrapper) WithError(err error) log.Entry {return logrusEntry{
e: l.l.WithError(err),}}
func (l *logrusWrapper) Debug(args ...interface{}) {l.l.Debug(args...)}func (l *logrusWrapper) Info(args ...interface{}) {l.l.Info(args...)}func (l *logrusWrapper) Warn(args ...interface{}) {l.l.Warn(args...)}func (l *logrusWrapper) Error(args ...interface{}) {l.l.Error(args...)}func (l *logrusWrapper) Debugf(format string, args ...interface{}) {l.l.Debugf(format, args...)}func (l *logrusWrapper) Infof(format string, args ...interface{}) {l.l.Infof(format, args...)}func (l *logrusWrapper) Warnf(format string, args ...interface{}) {l.l.Warnf(format, args...)}func (l *logrusWrapper) Errorf(format string, args ...interface{}) {l.l.Errorf(format, args...)}type logrusEntry struct {e logrus.FieldLogger}
func (l logrusEntry) WithFields(fs log.Fields) log.Entry {©2013-2022 Tencent Cloud. All rights reserved.

Page 42 of 69

TDMQ for Apache Pulsar

return logrusEntry{
e: l.e.WithFields(logrus.Fields(fs)),}}
func (l logrusEntry) WithField(name string, value interface{}) log.Entry {return logrusEntry{
e: l.e.WithField(name, value),}}func (l logrusEntry) Debug(args ...interface{}) {l.e.Debug(args...)}func (l logrusEntry) Info(args ...interface{}) {l.e.Info(args...)}func (l logrusEntry) Warn(args ...interface{}) {l.e.Warn(args...)}func (l logrusEntry) Error(args ...interface{}) {l.e.Error(args...)}func (l logrusEntry) Debugf(format string, args ...interface{}) {l.e.Debugf(format, args...)}func (l logrusEntry) Infof(format string, args ...interface{}) {l.e.Infof(format, args...)}func (l logrusEntry) Warnf(format string, args ...interface{}) {l.e.Warnf(format, args...)}func (l logrusEntry) Errorf(format string, args ...interface{}) {l.e.Errorf(format, args...)}2. Specify a custom log lib when creating the client.

©2013-2022 Tencent Cloud. All rights reserved.

Page 43 of 69

TDMQ for Apache Pulsar

client, err := pulsar.NewClient(pulsar.ClientOptions{
URL:"pulsar://localhost:6650",
Logger: NewLoggerWithLogrus(log.StandardLogger(), "test.log"),})
The above demo shows you how to redirect the log file of the Pulsar SDK for Go to the test.log file in the current
path. You can redirect the log file to a specified location as needed.

©2013-2022 Tencent Cloud. All rights reserved.

Page 44 of 69

TDMQ for Apache Pulsar

SDK for C++：

Last updated 2024-06-28 11:33:56

Overview
This document describes how to use open-source SDK to send and receive messages by using the SDK for C++ asan example and helps you better understand the message sending and receiving processes.

Prerequisites
You have created the required resources.
You have installed GCC
You have downloaded the demo

Directions
1. Prepare the environment.
1.1 Install the Pulsar C++ client in the client environment as instructed in Pulsar C++ client.
1.2 Introduce the files and dynamic libraries that are related to the Pulsar C++ client to the project.
2. Create a client.

©2013-2022 Tencent Cloud. All rights reserved.

Page 45 of 69

TDMQ for Apache Pulsar// Client configuration information
ClientConfiguration config;// Set the role token
AuthenticationPtr auth = pulsar::AuthToken::createWithToken(AUTHENTICATION);
config.setAuth(auth);// Create a client
Client client(SERVICE_URL, config);

Parameter

Description©2013-2022 Tencent Cloud. All rights reserved.

Page 46 of 69

TDMQ for Apache Pulsar

SERVICE_URL

Cluster access address, which can be viewed and copied on the Cluster page in the consolRole token, which can be copied in the Token column on the Role Management page.

AUTHENTICATION

3. Create a producer.

©2013-2022 Tencent Cloud. All rights reserved.

Page 47 of 69

TDMQ for Apache Pulsar// Producer configurations
ProducerConfiguration producerConf;
producerConf.setBlockIfQueueFull(true);producerConf.setSendTimeout(5000);// Producer
Producer producer;// Create a producer
Result result = client.createProducer(// Complete path of the topic in the format of `persistent://cluster (tenant) I"persistent://pulsar-xxx/sdk_cpp/topic1",
producerConf,©2013-2022 Tencent Cloud. All rights reserved.

Page 48 of 69

TDMQ for Apache Pulsar

producer);if (result != ResultOk) {std::cout << "Error creating producer: " << result << std::endl;
return -1;}

Note:
You need to enter the complete path of the topic name, i.e., persistent://clusterid/namespace/Topic ,where the clusterid/namespace/topic part can be copied directly from the Topic page in the console.
4. Send the message.

©2013-2022 Tencent Cloud. All rights reserved.

Page 49 of 69

TDMQ for Apache Pulsar// Message content
std::string content = "hello cpp client, this is a msg";// Create a message object
Message msg = MessageBuilder().setContent(content).setPartitionKey("mykey") // Business key.setProperty("x", "1")// Set message parameters.build();// Send the message
Result result = producer.send(msg);
if (result != ResultOk) {// The message failed to be sent
std::cout << "The message " << content << " could not be sent, received code
} else {// The message was successfully sent
std::cout << "The message " << content << " sent successfully" << std::endl;}

5. Create a consumer.

©2013-2022 Tencent Cloud. All rights reserved.

Page 50 of 69

TDMQ for Apache Pulsar// Consumer configuration information
ConsumerConfiguration consumerConfiguration;
consumerConfiguration.setSubscriptionInitialPosition(pulsar::InitialPositionEarl// Consumer
Consumer consumer;// Subscribe to a topic
Result result = client.subscribe(// Complete path of the topic in the format of `persistent://cluster (tenant"persistent://pulsar-xxx/sdk_cpp/topic1",// Subscription name"sub_topic1",©2013-2022 Tencent Cloud. All rights reserved.

Page 51 of 69

TDMQ for Apache Pulsar

consumerConfiguration,
consumer);
if (result != ResultOk) {std::cout << "Failed to subscribe: " << result << std::endl;
return -1;}

Note:
You need to enter the complete path of the topic name, i.e., persistent://clusterid/namespace/Topic ,where the clusterid/namespace/topic part can be copied directly from the Topic page in the console.

You need to enter the subscription name in the subscriptionName parameter, which can be viewed on theConsumption Management page.
6. Consume the message.

©2013-2022 Tencent Cloud. All rights reserved.

Page 52 of 69

TDMQ for Apache Pulsar

Message msg;// Obtain the message
consumer.receive(msg);// Simulate the business processing logic
std::cout << "Received: " << msg << " with payload '" << msg.getDataAsString()// Return `ack` as the acknowledgement
consumer.acknowledge(msg);// Return `nack` if the consumption fails, and the message will be delivered aga// consumer.negativeAcknowledge(msg);©2013-2022 Tencent Cloud. All rights reserved.

Page 53 of 69

TDMQ for Apache Pulsar

7. Log in to the TDMQ for Apache Pulsar console, click Topic > Topic Name to enter the Consumption
Management page, and click the triangle below a subscription name to view the production and consumption
records.

Note:
The above is a brief introduction to the way of publishing and subscribing to messages. For more operations, see
Demo or Pulsar C++ client.

©2013-2022 Tencent Cloud. All rights reserved.

Page 54 of 69

TDMQ for Apache Pulsar

SDK for Python：

Last updated 2024-06-28 11:33:56

Overview
This document describes how to use open-source SDK to send and receive messages by using the SDK for Python
as an example and helps you better understand the message sending and receiving processes.

Prerequisites
You have created or prepared the required resources as instructed in Resource Creation and Preparation.
You have installed Python. For the download address, click here.
You have installed pip. For the download address, click here.
You have downloaded the demo. For the download address, click here.

Directions
1. Prepare the environment.
Install the pulsar-client library in the client environment as instructed in Pulsar Python client.

©2013-2022 Tencent Cloud. All rights reserved.

Page 55 of 69

TDMQ for Apache Pulsar

pip install 'pulsar-client==3.1.0'
2. Create a client.

©2013-2022 Tencent Cloud. All rights reserved.

Page 56 of 69

TDMQ for Apache Pulsar# Create a client
client = pulsar.Client(authentication=pulsar.AuthenticationToken(# Authorized role token
AUTHENTICATION),# Service access address
service_url=SERVICE_URL)
Parameter

Description©2013-2022 Tencent Cloud. All rights reserved.

Page 57 of 69

TDMQ for Apache Pulsar

SERVICE_URL

Cluster access address, which can be viewed and copied on the Cluster page in the consolRole token, which can be copied in the Token column on the Role Management page.
AUTHENTICATION

3. Create a producer.

©2013-2022 Tencent Cloud. All rights reserved.

Page 58 of 69

TDMQ for Apache Pulsar# Create a producer
producer = client.create_producer(# Complete path of the topic in the format of `persistent://cluster (tenant) I
topic='pulsar-xxx/sdk_python/topic1')
Note:
You need to enter the complete path of the topic name, that is,persistent://clusterid/namespace/Topic , where the clusterid/namespace/topic part can becopied directly from the Topic page in the console.

©2013-2022 Tencent Cloud. All rights reserved.

Page 59 of 69

TDMQ for Apache Pulsar

4. Send the message.

# Send the message
producer.send(# Message content'Hello python client, this is a msg.'.encode('utf-8'),# Message parameter
properties={'k': 'v'},# Business key
partition_key='yourKey')©2013-2022 Tencent Cloud. All rights reserved.

Page 60 of 69

TDMQ for Apache Pulsar

The message can also be sent in async mode.

# Send the callback in async modedef send_callback(send_result, msg_id):
print('Message published: result:{}

msg_id:{}'.format(send_result, msg_id))# Send the message
producer.send_async(# Message content'Hello python client, this is a async msg.'.encode('utf-8'),# Async callback©2013-2022 Tencent Cloud. All rights reserved.

Page 61 of 69

TDMQ for Apache Pulsar

callback=send_callback,# Message configuration
properties={'k': 'v'},# Business key
partition_key='yourKey')
5. Create a consumer.

# Subscribe to the message
consumer = client.subscribe(# Complete path of the topic in the format of `persistent://cluster (tenant) I©2013-2022 Tencent Cloud. All rights reserved.

Page 62 of 69

TDMQ for Apache Pulsar

topic='pulsar-xxx/sdk_python/topic1',# Subscription namesubscription_name='sub_topic1')
Note:
You need to enter the complete path of the topic name, that is,persistent://clusterid/namespace/Topic , where the clusterid/namespace/topic part can becopied directly from the Topic page in the console.

You need to enter the subscription name for the subscriptionName parameter. The name can be viewed on theConsumption Management page.
6. Consume the message.

©2013-2022 Tencent Cloud. All rights reserved.

Page 63 of 69

TDMQ for Apache Pulsar# Obtain the message
msg = consumer.receive()try:# Simulate the business processing logic
print("Received message '{}' id='{}'".format(msg.data(), msg.message_id()))# Return `ack` as the acknowledgement if the consumption is successful
consumer.acknowledge(msg)
except:# If the consumption fails, the message will be delivered again.
consumer.negative_acknowledge(msg)©2013-2022 Tencent Cloud. All rights reserved.

Page 64 of 69

TDMQ for Apache Pulsar

7. Log in to the TDMQ for Apache Pulsar console, click Topic > Topic Name to enter the consumption management
page, and click the triangle below a subscription name to view the production and consumption records.

Note
Above is a brief introduction to message publishing and subscription. For more information, see Demo or Pulsar
Python client.

©2013-2022 Tencent Cloud. All rights reserved.

Page 65 of 69

TDMQ for Apache Pulsar

SDK for Node.js：

Last updated 2024-08-08 11:10:24

Overview
TDMQ for Apache Pulsar 2.7.1 and above clusters already support Apache Pulsar SDK for Node.js. This document
describes how to access the SDK.

Prerequisites
Get the access address
Copy the access address on the Cluster Management page in the TDMQ for Apache Pulsar console.
Get the token
Configure the role and permission as instructed in Role and Authentication and get the token of the role.

Directions
1. Install the Node.js client in your client environment as instructed in Pulsar Node.js client.

©2013-2022 Tencent Cloud. All rights reserved.

Page 66 of 69

TDMQ for Apache Pulsar$ npm install pulsar-client
2. In the code for creating the Node.js client, configure the prepared access address and token.

©2013-2022 Tencent Cloud. All rights reserved.

Page 67 of 69

TDMQ for Apache Pulsar

const Pulsar = require('pulsar-client');(async () => {const client = new Pulsar.Client({
serviceUrl: 'http://*', // Replace with the access address (copied from theauthentication:
Pulsar.NewAuthenticationToken("eyJh**"), // Replace with});await client.close();})();©2013-2022 Tencent Cloud. All rights reserved.

Page 68 of 69

TDMQ for Apache Pulsar

For how to use various features of the Apache Pulsar SDK for Node.js, see Pulsar Node.js client.

©2013-2022 Tencent Cloud. All rights reserved.

Page 69 of 69