tencent cloud

Feedback

Getting Started with Queue Model

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

    Overview

    This document describes how to create a queue service from scratch and use the SDK for Java to test message sending and receiving. It helps you quickly understand the basic operations required for client access to TDMQ for CMQ.

    Prerequisites

    Directions

    Step 1. Create a queue service

    1. Log in to the TDMQ for CMQ console.
    2. Select Queue Service on the left sidebar, select the region, click Create, and configure the queue service attributes.
    
    
    
    Attribute
    Description
    Value
    Queue Name
    It is the `QueueName` attribute of the queue.
    It is the unique identifier of a resource and can be used to differentiate API calls. It cannot be changed once the queue is created. It is case-insensitive and cannot end with `-retry` or `-dlq`.
    
    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.
    -
    Maximum Message Unacknowledged Time
    If the consumer client fails to acknowledge a received message within this time period, the server will automatically acknowledge the message.
    It ranges from 30 seconds to 12 hours.
    Long Polling Wait Time for Message Receipt
    It is the `PollingWaitSeconds` attribute of the queue. Just like with long polling of Ajax requests, a message consumption request will return a response only after a valid message is fetched or the long-polling time elapses.
    It ranges from 0 to 30 seconds. We recommend you set it to 3 seconds. A higher value may cause more duplicated messages.
    Hidden Duration of Fetched Message
    It is the `VisibilityTimeout` attribute of the queue. Each message has a default `VisibilityTImeout`, which starts counting after a worker receives a message. If the worker fails to complete processing the message within the period specified by this attribute, the message will be sent to and processed by another worker.
    It ranges from 1 second to 12 hours.
    Max Invisible Messages
    If the client fails to acknowledge messages in a timely manner, an excessive number of invisible messages are generated. The generation of invisible messages will consume the memory; therefore, a capacity upper limit is set for each queue.
    It is 100,000. If you need to increase the upper limit, contact technical support.
    Max Capacity for Heaped Messages
    Message heap is generally caused by the production speed being greater than the consumption speed or the consumption being blocked. The heap will use disk space; therefore, a capacity upper limit is set for each queue.
    It is 10 GB. If you need to increase the upper limit, contact technical support.
    Dead Letter Queue
    A dead letter queue is used to handle messages that cannot be consumed successfully after their retry limit has been reached. Rather than being discarded immediately, such messages will be sent to the consumer's specified dead letter queue.
    -
    Message Rewind
    If the message rewind feature is not enabled, a message consumed by a consumer and confirmed for deletion will be deleted immediately. When enabling this feature, you need to specify the rewindable time range.
    The rewindable time range must be equal to or shorter than the message lifecycle. We recommend you make it the same as the message lifecycle to facilitate troubleshooting.
    Rewindable Time Range
    If the message rewind feature is enabled, messages confirmed for deletion by the consumer will not be deleted immediately; instead, they will be stored for the maximum time configured here.
    It ranges from 1 to 15 days. A higher value may cause higher storage fees. The maximum rewindable time point is the current time minus the configured rewindable time range. Messages cannot be rewound if produced before this time.
    Rewindable Storage Space
    After the message rewind feature is enabled, if the volume of persistently stored messages exceeds this maximum storage space, messages will be deleted by time (the oldest data will be deleted first).
    It ranges from 1 to 10 GB. A higher value may cause higher storage fees.
    3. Click Submit, and you can see the created queue service in the queue service list.

    Step 2. Use the SDK to send and receive messages

    Note:
    The following takes Java as an example. For clients in other languages, see API Overview.
    1. Download the demo and decompress it.
    2. Import CMQ client dependencies.
    <!-- cmq sdk --><!-- 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. Send messages.
    Account account = new Account(SERVER_ENDPOINT, SECRET_ID, SECRET_KEY);
    Queue queue = account.getQueue(queueName);
    String msg = "hello client, this is a message. Time:" + new Date();
    CmqResponse response = queue.send(msg);
    
    Parameter
    Description
    SERVER_ENDPOINT
    API call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console.
    
    
    
    SECRET_ID, SECRET_KEY
    TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console.
    
    
    
    queueName
    Queue name, which can be obtained on the Queue Service page in the TDMQ for CMQ console.
    4. Consume messages.
    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);
    
    Parameter
    Description
    SERVER_ENDPOINT
    API call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console.
    
    
    
    SECRET_ID, SECRET_KEY
    TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console.
    
    
    
    queueName
    Queue name, which can be obtained on the Queue Service page in the TDMQ for CMQ console.
    Note:
    Above is a brief introduction to message production and consumption. 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