tencent cloud

Feedback

Downloading and Running Demo

Last updated: 2024-01-03 14:12:49

    Overview

    This document describes how to download the demo, perform a simple test, and run a client after you purchase the TDMQ for Pulsar and CVM services.
    Notes
    The following takes the Java client as an example. For clients in other languages, see SDK Overview.

    Prerequisites

    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.
    <!-- 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>
    Create a client
    Access sample for cluster on v2.7.1 or later
    Access sample for cluster on v2.6.1
    // One Pulsar client corresponds to one client connection
    // In principle, one process corresponds to one client. Try to avoid repeated creation, as this may consume resources.
    // For the best practice of clients and producers/consumers, see [Client Connection and Producer/Consumer](https://www.tencentcloud.com/document/product/1179/58090?from_cn_redirect=1).
    
    PulsarClient client = PulsarClient.builder()
    // Replace it with the cluster access address displayed on the **Cluster** page
    .serviceUrl("http://pulsar-..tencenttdmq.com:8080")
    // Replace it with the role token displayed on the **Role Management** page
    .authentication(AuthenticationFactory.token("eyJr"))
    .build();
    
    System.out.println(">> pulsar client created.");
    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.
    Note:
    Token leakage may lead to data leakage; therefore, you should keep your token confidential.
    
    PulsarClient client = PulsarClient.builder()
    .serviceUrl("pulsar://...:6000/")// Access address, which can be copied from the access point list on the **Cluster** page
    .listenerName("custom:pulsar-/vpc-/subnet-")// Replace the value of `custom:` with the route ID in the access point list on the **Cluster** page
    .authentication(AuthenticationFactory.token("eyJr"))// Replace it with the role token displayed on the **Role Management** page
    .build();
    System.out.println(">> pulsar client created.");
    serviceUrl is the access address, which can be viewed and copied on the Cluster > Access Point page in the console.
    listenerName is the custom: plus the route ID (NetModel), which can be viewed and copied on the Cluster > Access Point page in the console.
    token is the role token, which can be copied on the Role Management page.
    Note:
    Token leakage may lead to data leakage; therefore, you should keep your token confidential.
    
    Create a consumer process
    Consumer<byte[]> consumer = client.newConsumer()
    // Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`, which can be copied from the **Topic** page.
    .topic("persistent://pulsar-****/namespace/topicName")
    // You need to create a subscription on the topic details page in the console and enter the subscription name here
    .subscriptionName("subscriptionName")
    // Declare the exclusive mode as the consumption mode
    .subscriptionType(SubscriptionType.Exclusive)
    // Configure consumption starting at the earliest offset; otherwise, historical messages may not be consumed
    .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()
    // Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`
    .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;
    // Send the message
    MessageId msgId = producer.newMessage().value(value.getBytes()).send();
    System.out.println("deliver msg " + msgId + ",value:" + value);
    }
    // Close the producer
    producer.close();
    Consume a message
    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);
    // Messages must be acknowledged once received; otherwise, the offset will stay at the current message position without going forwards for consumption.
    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.
    
    3. After successful execution, upload the JAR file to the CVM instance. For directions, see Copying Local Files to CVMs.
    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.
    
    
    
    Run the java -jar tdmq-demo-1.0.0.jar command to run the demo and view the execution logs.
    
    
    
    5. Log in to the TDMQ for 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.
    6. 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:
    
    
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support