Overview
This document describes how to download the demo, perform a simple test, and run a client after you purchase the TDMQ for Apache Pulsar and CVM services.
Notes
The following takes the Java client as an example. For clients in other languages, see SDK Overview. Prerequisites
Note:
The region and VPC of the cloud server should match those of the existing Pulsar cluster.
Directions
1. Download the demo here and configure relevant parameters. About Maven dependencies
The dependencies in the pom.xml
file are configured according to Pulsar's official dependencies. For more information, see Pulsar Java client.
<pulsar.version>2.7.2</pulsar.version>
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-client</artifactId>
<version>${pulsar.version}</version>
</dependency>
Create a client
PulsarClient client = PulsarClient.builder()
.serviceUrl("http://pulsar-..tencenttdmq.com:8080")
.authentication(AuthenticationFactory.token("eyJr"))
.build();
System.out.println(">> pulsar client created.");
Note:
serviceUrl
is the access address, which can be viewed and copied on the Cluster page in the console.
token
is the role token, which can be copied on the Role Management page.Token leakage may lead to data leakage; therefore, you should keep your token confidential.
Create a consumer process
Consumer<byte[]> consumer = client.newConsumer()
.topic("persistent://pulsar-****/namespace/topicName")
.subscriptionName("subscriptionName")
.subscriptionType(SubscriptionType.Exclusive)
.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
.subscribe();
System.out.println(">> pulsar consumer created.");
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 the Consumption Management page.
Create a producer process
Producer<byte[]> producer = client.newProducer()
.topic("persistent://pulsar-****/namespace/topicName")
.create();
System.out.println(">> pulsar producer created.");
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. Produce a message
for (int i = 0; i < 5; i++) {
String value = "my-sync-message-" + i;
MessageId msgId = producer.newMessage().value(value.getBytes()).send();
System.out.println("deliver msg " + msgId + ",value:" + value);
}
producer.close();
Consume a message
for (int i = 0; i < 5; i++) {
Message<byte[]> msg = consumer.receive();
MessageId msgId = msg.getMessageId();
String value = new String(msg.getValue());
System.out.println("receive msg " + msgId + ",value:" + value);
consumer.acknowledge(msg);
}
2. Run the mvn clean package
command in the directory of pom.xml
or use the features of the IDE to package the entire project and generate an executable JAR file in the target
directory.
4. Log in to the CVM instance and enter the directory of the JAR file just uploaded, where you can see that the file has been uploaded to the CVM instance.
5. Run the java -jar tdmq-demo-1.0.0.jar
command to run the demo and view the execution logs.
6. 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. 7. Enter the Message Query page to view the message trace after running the demo. Note:
You can only query the trace of one single message. If you enable the batch feature in the producer, only the first message in a batch can be queried for its trace.
The message trace is as follows:
Was this page helpful?