// Authentication informationCredential cred = Credential(SECRET_ID, SECRET_KEY);HttpProfile httpProfile = HttpProfile();httpProfile.SetEndpoint(ENDPOINT);ClientProfile clientProfile = ClientProfile();clientProfile.SetHttpProfile(httpProfile);// Create the TDMQ clientTdmqClient client = TdmqClient(cred, REGION, clientProfile);
Parameter | Description |
SECRET_ID and SECRET_KEY | |
ENDPOINT | API request domain name: tdmq.tencentcloudapi.com. |
REGION | Cluster region. |
SendMessagesRequest req = SendMessagesRequest();// Set the authorized role tokenreq.SetStringToken(token);// Set the authorized role namereq.SetProducerName(userName);// Set the topic name in the format of cluster (tenant) ID/namespace/topic namereq.SetTopic(topicName);// Message contentreq.SetPayload("this is a new message.");// Set the message sending timeout periodreq.SetSendTimeout(3000);// Send the messageauto outcome = client.SendMessages(req);if (!outcome.IsSuccess()) {cout << outcome.GetError().PrintAll() << endl;return -1;}// Get the resultSendMessagesResponse resp = outcome.GetResult();
Parameter | Description |
token | |
userName | Role name, which can be copied on the Role Management page. There is no need to specify ProducerName . If your business requires it, make sure that the userName is globally unique; otherwise, message sending will fail. |
topicName | Topic name in the format of cluster (tenant) ID/namespace/topic name, such as pulsar-xxx/sdk_http/topic1 . You can directly copy it on the Topic page in the console. |
ReceiveMessageRequest req = ReceiveMessageRequest();// Set the topic name in the format of cluster (tenant) ID/namespace/topic namereq.SetTopic(topicName);// Set the subscription namereq.SetSubscriptionName(subName);// Messages received by the consumer will first be stored in the `receiverQueueSize` queue to tune the message receiving rate.req.SetReceiverQueueSize(10);// It is used to determine the position where the consumer initially receives messages. Valid values: `Earliest`, `Latest`.req.SetSubInitialPosition("Latest");// Receive the messageauto outcome = client.ReceiveMessage(req);if (!outcome.IsSuccess()) {cout << outcome.GetError().PrintAll() << endl;return -1;}// Get the resultReceiveMessageResponse resp = outcome.GetResult();
Parameter | Description |
topicName | Topic name in the format of cluster (tenant) ID/namespace/topic name, such as pulsar-xxx/sdk_http/topic1 . You can directly copy it on the Topic page in the console. |
subName | Subscription name, which can be copied on the Cluster > Consumer tab in the console. |
AcknowledgeMessageRequest req = AcknowledgeMessageRequest();// ID of the acknowledged messagereq.SetMessageId(messageId);// Set the topic name in the format of cluster (tenant) ID/namespace/topic namereq.SetAckTopic(topicName);// Set the message subscriptionreq.SetSubName(subName);// Acknowledge the messageauto outcome = client.AcknowledgeMessage(req);if (!outcome.IsSuccess()) {cout << outcome.GetError().PrintAll() << endl;return -1;}// Get the resultAcknowledgeMessageResponse resp = outcome.GetResult();
Parameter | Description |
messageId | Message ID obtained after message consumption. |
topicName | Topic name in the format of cluster (tenant) ID/namespace/topic name, such as pulsar-xxx/sdk_http/topic1 . You can directly copy it on the Topic page in the console. |
subName | Subscription name, which can be copied on the Cluster > Consumer tab in the console. |
Was this page helpful?