tencent cloud

All product documents
TDMQ for RocketMQ
Use of C++ SDK
Last updated: 2024-01-17 16:56:58
Use of C++ SDK
Last updated: 2024-01-17 16:56:58

Overview

This document describes how to use an open-source SDK to send and receive messages with the SDK for C++ serving as example, for you to better understand the complete procedure involved in message sending and receiving.

Prerequisites

Directions:

1. Prepare the environment.
1.1 You need to install the RocketMQ-Client-CPP library in the client environment. Follow the official guide to Install the CPP Dynamic Library.The master branch build is recommended.
1.2 Incorporate the associated header files and dynamic libraries of RocketMQ-Client-CPP into the project.
2. Initialize 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 key
secretKey, // Role name
"");
// Set the namespace (full namespace name)
producer.setNameSpace(namespace);
// Ensure that the parameters are set before initiation
producer.start();
Parameter
Description
groupName
Producer group name, which can be obtained from the Group tab of cluster management on the console.
nameserver
Cluster access address in the basic information of the cluster. Select either the private network or public network access address as needed.

secretKey
Role name, which can be copied from SecretKey on the Cluster Permission page.
accessKey
Role key, which can be copied from AccessKey on the Cluster Permission page.

3. Send the message.
// Initialize message content
MQMessage msg(
topicName, // Topic name
TAGS, // Message tag
KEYS, // Business 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 from the Topic tab on the console.
TAGS
Used to set the message tag.
KEYS
Used to configure the message business key.
4. Release the resources.
// Release resources
producer.shutdown();
5. Initialize the consumer.
// Monitor 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` when consumption is successful
return CONSUME_SUCCESS;
// Return `RECONSUME_LATER` when consumption fails. The message will be re-consumed
// 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");

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

// After the preparations are complete, the startup function must be called for normal operation.
consumer->start();
Parameter
Description
groupName
Consumer group name, which can be obtained from the Group tab of cluster management on the console.
nameserver
Cluster access address, which can be obtained by clicking on Get Access Address in the Operation column on the Cluster Management page.

secretKey
Role name, which can be copied from SecretKey on the Cluster Permission page.
accessKey
Role key, which can be copied from AccessKey on the Cluster Permission page.

topicName
Topic name, which can be copied from the Topic tab on the console.
TAGS
Used to set the tag of the subscribed messages.
6. Release the resources.
// Release resources
consumer->shutdown();
7. After the message is sent, you will receive a message ID (messageID). Developers can query the recently sent messages on the Message Query page, as shown in the following figure. Information such as details and traces for specific messages is also available. For details, see Message Query.

Note:
The preceding sections briefly describe how to publish and subscribe to messages. For more operations, see the Demo or the RocketMQ-Client-CPP Examples.


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

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