tencent cloud

TDMQ for RocketMQ

Release Notes and Announcements
Release Notes
Announcements
Product Introduction
Introduction and Selection of the TDMQ Product Series
What Is TDMQ for RocketMQ
Strengths
Scenarios
Product Series
Comparison with Open-Source RocketMQ
High Availability
Quotas and Limits
Supported Regions
Basic Concepts
Billing
Billing Overview
Pricing
Billing Examples
Pay-as-you-go Switch to Monthly Subscription (5.x)
Renewal
Viewing Consumption Details
Refund
Overdue Payments
Getting Started
Getting Started Guide
Preparations
Step 1: Creating TDMQ for RocketMQ Resources
Step 2: Using the SDK to Send and Receive Messages (Recommended)
Step 2: Running the TDMQ for RocketMQ Client (Optional)
Step 3: Querying Messages
Step 4: Deleting Resources
User Guide
Usage Process Guide
Configuring Account Permissions
Creating the Cluster
Configuring the Namespace
Configuring the Topic
Configuring the Group
Connecting to the Cluster
Managing Messages
Managing the Cluster
Viewing Monitoring Data and Configuring Alarms
Cross-Cluster Message Replication
Use Cases
Naming Conventions for Common Concepts of TDMQ for RocketMQ
RocketMQ Client Use Cases
RocketMQ Performance Load Testing and Capacity Assessment
Access over HTTP
Client Risk Descriptions and Update Guide
Migration Guide for TencentCloud API Operations Related to RocketMQ 4.x Cluster Roles
Migration Guide
Disruptive Migration
Seamless Migration
Developer Guide
Message Types
Message Filtering
Message Retries
POP Consumption Mode (5.x)
Clustering Consumption and Broadcasting Consumption
Subscription Relationship Consistency
Traffic Throttling
​​API Reference(5.x)
History
API Category
Making API Requests
Topic APIs
Consumer Group APIs
Message APIs
Role Authentication APIs
Hitless Migration APIs
Cloud Migration APIs
Cluster APIs
Data Types
Error Codes
​​API Reference(4.x)
SDK Reference
SDK Overview
5.x SDK
4.x SDK
Security and Compliance
Permission Management
CloudAudit
Deletion Protection
FAQs
4.x Instance FAQs
Agreements
TDMQ for RocketMQ Service Level Agreement
Contact Us

Using the Python SDK

PDF
Mode fokus
Ukuran font
Terakhir diperbarui: 2026-01-23 17:52:24

Scenarios

This document uses the Python software development kit (SDK) as an example to describe how to send and receive messages of the TDMQ for RocketMQ 5.x series through an open-source SDK, helping you better understand the complete process of sending and receiving messages.

Prerequisites

You have obtained the client connection parameters as instructed in SDK Overview.

Operation Steps

Step 1: Preparing the Environment

Ensure that Python 3.7 or later is used. It is recommended Python SDK 5.0.6 be used.
pip install rocketmq-python-client==5.0.6

Running Environment

Operating System: Ubuntu 24.04.2 LTS / x86_64

Runtime Version: GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)

Step 2: Producing Messages

Create, compile, and run a message production program.
from rocketmq import ClientConfiguration, Credentials, Message, Producer

if __name__ == '__main__':
endpoints = "rmq-xxxx.rocketmq.gz.qcloud.tencenttdmq.com:8080"
credentials = Credentials()
# if auth enable
credentials = Credentials("ak", "sk")
config = ClientConfiguration(endpoints, credentials)
topic = "topic"
producer = Producer(config, (topic,))

try:
producer.startup()
try:
msg = Message()
# topic for the current message
msg.topic = topic
msg.body = "hello, rocketmq.".encode('utf-8')
# secondary classifier of message besides topic
msg.tag = "rocketmq-send-message"
# key(s) of the message, another way to mark message besides message id
msg.keys = "send_sync"
# user property for the message
msg.add_property("send", "sync")
res = producer.send(msg)
print(f"{producer.__str__()} send message success. {res}")
producer.shutdown()
print(f"{producer.__str__()} shutdown.")
except Exception as e:
print(f"normal producer example raise exception: {e}")
producer.shutdown()
except Exception as e:
print(f"{producer.__str__()} startup raise exception: {e}")
producer.shutdown()
Note:
You can log in to the TDMQ for RocketMQ console to obtain the following parameters.
Parameter
Description
endpoints
Cluster access address. You can obtain the access address from the Access Information module on the cluster basic information page in the console.
secretKey
Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console.
accessKey
Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console.
topicName
Topic name. You can copy the name from the Topic Management page in the console.
The Python client of the current open-source community has certain defects in producing messages, resulting in unbalanced loads between different queues of the same topic. For details, see Defect Details.

Step 3: Consuming Messages

Create, compile, and run a message consumption program.
from rocketmq import ClientConfiguration, Credentials, SimpleConsumer

if __name__ == '__main__':
endpoints = "rmq-xxxx.rocketmq.gz.qcloud.tencenttdmq.com:8080"
# if auth enable
credentials = Credentials("ak", "sk")
config = ClientConfiguration(endpoints, credentials)
# with namespace
# config = ClientConfiguration(endpoints, credentials, "namespace")
topic = "topic"
# in most case, you don't need to create too many consumers, singleton pattern is recommended
# close the simple consumer when you don't need it anymore
simple_consumer = SimpleConsumer(config, "consumer-group")
try:
simple_consumer.startup()
try:
simple_consumer.subscribe(topic)
# use tag filter
# simple_consumer.subscribe(topic, FilterExpression("tag"))
while True:
try:
# max message num for each long polling and message invisible duration after it is received
messages = simple_consumer.receive(32, 15)
if messages is not None:
print(f"{simple_consumer.__str__()} receive {len(messages)} messages.")
for msg in messages:
simple_consumer.ack(msg)
print(f"{simple_consumer.__str__()} ack message:[{msg.message_id}].")
except Exception as e:
print(f"receive or ack message raise exception: {e}")
except Exception as e:
print(f"{simple_consumer.__str__()} subscribe topic:{topic} raise exception: {e}")
simple_consumer.shutdown()
except Exception as e:
print(f"{simple_consumer.__str__()} startup raise exception: {e}")
simple_consumer.shutdown()
Note:
You can log in to the TDMQ for RocketMQ console to obtain the following parameters.
Parameter
Description
groupName
Consumer group name. You can copy the name from the Group Management page in the console.
endpoints
Cluster access address. You can obtain the access address from the Access Information module on the cluster basic information page in the console.
secretKey
Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console.
accessKey
Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console.
topicName
Topic name. You can copy the name from the Topic Management page in the console.

Step 4: Viewing Message Details

After a message is sent, you will receive a message ID (messageID). You can choose Message Query > General Query in the console to query the recently sent message, including the message details and trace.
Note:
The above is a brief introduction to message publishing and subscription. For details, see Demo or RocketMQ-Clients-Python example.

Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan