tencent cloud

Feedback

SDK for Java

Last updated: 2022-05-24 16:33:26

    Overview

    This document uses Java as an example to describe how to connect to TDMQ for Pulsar to send and receive messages over the HTTP protocol.

    Prerequisites

    Directions

    1. Add SDK for Java dependencies first.
      <dependency>
      <groupid>com.tencentcloudapi</groupid>
      <artifactid>tencentcloud-sdk-java-tdmq</artifactid>
      <!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
      <!-- Query the latest version at https://search.maven.org/search?q=tencentcloud-sdk-java, which is as follows -->
      <version>3.1.423</version>
      </dependency>
    2. Create the TDMQ client.
      // Instantiate an authentication object. Pass in secretID and secretKey of your Tencent Cloud account as the input parameters and keep them confidential.
      // You can get them at https://console.tencentcloud.com/cam/capi
      Credential cred = new Credential(secretId, secretKey);
      // (Optional) Instantiate an HTTP option
      HttpProfile httpProfile = new HttpProfile();
      httpProfile.setEndpoint(endpoint);
      // (Optional) Instantiate a client option
      ClientProfile clientProfile = new ClientProfile();
      clientProfile.setHttpProfile(httpProfile);
      // Instantiate the client object of the requested product. `clientProfile` is optional
      TdmqClient client = new TdmqClient(cred, region, clientProfile);
      Parameter Description
      secretId、secretKey TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. img
      endpoint API request domain: tdmq.tencentcloudapi.com.
      region Cluster region.
    3. Send messages.
      // Instantiate a request object. Each API corresponds to a request object
      SendMessagesRequest req = new SendMessagesRequest();
      // Set the authorized role key
      req.setStringToken(token);
      // Set the authorized role name
      req.setProducerName(userName);
      // Set the topic name in the following format: cluster (tenant) ID/namespace/topic name
      req.setTopic(topicName);
      // Message content
      req.setPayload("this is a new message.");
      // Set the sending timeout period
      req.setSendTimeout(3000L);
      // The returned `resp` is an instance of `SendMessagesResponse` which corresponds to the request object
      SendMessagesResponse resp = client.SendMessages(req);
      Parameter Description
      token Role key, which can be copied in the Key column on the Role Management page. img
      userName Role name, which can be copied in the Name column on the Role Management page.
      topicName Topic name in the following format: cluster (tenant) ID/namespace/topic name, such as `pulsar-xxx/sdk_http/topic1`. You can directly copy it on the Topic Management page in the console.
    4. Consume messages.
      // Instantiate a request object. Each API corresponds to a request object
      ReceiveMessageRequest req = new ReceiveMessageRequest();
      // Set the topic name in the following format: persistent://cluster (tenant) ID/namespace/topic name
      req.setTopic(topicName);
      // Set the subscription name
      req.setSubscriptionName(subName);
      // Messages received by the consumer will first be stored in the `receiverQueueSize` queue to tune the message receiving rate
      req.setReceiverQueueSize(10L);
      // It is used to determine the position where the consumer initially receives messages. Valid values: Earliest, Latest.
      req.setSubInitialPosition("Latest");
      // The returned `resp` is an instance of `ReceiveMessageResponse` which corresponds to the request object
      ReceiveMessageResponse resp = client.ReceiveMessage(req);
      Parameter Description
      topicName Topic name in the following format: cluster (tenant) ID/namespace/topic name, such as `pulsar-xxx/sdk_http/topic1`. You can directly copy it on the Topic Management page in the console.
      subName Subscription name, which can be copied on the Cluster > Consumer tab in the console.
    5. Acknowledge messages.
      // Instantiate a request object. Each API corresponds to a request object
      AcknowledgeMessageRequest req = new AcknowledgeMessageRequest();
      // Set the message ID
      req.setMessageId(messageId);
      // Set the topic name in the following format: persistent://cluster (tenant) ID/namespace/topic name
      req.setAckTopic(topicName);
      // Set the subscription name
      req.setSubName(subName);
      // The returned `resp` is an instance of `AcknowledgeMessageResponse` which corresponds to the request object
      AcknowledgeMessageResponse resp = client.AcknowledgeMessage(req);
      Parameter Description
      messageId Message ID obtained after message consumption.
      topicName Topic name in the following format: cluster (tenant) ID/namespace/topic name, such as `pulsar-xxx/sdk_http/topic1`. You can directly copy it on the Topic Management page in the console.
      subName Subscription name, which can be copied on the Cluster > Consumer tab in the console.
    Note:

    Above is a brief introduction to message sending and receiving operations. For more information, see Demo or API Explorer.

    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