tencent cloud

文档反馈

Usage Examples

最后更新时间:2024-12-18 17:35:00
    Note:
    This is a legacy API which has been hidden and will no longer be updated. We recommend using the new CMQ API 3.0 which is standardized and faster.
    In order to help users quickly use the cloud messaging service (CMQ) API, a Use Case is given here, which can guide users to create queues, send messages, consume messages, delete messages and delete queues using API.
    The parameters in the following example are for reference only and are actually based on the values returned by the system. For ease of illustration, operations on messages take a single operation as an example, batch interfaces such as Send messages in batchBulk delete messages Etc.) is also available.
    Please refer to the following instructions to replace {$region} in the domain name with the corresponding region:
    Domain name requested by public network API: https://cmq-queue-{$region}.api.qcloud.com
    Domain name requested by private network API: http://cmq-queue-{$region}.api.tencentyun.com
    {$region} need to replace: gz (Guangzhou), sh (Shanghai), bj (Beijing), shjr (Shanghai Finance), szjr (Shenzhen Finance), hk (Hong Kong), cd (Chengdu), ca (North American), usw (Maxi), sg (Singapore with a specific region. The region value in the common parameters should be consistent with the region value of the domain name. If there is any inconsistency, the request will be sent to the region specified by the domain name region based on the region value of the domain name.

    Queue model

    Take private network as an example (when operating in a public network, you need to replace the tencentyun in the domain name with the composition rules of the domain name requested by qcloud), as shown in cmq-queue-{$region}.api.tencentyun.com/v2/index.php The {$region} field needs to be replaced by a specific region:: gz (Guangzhou), sh (Shanghai), bj (Beijing), shjr (Shanghai Finance), szjr (Shenzhen Finance), hk (Hong Kong), cd (Chengdu), ca (North American), usw (Maxi), sg (Singapore). Common Parameters The region value in should be consistent with the region value of the domain name. If there is any inconsistency, the request will be sent to the region specified by the domain name region based on the region value of the domain name. For more information on the generation rules of Signature parameters, please see Signature Method .

    Create a queue

    Before you create a queue, please refer to the Create a queue interface Description: adjust the attribute value of the queue according to your own service. If we create a queue in Guangzhou and expect business messages to be faster than production, the specific request parameters required are shown in the following table:
    Parameter name
    Description
    Sample value
    QueueName
    The queue name must be unique under the same account in a single region. The queue name is a string of no more than 64 characters, must begin with a letter, and the rest can contain letters, numbers, and dashes (-).
    Test-queue-1
    PollingWaitSeconds
    Long-polling Waiting Time for Message Receipt . The value range is 0-30 seconds, and the default value is 0.
    thirty
    To sum up, combined with common request parameters and API request parameters, the final request form is as follows:
    http://cmq-queue-gz.api.tencentyun.com/v2/index.php?
    Action=CreateQueue
    &Region=gz
    &Timestamp=1465750149
    &Nonce=46364
    &SecretId=AKIDxxxxugEY
    &Signature=5umi9gUWpTTyk18V2g%2FYi56hqls%3D
    &queueName=test-queue-1
    &pollingWaitSeconds=30
    Returned result:
    {
    "code" : 0,
    "message" : "",
    "requestId":"14534664555",
    "queueId":"queue-ajksdfasdowe"
    }
    At this point, we have created a queue that can manage queues and send consumer messages.

    Message sending

    Please refer to the Message sending interface Description. If we create the example above test-queue-1 Send a message in the queue. The specific request parameters required are shown in the following table:
    Parameter name
    Description
    Sample value
    QueueName
    The queue name must be unique under the same account in a single region. The queue name is a string of no more than 64 characters, must begin with a letter, and the rest can contain letters, numbers, and dashes (-).
    Test-queue-1
    MsgBody
    Message body. The size is at least 1Byte, and the maximum length is limited by the set queue message maximum length attribute.
    After This'is test message (url coding, it becomes This%27is+test+message )
    Request:
    http://cmq-queue-gz.api.tencentyun.com/v2/index.php?
    Action=SendMessage
    &Region=gz
    &Timestamp=1465750149
    &Nonce=46365
    &SecretId=AKIDxxxxugEY
    &Signature=5umi9gUWagTTyk18V2g%2FYi56hqls%3D
    &queueName=test-queue-1
    &msgBody=This%27is+test+message
    Returned result:
    {
    "code" : 0,
    "message" : "",
    "requestId":"145346456555",
    "msgId":"123345346"
    }

    Consumption message

    Please refer to the message carefully before consuming the message. Consumer message interface Description. According to the characteristics of the business, you can choose to specify the value of pollingWaitSeconds. If we create from the example above test-queue-1 Messages are consumed in the queue. The specific request parameters required are shown in the following table:
    Parameter name
    Description
    Sample value
    QueueName
    The queue name must be unique under the same account in a single region. The queue name is a string of no more than 64 characters, must begin with a letter, and the rest can contain letters, numbers, and dashes (-).
    Test-queue-1
    PollingWaitSeconds
    The long polling waiting time for this request. The value range is 0-30 seconds, and the default value is 0.
    10
    Request:
    http://cmq-queue-gz.api.tencentyun.com/v2/index.php?
    Action=ReceiveMessage
    &Region=gz
    &Timestamp=1465750150
    &Nonce=46368
    &SecretId=AKIDxxxxugEY
    &Signature=5umi9gUaagTTyk18V2g%2FYi56hqls%3D
    &queueName=test-queue-1
    &pollingWaitSeconds=10
    Returned result:
    {
    "code" : 0,
    "message" : "",
    "requestId":"145346635355",
    "msgBody":"This is test message",
    "msgId":"123345346",
    "receiptHandle": "283748239349283",
    "enqueueTime": 1462351990,
    "firstDequeueTime": 1462352990,
    "nextVisibleTime": 1462352999,
    "dequeueCount": 1
    }

    Delete message

    In general, messages should be deleted once consumed, unless the business has a need for repeated consumption. Please read carefully before deleting the message Delete message interface Description. For example, if we spend test-queue-1 A message in the queue and delete it after consumption. Be sure to delete before the time of nextVisibleTime Otherwise, the receiptHandle will become invalid, causing the deletion to fail. The specific request parameters required are shown in the following table:
    Parameter name
    Description
    Sample value
    QueueName
    The queue name must be unique under the same account in a single region. The queue name is a string of no more than 64 characters, must begin with a letter, and the rest can contain letters, numbers, and dashes (-).
    Test-queue-1
    ReceiptHandle
    Each consumption returns a unique Message Handler, which is used to delete messages. The handle generated when and only when the message was last consumed can be used to delete this message.
    "283748239349283" (receiptHandle in the example above)
    Request:
    http://cmq-queue-gz.api.tencentyun.com/v2/index.php?
    Action=DeleteMessage
    &Region=gz
    &Timestamp=1465750151
    &Nonce=46369
    &SecretId=AKIDxxxxugEY
    &Signature=5umi9gUaagTasdfk18V2g%2FYi56hqls%3D
    &queueName=test-queue-1
    &receiptHandle=283748239349283
    Returned result:
    {
    "code" : 0,
    "message" : "",
    "requestId":"14534454555"
    }

    Delete queue

    Before deleting the queue, please refer to the Delete queue interface Description. When the queue is no longer in use, it needs to be deleted. The specific request parameters required are shown in the following table:
    Parameter name
    Description
    Value
    QueueName
    The queue name must be unique under the same account in a single region. The queue name is a string of no more than 64 characters, the first character must be a letter, and the rest can contain letters, numbers, and dashes (-).
    Test-queue-1
    Request:
    http://cmq-queue-gz.api.tencentyun.com/v2/index.php?
    Action=DeleteQueue
    &Region=gz
    &Timestamp=1465750152
    &Nonce=46370
    &SecretId=AKIDxxxxugEY
    &Signature=5umi9gUaagTasasdl18V2g%2FYi56hqls%3D
    &queueName=test-queue-1
    Returned result:
    {
    "code" : 0,
    "message" : "",
    "requestId":"14534454555"
    }
    联系我们

    联系我们,为您的业务提供专属服务。

    技术支持

    如果你想寻求进一步的帮助,通过工单与我们进行联络。我们提供7x24的工单服务。

    7x24 电话支持