All Products
Search
Document Center

Message Service:BatchSendMessage

Last Updated:Sep 28, 2023

Sends multiple messages to a queue at a time.

Authorization information

By default, only Alibaba Cloud accounts can call this operation. RAM users can call this operation only after the RAM users are granted the related permissions. The following table describes the authorization information of this operation. For more information, see Permission policies and examples.

Name

Value

API

BatchSendMessage

Action

mns:BatchSendMessage

Resource

acs:mns:$region:$accountid:/queues/$queueName/messages

Usage notes

  • You can call this operation to send a maximum of 16 messages to a queue at a time. The maximum size of messages that can be sent at a time is 64 KB. The maximum size of messages that can be sent at a time is 64 KB. If you do not specify the DelaySeconds parameter when you send a message to the queue, the message is immediately consumed. If you want the message to be consumed on a regular basis, you can specify the DelaySeconds parameter when you send the message to the queue.

  • If you set the DelaySeconds parameter to a value that is greater than 0, the message is in the Delayed state after the message is sent to the queue. The message cannot be consumed until the period that is specified by the DelaySeconds parameter ends and the status of the message changes to Active. If the value of the DelaySeconds parameter for the message and queue is different from the value of the DelaySeconds parameter for the queue, the value of the DelaySeconds parameter for the message takes effect.

  • When you call the BatchSendMessage operation, equals to the value generated by dividing the QPS limit by the number of messages. Assume that you want to call the BatchSendMessage operation to send 100 messages and the QPS limit of a single queue is 3000. The maximum number of concurrent requests per second is calculated based on the following formula: 3000/100 = 30.

  • Before you call this API operation to send a message, encode the message body by using Base64. This prevents unexpected errors due to special characters.

Request

A request consists of the following parts:

  • Request line

    POST /queues/$queueName/messages HTTP/1.1

  • Operation-specific request headers

    None.

  • Request Body

    The request body is in the XML format. The parameters of multiple messages are included in the request body.

    Parameter

    Type

    Required

    Example

    Description

    MessageBody

    String

    Yes

    This is test message 1.

    The body of the message.

    DelaySeconds

    Integer

    No

    60

    The delay period after which the message sent to the queue can be consumed.

    Valid values: 0 to 604800. Unit: seconds.

    Default value: 0.

    Priority

    Integer

    No

    1

    The priority of the message. A message with a higher priority is consumed earlier.

    Valid values: 1 to 16. A smaller value indicates a higher priority.

    Default value: 8.

Response

A response consists of the following parts:

  • HTTP Status Code

    HTTP/1.1 201 Created

  • Operation-specific response headers

    None.

  • Response Body

    The response body is in the XML format. The MessageId and MessageBodyMD5 parameters of multiple messages are returned.

    Parameter

    Type

    Example

    Description

    MessageId

    String

    5F290C926D472878-2-14D9529A8FA-20000****

    The ID of the message. The message ID is unique in the queue.

    MessageBodyMD5

    String

    C5DD56A39F5F7BB8B3337C6D11B6****

    The MD5 hash of the message body.

Examples

Sample requests

POST /queues/$queueName/messages HTTP/1.1
Host: $AccountId.mns.cn-hangzhou.aliyuncs.com
Date: Wed, 18 Mar 2012 12:00:00 GMT
Content-Length:500
Content-Type:text/xml;charset=utf-8
x-mns-version: 2015-06-06
Authorization: MNS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEXA****

<?xml version="1.0" encoding="UTF-8"?>
<Messages xmlns="http://mns.aliyuncs.com/doc/v1/">
    <Message>
        <MessageBody>This is test message 1.</MessageBody>
        <DelaySeconds>60</DelaySeconds>
        <Priority>1</Priority>
    </Message>
    <Message>
        <MessageBody>This is test message 2.</MessageBody>
        <DelaySeconds>60</DelaySeconds>
        <Priority>1</Priority>
    </Message>
</Messages>           

Sample responses

  • All messages are sent:

        HTTP/1.1 201 Created
        Content-Length:120
        Content-Type:text/xml;charset=utf-8
        x-mns-request-id:512B2A634403E52B1956****
        x-mns-version: 2015-06-06
    
        <?xml version="1.0" encoding="UTF-8"?>
        <Messages xmlns="http://mns.aliyuncs.com/doc/v1/">
        <Message>
            <MessageId>5F290C926D472878-2-14D9529A8FA-20000****</MessageId>
            <MessageBodyMD5>C5DD56A39F5F7BB8B3337C6D11B6****</MessageBodyMD5>
        </Message>
        <Message>
            <MessageId>5F290C926D472878-2-14D9529A8FA-20000****</MessageId>
            <MessageBodyMD5>377AC5283D8765C9CEE4E0EA353D****</MessageBodyMD5>
        </Message>
        </Messages>                   
  • Some messages fail to be sent:

        HTTP/1.1 500
        Content-Length:120
        Content-Type:text/xml;charset=utf-8
        x-mns-request-id:512B2A634403E52B1956****
        x-mns-version: 2015-06-06
    
        <?xml version="1.0" encoding="UTF-8"?>
        <Messages xmlns="http://mns.aliyuncs.com/doc/v1/">
        <Message>
            <MessageId>5F290C926D472878-2-14D9529A8FA-20000****</MessageId>
            <MessageBodyMD5>C5DD56A39F5F7BB8B3337C6D11B6****</MessageBodyMD5>
        </Message>
        <Message>
            <MessageId>5F290C926D472878-2-14D9529A8FA-20000****</MessageId>
            <MessageBodyMD5>377AC5283D8765C9CEE4E0EA353D****</MessageBodyMD5>
        </Message>
        <Message>
            <ErrorCode>InternalError</ErrorCode>
            <ErrorMessage>Internal error.</ErrorMessage>
        </Message>
        </Messages>              

Error codes

Error codeError messageThe HTTP status codeDescription
QueueNotExistThe queue name you provided is not exist.404The error message returned because the specified queue does not exist. Create a queue.
MalformedXMLThe XML you provided was not well-formed.400The error message returned because the format of the XML file is invalid.
InvalidArgumentThe value of Element should between Low and High seconds/bytes.400The error message returned because the value of the parameter is invalid. You must specify a valid value as prompted.