tencent cloud

Feedback

SDK for C++

Last updated: 2022-09-21 18:25:05

    Overview

    This document describes how to use open-source SDK to send and receive messages by using the SDK for C++ as an example and helps you better understand the message sending and receiving processes.

    Prerequisites

    • You have installed GCC.
    • You have downloaded the demo.

    Directions

    1. Prepare the environment.
      1. Install RocketMQ-Client-CPP in the client environment as instructed in the official documentation. The master branch is recommended.
      2. Import the header files and dynamic libraries related to RocketMQ-Client-CPP to the project.
    2. Instantiate the message producer.
      // Set the producer group name
      DefaultMQProducer producer(groupName);
      // Set the service access address
      producer.setNamesrvAddr(nameserver);
      // Set user permissions
      producer.setSessionCredentials(
      accessKey, // Role token
      secretKey, // Role name
      "");
      // Set the full namespace name
      producer.setNameSpace(namespace);
      // Make sure all parameters are configured before the start
      producer.start();
      Parameter Description
      groupName Producer group name, which can be obtained under the Group tab on the Cluster page in the console.
      nameserver Cluster access address, which can be copied from Access Address in the Operation column on the Cluster page in the console. Namespace access addresses in new shared or exclusive clusters can be copied from the Namespace list.
      secretKey Role name, which can be copied on the Role Management page.
      accessKey Role token, which can be copied in the Token column on the Role Management page. img
      namespace The namespace name can be copied under the Topic tab on the Cluster page in the console, which is in the format of cluster ID + | + namespace.
    3. Send messages.
      // Initialize message content
      MQMessage msg(
      topicName, // Topic name
      TAGS, // Message tag
      KEYS, // Message key
      "Hello cpp client, this is a message." // Message content
      )
      ;

      try {
      // Send the message
      SendResult sendResult = producer.send(msg);
      std::cout << "SendResult:" << sendResult.getSendStatus() << ", Message ID: " << sendResult.getMsgId()
      << std::endl;
      } catch (MQException e) {
      std::cout << "ErrorCode: " << e.GetError() << " Exception:" << e.what() << std::endl;
      }
      Parameter Description
      topicName Topic name, which can be copied under the Topic tab on the Cluster page in the console.
      TAGS A parameter used to set the message tag.
      KEYS A parameter used to set the message key.
    4. Release the resources.
      // Release resources
      producer.shutdown();
    5. Initialize the consumer.
      // Listen on messages
      class ExampleMessageListener : public MessageListenerConcurrently {
      public:
      ConsumeStatus consumeMessage(const std::vector<mqmessageext> &msgs)
      {
      for (auto item = msgs.begin(); item != msgs.end(); item++)
      {
      // Business
      std::cout << "Received Message Topic:" << item->getTopic() << ", MsgId:" << item->getMsgId() << ", TAGS:"
      << item->getTags() << ", KEYS:" << item->getKeys() << ", Body:" << item->getBody() << std::endl;
      }
      // Return CONSUME_SUCCESS if the consumption is successful
      return CONSUME_SUCCESS;
      // Return RECONSUME_LATER if the consumption failed. The message will be consumed again.
      // return RECONSUME_LATER;
      }
      };

      // Initialize the consumer
      DefaultMQPushConsumer *consumer = new DefaultMQPushConsumer(groupName);
      // Set the service address
      consumer->setNamesrvAddr(nameserver);
      // Set user permissions
      consumer->setSessionCredentials(
      accessKey,
      secretKey,
      "");
      // Set the namespace
      consumer->setNameSpace(namespace);
      // Set the instance name
      consumer->setInstanceName("CppClient");

      // Register a custom listener function to process the received messages and return the processing results
      ExampleMessageListener *messageListener = new ExampleMessageListener();
      // Subscribe to the message
      consumer->subscribe(topicName, TAGS);
      // Set the message listener
      consumer->registerMessageListener(messageListener);

      // After the preparations, you must call the start function before the consumption can start.
      consumer->start();
      Parameter Description
      groupName Consumer group name, which can be obtained under the Group tab on the Cluster page in the console.
      nameserver Cluster access address, which can be copied from Access Address in the Operation column on the Cluster page in the console. Namespace access addresses in new shared or exclusive clusters can be copied from the Namespace list.
      secretKey Role name, which can be copied on the Role Management page.
      accessKey Role token, which can be copied in the Token column on the Role Management page. img
      namespace The namespace name can be copied under the Topic tab on the Cluster page in the console, which is in the format of cluster ID + | + namespace.
      topicName Topic name, which can be copied under the Topic tab on the Cluster page in the console.
      TAGS A parameter used to set the tag of messages that are subscribed to.
    6. Release the resources.
      // Release resources
      consumer->shutdown();
    7. View consumption details. Log in to the TDMQ console, go to the Cluster > Group page, and view the list of clients connected to the group. Click View Details in the Operation column to view consumer details.

    img

    Note:

    Above is a brief introduction to message publishing and subscription. For more information, see the demo or RocketMQ-Client-CPP Examples.

    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