tencent cloud

Feedback

Downloading and Running Demo

Last updated: 2022-06-29 15:16:26

    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.

    Note

    The following takes the Java client as an example. For clients in other languages, see SDK Overview.

    Prerequisites

    You have purchased a CVM instance.

    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.1</pulsar.version>
      <!-- in your <dependencies> block -->
      <dependency>
      <groupid>org.apache.pulsar</groupid>
      <artifactid>pulsar-client</artifactid>
      <version>${pulsar.version}</version>
      </dependency>
      Create a client

      // 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

      PulsarClient client = PulsarClient.builder()
      // Replace it with the cluster access address displayed on the Cluster Management 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 Management 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 **Topic Management**
      .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 Management 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 Management page in the console.

      Produce a message
      for (int i = 0; i < 5; i++) {
      String value = "my-sync-message-" + i;
      // Send a message
      MessageId msgId = producer.newMessage().value(value.getBytes()).send();
      System.out.println("deliver msg " + msgId + ",value:" + value);
      }
      // Disable the producer
      producer.close();
      Consume the 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 after being received; otherwise, the offset will be held in the position of the current message, and consumption cannot continue
      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 Management > 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.

      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