tencent cloud

Feedback

Getting Started with Topic Model

Last updated: 2024-01-03 10:15:44

    Overview

    This document describes how to create a topic and use the SDK for Java to test message sending and receiving, so as to help you quickly understand the basic operations required for client access to TDMQ for CMQ.

    Prerequisite

    Directions

    Step 1. Create a topic

    1. Log in to the TDMQ for CMQ console.
    2. Select Topic Subscription on the left sidebar, select a region, click Create, and enter the topic name.
    
    
    
    Topic Name: It can contain up to 64 letters, digits, "-", or "_", and must start with a letter. It cannot be modified once created.
    Message Heap: If this option is enabled, messages that failed to be pushed to or received by the subscriber will be temporarily heaped in the topic.
    Message Filter Type:
    Tag: TDMQ for CMQ can match message tags for production and subscription, which can be used for message filtering. For detailed rules, see Tag Key Matching Feature Description.
    Routing matching: The binding key and routing key are used together and are fully compatible with the topic match mode of RabbitMQ. The routing key carried when a message is sent is added by the client, and the binding key carried when a subscription is created is the binding relationship between the topic and the subscriber. For detailed rules, see Routing Key Matching Feature Description.
    Resource Tag: It is optional and can help you easily categorize and manage TDMQ for CMQ resources in many dimensions. For detailed usage, see Managing Resource with Tag.
    3. Click Submit, and you can see the created topic in the topic subscription list.

    Step 2. Create a subscription

    A topic can publish messages only if it is subscribed to by at least one subscriber. If there are no subscribers, messages in the topic will not be delivered, and message publishing will be meaningless.
    1. On the Topic Subscription page, click the ID of the topic you just created to enter the topic details page.
    2. Select the Subscriber tab at the top, click Create, and enter the subscriber information.
    
    
    
    Subscriber Type
    Queue service: you can select a queue for the subscriber to receive published messages.
    URL: Subscribers can process messages on their own without using queues. For more information, see Delivering Message.
    Subscriber Tag: When adding a subscriber, you can add filter tags (FilterTag), so that the subscriber can receive only messages with the specified tags. Up to five tags can be added for one subscriber. As long as a tag matches a topic filter tag, the subscriber can receive messages delivered by the topic. If a message does not have any tag, the subscriber cannot receive it.
    Tag: For detailed rules, see Tag Key Matching Feature Description.
    Routing matching: For detailed rules, see Routing Key Matching Feature Description.
    Retry Policy: After a message is published by a topic, it will automatically be pushed to the subscription. If the push fails, there are two retry policies:
    Backoff retry: An attempt will be retried three times at random intervals between 10 and 20 seconds. After three retries, the message will be discarded for the subscriber and will not be retried again.
    Exponential decay retry: An attempt will be retried 176 times at exponentially increasing intervals: 2^0 second, 2^1 seconds, ..., 512 seconds, 512 seconds, ..., 512 seconds. The total retry duration is 1 day. This is the default retry policy.
    3. Click Submit, and you can see the created subscriber in the subscriber list.

    Step 3. Use the SDK to send and receive messages

    Note:
    The following takes Java as an example. For clients in other languages, see the SDK Documentation.
    1. Download the demo and decompress it.
    2. Import CMQ client dependencies.
    <!-- cmq sdk -->
    <dependency>
    <groupId>com.qcloud</groupId>
    <artifactId>cmq-http-client</artifactId>
    <version>1.0.7</version>
    </dependency>
    
    <!-- TencentCloud API SDK -->
    <dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java</artifactId>
    <version>3.1.423</version>
    </dependency>
    
    
    3. Create a topic object.
    Account account = new Account(SERVER_ENDPOINT, SECRET_ID, SECRET_KEY);
    Topic topic = account.getTopic(topicName);
    
    Parameter
    Description
    SERVER_ENDPOINT
    API call address, which can be copied from Topic Subscription > API Request Address in the TDMQ for CMQ console.
    
    
    
    SECRET_ID, SECRET_KEY
    TencentCloud API key, which can be copied on the Access Key > Manage API Key page in the CAM console.
    
    
    
    topicName
    Topic subscription name, which can be obtained on the Topic Subscription page in the TDMQ for CMQ console.
    4. Send tag messages.
    String msg = "hello client, this is a message. tag=TAG1. Time:" + new Date();
    List<String> tags = Collections.singletonList("TAG1");
    String messageId = topic.publishMessage(msg, tags, null);
    
    5. Send route messages.
    String msg = "hello client, this is a message. route(abc) Time:" + new Date();
    String messageId = topic.publishMessage(msg, "abc");
    
    6. Consume messages in the queue corresponding to the subscriber.
    Account account = new Account(SERVER_ENDPOINT, SECRET_ID, SECRET_KEY);
    Queue queue = account.getQueue(queueName);
    Message message = queue.receiveMessage();
    // Successfully consumed messages are deleted. Retained messages can be delivered again after a certain period of time
    queue.deleteMessage(message.receiptHandle);
    Note:
    The above is a brief introduction to the message production and consumption in TDMQ for CMQ. For more information, see Demo.
    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