pip install --upgrade tencentcloud-sdk-python
# Authentication informationcred = credential.Credential(SECRET_ID, SECRET_KEY)httpProfile = HttpProfile()httpProfile.endpoint = ENDPOINTclientProfile = ClientProfile()clientProfile.httpProfile = httpProfile# Create the TDMQ clientclient = tdmq_client.TdmqClient(cred, REGION, clientProfile)
Parameter | Description |
SECRET_ID and SECRET_KEY | |
ENDPOINT | API request domain name: tdmq.tencentcloudapi.com. |
REGION | Cluster region. |
# Message sending request parametersreq = models.SendMessagesRequest()params = {# Authorized role token"StringToken": token,# Topic name in the format of cluster (tenant) ID/namespace/topic name"Topic": topicName,# Message content"Payload": "this is a new message.",# Authorized role name"ProducerName": userName,# Sending timeout period"SendTimeout": 3000,}req.from_json_string(json.dumps(params))# Send the messageresp = client.SendMessages(req)
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. |
# Message receiving request parametersreq = models.ReceiveMessageRequest()params = {# Topic name in the format of cluster (tenant) ID/namespace/topic name"Topic": topicName,# Subscription name"SubscriptionName": subName,# Messages received by the consumer will first be stored in the `receiverQueueSize` queue to tune the message receiving rate."ReceiverQueueSize": 10,# It is used to determine the position where the consumer initially receives messages. Valid values: `Earliest`, `Latest`."SubInitialPosition": "Latest"}req.from_json_string(json.dumps(params))# Receive the messageresp = client.ReceiveMessage(req)
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. |
# Message acknowledging request parametersreq = models.AcknowledgeMessageRequest()params = {# ID of the message to be acknowledged"MessageId": messageId,# Topic name in the format of cluster (tenant) ID/namespace/topic name"AckTopic": topicName,# Subscription name"SubName": subName}req.from_json_string(json.dumps(params))# Acknowledge the messageresp = client.AcknowledgeMessage(req)
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?