tencent cloud

Feedback

SDK for C++

Last updated: 2023-05-16 11:07:52

    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.

    Directions

    1. Prepare the environment.
    1.1 Install RocketMQ-Client-CPP in the client environment as instructed in the official documentation. The master branch is recommended.
    1.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 copied under the Group tab on the Cluster page in the console.
    nameserver
    Cluster access address, which can be obtained in the Operation column on the Cluster Management page. Namespace access addresses in new virtual 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.
    
    namespace
    Namespace name, which can be copied on the Namespace page in the console.
    3. Send a message.
    // 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 on the Topic 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 resource.
    // 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 details page in the console.
    nameserver
    Cluster access address, which can be obtained in the Operation column on the Cluster Management page Namespace access addresses in new virtual 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.
    
    namespace
    Namespace name, which can be copied on the Namespace page in the console.
    topicName
    Topic name, which can be copied on the Topic page in the console.
    TAGS
    A parameter used to set the message tag.
    6. Release the resource.
    // Release resources
    consumer->shutdown();
    7. View consumer 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.
    
    
    Note
    Above is a brief introduction to message publishing and subscription. Above is a brief introduction to message publishing and For more information, see Demo or RocketMQ-Client-CPP Example.
    
    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