tencent cloud

Using the SDK to Send and Receive General Messages
Last updated:2025-12-24 14:54:02
Using the SDK to Send and Receive General Messages
Last updated: 2025-12-24 14:54:02

Scenarios

This document describes how to download the demo and perform simple tests after resources, such as clusters and topics, are created in the TDMQ for Apache Pulsar console, helping you get started with TDMQ for Apache Pulsar services quickly.

Prerequisites

You have completed the preparations.

Operation Steps

Step 1: Creating a Cluster and Configuring the Network

1. Log in to the TDMQ for Apache Pulsar console, go to the Cluster page, and select the target region.
2. Click Create Cluster to create a cluster.
3. Click Access Address in the operation column of the created cluster.



Note
For more information and operations related to clusters, see Managing Clusters.
VPC access addresses do not support cross-region access. Ensure that the client and cluster are in the same region.

Step 2: Creating a Namespace

In the TDMQ for Apache Pulsar console, select the region and the newly created cluster on the Namespace page, and click Create to create a namespace.


Step 3: Creating a Role and Granting Authorization

1. In the TDMQ for Apache Pulsar console, select the region and the newly created cluster on the Role Management page, and click Create to go to the New Role page.
2. Specify the role name and description, and click Save to complete role creation.
3. Go to the Namespace page, and click Configure Permissions in the operation column of the newly created namespace to go to the permissions list of the namespace.
4. On the Configure Permissions page, click Add Role to add the newly created role and assign production and consumption permissions.


5. The following figure indicates a successful configuration.



Step 4: Creating a Topic and Subscription Relationship

1. On the Topic page, select the target region, cluster, and namespace at the top, and click Create to create a topic.
2. Click Add Subscription in the operation column to create a subscription relationship for the newly created topic.
3. Click More in the operation column and choose View Subscriptions/Consumers from the drop-down list to view the newly created subscription.



Step 5: Downloading and Running the Demo

1. Download the demo (Demo download address), and configure relevant parameters.
About Maven dependencies
The dependencies in the pom.xml file are configured based on the official dependencies of TDMQ for Apache Pulsar. For details, see the official documentation. For specific versions of the TDMQ for Apache Pulsar SDK, see the versions and usage instructions provided in Java SDK.
<!-- in your <properties> block -->
<pulsar.version>2.7.2</pulsar.version>
<!-- in your <dependencies> block -->
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-client</artifactId>
<version>${pulsar.version}</version>
</dependency>
Creating a client
// One TDMQ for Apache Pulsar client corresponds to one client connection.
// In principle, one process has one client. Avoid repeated creation, which consumes resources.
// For use cases about clients and producers/consumers, see the official documentation at https://www.tencentcloud.com/document/product/1179/58090?from_cn_redirect=1.

PulsarClient client = PulsarClient.builder()
//Replace it with the cluster access address on the Cluster page.
.serviceUrl("http://pulsar-..tencenttdmq.com:8080")
//Replace it with the role token on the Role Management page.
.authentication(AuthenticationFactory.token("eyJr"))
.build();

System.out.println(">> pulsar client created.");
Note:
serviceUrl is the access address, which can be viewed and copied from the Cluster page in the console.



token is the role token, which can be copied from Role Management. Token leakage may lead to data leakage. Keep your token secure.
Creating a consumer process
Consumer<byte[]> consumer = client.newConsumer()
//Full topic path, in the format of persistent://cluster (tenant) ID/namespace/topic name, copied from the Topic page.
.topic("persistent://pulsar-****/namespace/topicName")
//Create a subscription on the topic details page in the console and specify the subscription name here.
.subscriptionName("subscriptionName")
//Declare the consumption mode to be the Exclusive mode.
.subscriptionType(SubscriptionType.Exclusive)
//Configure consumption from the earliest time. Otherwise, historical messages may not be consumed.
.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
.subscribe();
System.out.println(">> pulsar consumer created.");
Note:
For the topic name, specify the full path in the format of persistent://clusterid/namespace/Topic. The clusterid/namespace/topic part can be copied from the Topic page in the console.



For subscriptionName, specify the subscription name, which can be viewed on the Consumer page.
Creating a producer process
Producer<byte[]> producer = client.newProducer()
//Full topic path, in the format of persistent://cluster (tenant) ID/namespace/topic name.
.topic("persistent://pulsar-****/namespace/topicName")
.create();
System.out.println(">> pulsar producer created.");
Note:
For the topic name, specify the full path in the format of persistent://clusterid/namespace/Topic. The clusterid/namespace/topic part can be copied from the Topic page in the console.
Producing messages
for (int i = 0; i < 5; i++) {
String value = "my-sync-message-" + i;
//Send messages.
MessageId msgId = producer.newMessage().value(value.getBytes()).send();
System.out.println("deliver msg " + msgId + ",value:" + value);
}
//Disable the producer.
producer.close();
Consuming messages
for (int i = 0; i < 5; i++) {
//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);
//After the message is received, it must be acknowledged. Otherwise, the offset remains at the current message, and other messages cannot be consumed.
consumer.acknowledge(msg);
}
2. In the directory where pom.xml is located, run the mvn clean package command. Alternatively, use the integrated development environment (IDE) built-in feature to package the entire project and generate an executable JAR file in the target directory.



3. After successful execution, upload the JAR file to Cloud Virtual Machine (CVM). For specific operations, see How to Copy Local Files to CVM.
4. Log in to CVM and go to the directory where the uploaded JAR file is located. You can see that the file has been uploaded to CVM.

5. Run the tdmq-demo-cloud-1.0.0.jar command to run the demo and view the running logs.

6. Log in to the TDMQ for Apache Pulsar console, choose Topic > Topic Name to go to the Consumption Management page. Click the rightward chevron next to the subscription name to view the production and consumption records.




Step 6: Querying Messages

1. Go to the Message Query page to view the message traces after the demo runs.
Note:
Only the message trace of a single message can be queried at a time. If the batch feature is enabled on the producer side, only the message trace of the first message within a batch is queried.



The following figure shows the message trace.




Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback