tencent cloud

Feedback

SDK for C++

Last updated: 2022-05-24 16:53:54

    Overview

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

    Prerequisites

    Directions

    1. Prepare the environment.
    2. Configure dependencies and compile the code for installation as instructed in SDK for C++ Compilation and Installation.
    3. Import relevant header files and dependency libraries to the project.
    4. Create the TDMQ client.
      // Authentication information
      Credential cred = Credential(SECRET_ID, SECRET_KEY);

      HttpProfile httpProfile = HttpProfile();
      httpProfile.SetEndpoint(ENDPOINT);

      ClientProfile clientProfile = ClientProfile();
      clientProfile.SetHttpProfile(httpProfile);
      // Create the TDMQ client
      TdmqClient client = TdmqClient(cred, REGION, clientProfile);
      Parameter Description
      SECRET_ID、SECRET_KEY img
      ENDPOINT API request domain: tdmq.tencentcloudapi.com.
      REGION Cluster region.
    5. Send messages.
      SendMessagesRequest req = 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 message sending timeout period
      req.SetSendTimeout(3000);
      // Send the message
      auto outcome = client.SendMessages(req);
      if (!outcome.IsSuccess())
      {
      cout << outcome.GetError().PrintAll() << endl;
      return -1;
      }
      // Get the result
      SendMessagesResponse resp = outcome.GetResult();
      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.
    6. Consume messages.
      ReceiveMessageRequest req = ReceiveMessageRequest();
      // Set the topic name in the following format: 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(10);
      // It is used to determine the position where the consumer initially receives messages. Valid values: Earliest, Latest.
      req.SetSubInitialPosition("Latest");

      // Receive messages
      auto outcome = client.ReceiveMessage(req);
      if (!outcome.IsSuccess())
      {
      cout << outcome.GetError().PrintAll() << endl;
      return -1;
      }
      // Get the result
      ReceiveMessageResponse resp = outcome.GetResult();
      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.
    7. Acknowledge messages.
      AcknowledgeMessageRequest req = AcknowledgeMessageRequest();
      // Acknowledge the message ID
      req.SetMessageId(messageId);
      // Set the topic name in the following format: cluster (tenant) ID/namespace/topic name
      req.SetAckTopic(topicName);
      // Set the message subscription
      req.SetSubName(subName);
      // Acknowledge messages
      auto outcome = client.AcknowledgeMessage(req);
      if (!outcome.IsSuccess())
      {
      cout << outcome.GetError().PrintAll() << endl;
      return -1;
      }
      // Get the result
      AcknowledgeMessageResponse resp = outcome.GetResult();
      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