tencent cloud

Feedback

SDK for Python

Last updated: 2022-09-21 18:25:05

    Overview

    This document describes how to use open-source SDK to send and receive messages by using the SDK for Python as an example and helps you better understand the message sending and receiving processes.

    Prerequisites

    Directions

    Step 1. Prepare the environment

    As RocketMQ-client Python is lightweight wrapper around rocketmq-client-cpp, you need to install librocketmq first.

    1. Install librocketmq 2.0.0 or later as instructed at GitHub.
    2. Run the following command to install rocketmq-client-python.
      pip install rocketmq-client-python

    Step 2. Produce messages

    Create, compile, and run a message production program.

    from rocketmq.client import Producer, Message

    # Initialize the producer and set the producer group information. Be sure to use the full name of the group, such as `rocketmq-xxx|namespace_python%group1`.
    producer = Producer(groupName)
    # Set the service address
    producer.set_name_server_address(nameserver)
    # Set permissions (role name and token)
    producer.set_session_credentials(
    accessKey, # Role token
    secretKey, # Role name
    ''
    )
    # Start the producer
    producer.start()

    # Assemble messages. The topic name must be a full name concatenated by the full namespace name and the topic name, such as "rocketmq-xxx|namespace_python%topic1".
    msg = Message(topicName)
    # Set keys
    msg.set_keys(TAGS)
    # Set tags
    msg.set_tags(KEYS)
    # Message content
    msg.set_body('This is a new message.')

    # Send messages in sync mode
    ret = producer.send_sync(msg)
    print(ret.status, ret.msg_id, ret.offset)
    # Release resources
    producer.shutdown()
    Parameter Description
    groupName Producer group name, which can be obtained under the Group tab on the Cluster page in the console.
    nameserver Cluster access address, which can be copied from Access Address in the Operation column on the Cluster page in the console. Namespace access addresses in new shared or exclusive clusters can be copied from the Namespace list.
    secretKey Role name, which can be copied on the Role Management page.
    accessKey Role token, which can be copied in the Token column on the Role Management page. img
    topicName topicName is in the format of namespace name+%+topic name.
  • The namespace name can be copied under the Topic tab on the Cluster page in the console, which is in the format of cluster ID + | +namespace.
  • TAGS A parameter used to set the message tag.
    KEYS A parameter used to set the message key.

    Step 3. Consume messages

    Create, compile, and run a message consumption program.

    import time

    from rocketmq.client import PushConsumer, ConsumeStatus


    # Message processing callback
    def callback(msg):
    # Simulate the business processing logic
    print('Received message. messageId: ', msg.id, ' body: ', msg.body)
    # Return CONSUME_SUCCESS if the consumption is successful
    return ConsumeStatus.CONSUME_SUCCESS
    # Return the consumption status if the consumption is successful
    # return ConsumeStatus.RECONSUME_LATER


    # Initialize the consumer and set the consumer group information (consumer group information refers to the full namespace name concatenated by the group name, such as "rocketmq-xxx|namespace_python%group11")
    consumer = PushConsumer(groupName)
    # Set the service address
    consumer.set_name_server_address(nameserver)
    # Set permissions (role name and token)
    consumer.set_session_credentials(
    accessKey, # Role token
    secretKey, # Role name
    ''
    )
    # Subscribe to a topic
    consumer.subscribe(topicName, callback, TAGS)
    print(' [Consumer] Waiting for messages.')
    # Start the consumer
    consumer.start()

    while True:
    time.sleep(3600)
    # Release resources
    consumer.shutdown()
    Parameter Description
    groupName The consumer group information refers to the full namespace name concatenated by the group name, such as rocketmq-xxx|namespace_python%group11. The namespace name and group name can be obtained respectively under the Namespace and Group tab in the console.
    nameserver Cluster access address, which can be copied from Access Address in the Operation column on the Cluster page in the console. Namespace access addresses in new shared or exclusive clusters can be copied from the Namespace list.
    secretKey Role name, which can be copied on the Role Management page.
    accessKey Role token, which can be copied in the Token column on the Role Management page. img
    topicName topicName is in the format of namespace name+%+topic name.
  • The namespace name can be copied under the Topic tab on the Cluster page in the console, which is in the format of cluster ID + | + namespace.
  • The topic name can be copied under the Topic tab on the Cluster page in the console.
  • TAGS A parameter used to set the tag of messages that are subscribed to. The default value is set to "*", which means subscribing to all messages.

    Step 4. View consumption details

    Log in to the TDMQ console, go to the Cluster > Group page, and view the list of clients connected to the group. Click View Details in the Operation column to view consumer details.
    img

    Note:

    Above is a brief introduction to message publishing and subscription. For more information, see the demo or RocketMQ-Client-Python Samples.

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support