Release Notes
Announcements
pip install rocketmq-python-client==5.0.6
Operating System: Ubuntu 24.04.2 LTS / x86_64
Runtime Version: GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
from rocketmq import ClientConfiguration, Credentials, Message, Producerif __name__ == '__main__':endpoints = "rmq-xxxx.rocketmq.gz.qcloud.tencenttdmq.com:8080"credentials = Credentials()# if auth enablecredentials = Credentials("ak", "sk")config = ClientConfiguration(endpoints, credentials)topic = "topic"producer = Producer(config, (topic,))try:producer.startup()try:msg = Message()# topic for the current messagemsg.topic = topicmsg.body = "hello, rocketmq.".encode('utf-8')# secondary classifier of message besides topicmsg.tag = "rocketmq-send-message"# key(s) of the message, another way to mark message besides message idmsg.keys = "send_sync"# user property for the messagemsg.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()
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. |
from rocketmq import ClientConfiguration, Credentials, SimpleConsumerif __name__ == '__main__':endpoints = "rmq-xxxx.rocketmq.gz.qcloud.tencenttdmq.com:8080"# if auth enablecredentials = 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 anymoresimple_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 receivedmessages = 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()
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. |
Apakah halaman ini membantu?
Anda juga dapat Menghubungi Penjualan atau Mengirimkan Tiket untuk meminta bantuan.
masukan