tencent cloud

文档反馈

Batch publish messages

最后更新时间:2024-12-18 17:45:20

    1. API Description

    This API (BatchPublishMessage) is used to publish a batch of messages (a maximum of 16 currently) to a specified topic.

    Domain for public network API request: cmq-topic-region.api.qcloud.com

    Domain for private network API request: cmq-topic-region.api.tencentyun.com

    Whenever (including during alpha test) any public network downstream traffic is generated from the use of a public network domain, a fee will be charged. It is strongly recommended that users whose services are on the Tencent Cloud use private network domains, because no fee will be charged for the traffic consumed in the private network.

    • region should be replaced with a specific region: gz (Guangzhou), sh (Shanghai), or bj (Beijing). The region value in the common parameters should be consistent with the region value of the domain. If there is an inconsistency, the request will be sent to the region specified by the domain.
    • Public network domain requests both support http and https. Private network requests only support http.
    • Some of the input parameters are optional. If not specified, the default value will be taken.
    • All the output parameters will be returned to the user when the request is successful; otherwise, at least code, message, and requestId will be returned.

    2. Input Parameters

    The following request parameter list only provides API request parameters. For other parameters, refer to Common Request Parameters.

    Parameter Name Required Type Description
    topicName Yes String Topic name. It is unique under the same account in one region. The topic name is a string of no more than 64 characters, which can contain letters, numbers, and hyphens (-) and must begin with a letter.
    msgBody.n Yes String Message body. One of the messages in the batch. Currently, there can be up to 16 messages in one batch.

    To make it more convenient for users, "n" may start from either 0 or 1, but must be consecutive. For example, if you send two messages, they can be (msgBody.0, msgBody.1) or (msgBody.1, msgBody.2).

    Note: Currently, the total size of all messages is limited to 64 k (only the size of msgBody, and message header and other parameters are not included), so we recommend that you determine the number of messages to be sent in the batch beforehand.
    msgTag.n No String Tag for message filtering. Message tag (used to filter messages). The maximum number of tags is 5, with each containing not more than 16 characters. It is used in combination with the filterTag parameter of API Subscribe, and the rules are: 1) if filterTag is not configured, subscribers can receive all the messages published on Topic no matter whether msgTag is configured; 2) if filterTag array is specified, subscribers can receive the messages published on Topic only when at least one value in filterTag array also exists in the msgTag array (that is, there is an intersection between filterTag and msgTag); 3) if filterTag array is specified, but msgTag is not configured, subscribers cannot receive any messages published on Topic. 3) can be considered as a special case of 2), and there is no intersection between filterTag and msgTag in this case. The rules are designed to allow subscribers to choose at their own discretion.
    routingKey No String This field indicates the routing path for sending messages. Its length should be not more than 64 bytes and contain up to 15 ".", i.e. 16 phrases at most.
    The "routingKey" cannot be specified randomly when messages are sent to the "exchange" of topic type. The above format requirement must be met. A message with routingKey specified by a subscriber will be pushed to all the consumers whose BindingKey matches with routingKey. There are two matching options:
    1 * (asterisk) can be a substitute for a word (a sequence of alphabetic string);
    2 # (pound sign) can be used to match one or more characters.

    3. Output Parameters

    Parameter Name Type Description
    code Int 0: Succeeded, others: Error. See the table below for specific errors.
    message String Error message.
    requestId String Request ID generated by server. When there is an internal error on the server, users can submit this ID to the backend to locate the problem.
    msgList Array List of unique IDs of the messages generated by the server. Each element indicates the information of one message.

    msgList is defined as follows:

    Parameter Name Type Description
    msgId String Unique ID of the message generated by the server.
    Error Code Module Error Code Error Message Description
    4000 10490 number of filterTag exceed limit The number of filterTag exceeds the limit. Currently, the maximum is 5.
    6030 10730 no bindingKey or filterTag matches the routingKey or msgTag No subscription matches the message tag or routingKey for this publication. Therefore, the message published this time will not be delivered to subscribers.
    6030 10650 topic has no subscription, please create a subscription before publishing message There is no subscriber under this topic. Please create a subscriber before publishing a message.
    4000 10700 parameters lack of routingKey The routingKey parameter is missing.
    4000 10720 too many msgTag There are too many msgTag.

    Note: The error codes listed in the above table are specific to this API. If the error code you are looking for is not in the table, you may find it in the Common Error Codes.

    4. Example

    Input:

     https://domain/v2/index.php?Action=BatchPublishMessage
     &topicName=test-topic-123
     &msgBody.1=helloworld1
     &msgBody.2=helloworld2
     &<Common request parameters>
    

    Output:

    {
    "code" : 0,
    "message" : "",
    "requestId":"14534664555",
    "msgList":
      [
        {
          "msgId":"123345346"
        },
        {
           "msgId":"456436346"
        }
      ]
    }