// Set the producer group nameDefaultMQProducer producer(groupName);// Set the service access addressproducer.setNamesrvAddr(nameserver);// Set user permissionsproducer.setSessionCredentials(accessKey, // Role keysecretKey, // Role name"");// Set the namespace (full namespace name)producer.setNameSpace(namespace);// Ensure that the parameters are set before initiationproducer.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. |
// Initialize message contentMQMessage msg(topicName, // Topic nameTAGS, // Message tagKEYS, // Business 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 from the Topic tab on the console. |
TAGS | Used to set the message tag. |
KEYS | Used to configure the message business key. |
// Release resourcesproducer.shutdown();
// Monitor 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` when consumption is successfulreturn CONSUME_SUCCESS;// Return `RECONSUME_LATER` when consumption fails. The message will be re-consumed// 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");// Please register the custom listening function to process the received messages and return the corresponding processing results.ExampleMessageListener *messageListener = new ExampleMessageListener();// Subscribe to the messageconsumer->subscribe(topicName, TAGS);// Set the message listenerconsumer->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. |
// Release resourcesconsumer->shutdown();
Was this page helpful?