// Set the producer group nameDefaultMQProducer producer(groupName);// Set the service access addressproducer.setNamesrvAddr(nameserver);// Set user permissionsproducer.setSessionCredentials(accessKey, // Role tokensecretKey, // Role name"");// Set the full namespace nameproducer.setNameSpace(namespace);// Make sure all parameters are configured before the startproducer.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 virtual or exclusive clusters can be copied from the Namespace list. |
secretKey | |
accessKey | |
namespace | Full namespace name, which can be copied under the Topic tab on the Cluster page in the console and is in the format of cluster ID + | + namespace. |
// Initialize the message contentMQMessage msg(topicName, // Topic nameTAGS, // Message tagKEYS, // Message key"Hello cpp client, this is a message." // Message content);try {// Send the messageSendResult 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. |
// Release the resourcesproducer.shutdown();
// Listen on messagesclass ExampleMessageListener : public MessageListenerConcurrently {public:ConsumeStatus consumeMessage(const std::vector<MQMessageExt> &msgs) {for (auto item = msgs.begin(); item != msgs.end(); item++) {// Businessstd::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 successfulreturn CONSUME_SUCCESS;// Return RECONSUME_LATER if the consumption failed. The message will be consumed again.// return RECONSUME_LATER;}};// Initialize the consumerDefaultMQPushConsumer *consumer = new DefaultMQPushConsumer(groupName);// Set the service addressconsumer->setNamesrvAddr(nameserver);// Set user permissionsconsumer->setSessionCredentials(accessKey,secretKey,"");// Set the namespaceconsumer->setNameSpace(namespace);// Set the instance nameconsumer->setInstanceName("CppClient");// Register a custom listener function to process the received messages and return the processing resultsExampleMessageListener *messageListener = new ExampleMessageListener();// Subscribe to the messageconsumer->subscribe(topicName, TAGS);// Set the message listenerconsumer->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 virtual or exclusive clusters can be copied from the Namespace list. |
secretKey | |
accessKey | |
namespace | Full namespace name, which can be copied under the Topic tab on the Cluster page in the console and 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. |
// Release the resourcesconsumer->shutdown();
Was this page helpful?