Use the BatchReceiveMessage operation to batch consume messages from a queue.
Authorization information
By default, only Alibaba Cloud accounts can call this operation. A RAM user can call this operation only after receiving the necessary permissions. The following table describes the permissions a RAM user needs to call this operation. For more information, see the authorization policy and examples.
Name | Value |
API | BatchReceiveMessage |
RAM authorization operation | mns:BatchReceiveMessage |
Resource | acs:mns:$region:$accountid:/queues/$queueName/messages |
Usage notes
Consumers use this operation to batch consume messages from a queue. A single BatchReceiveMessage operation can retrieve up to 16 messages. This operation changes the status of the retrieved messages to Inactive. The Inactive status duration is determined by the queue's VisibilityTimeout
property. For more information, see CreateQueue.
After successfully consuming messages within the VisibilityTimeout
period, the consumer must call the DeleteMessage operation to delete the retrieved messages. Otherwise, the messages will revert to Active status and may be consumed again.
Request message
The request message consists of four parts: the request line, specific URL parameters, HTTP headers, and the message body:
-
Request Line
GET /queues/$queueName/messages?numOfMessages=16&waitseconds=10 HTTP/1.1
-
Specific URI Parameters
Parameter name
Type
Required
Example value
Description
numOfMessages
Integer
Yes
10
The maximum number of messages that can be retrieved in this BatchReceiveMessage operation.
waitseconds
Integer
No
0
The maximum polling wait time for this ReceiveMessage request, in seconds.
-
Including the
waitseconds
parameter in the BatchReceiveMessage request initiates long polling when the queue is empty. The wait time is defined bywaitseconds
. During this time, if messages arrive in the queue, they will be returned to the user. Ifwaitseconds
is not specified, the queue'sPollingWaitSeconds
property is used by default. For more information, see CreateQueue. -
Upon engaging in long polling, it is advisable to lower the concurrency of external long polling calls and select a suitable long polling period. For more information, see Short polling and long polling. The Simple Message Queue (formerly MNS) server dynamically adjusts the maximum concurrency for long polling to prevent attacks, taking into account the number of long polling requests, wait times, and the number of accessing IPs. When there are no messages in the queue, requests that exceed the concurrency limit for long polling will not be processed and will return a 404 MessageNotExist error (which is billed as per the request volume). If you need to temporarily raise the concurrency limit for long polling, you should submit a ticket promptly to request assistance from customer service and prevent unnecessary charges.
-
Specific Request Header
None.
-
Request Body
None.
Response message
The response message consists of three parts: the response status line, HTTP headers, and the message body:
-
HTTP Status Code
HTTP/1.1 200 OK
-
Specific Response Header
None.
-
Response Body
The response is in XML format and includes multiple message bodies and attributes.
Parameter name
Type
Example value
Description
MessageId
String
5F290C926D472878-2-14D9529A8FA-20000****
The unique message ID in a queue.
ReceiptHandle
String
1-ODU4OTkzNDU5My0xNDMyNzI3ODI3LTItOA==
The temporary handle generated for this message retrieval, used to delete and modify messages in the Inactive status. Valid before NextVisibleTime.
MessageBody
String
This is test message 1.
Message content.
MessageBodyMD5
String
C5DD56A39F5F7BB8B3337C6D11B6****
The MD5 hash of the message content.
EnqueueTime
Long
1250700979248
The time when the message was sent to the queue, in milliseconds since 00:00:00 000 on January 1, 1970.
NextVisibleTime
Long
1250700799348
The next time the message can be consumed again, in milliseconds since 00:00:00 000 on January 1, 1970.
FirstDequeueTime
Long
1250700779318
The first time the message was consumed, in milliseconds since 00:00:00 000 on January 1, 1970.
DequeueCount
Integer
1
The total number of times the message has been consumed.
Examples
Request Example:
GET /queues/$queueName/messages?numOfMessages=16 HTTP/1.1
Host: $AccountId.mns.cn-hangzhou.aliyuncs.com
Date: Wed, 28 May 2012 22:32:00 GMT
x-mns-version: 2015-06-06
Authorization: MNS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEXA****
Response Example:
HTTP/1.1 200 OK
Content-Type=text/xml;charset=utf-8
Content-Length:500
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>
<ReceiptHandle>1-ODU4OTkzNDU5My0xNDMyNzI3ODI3LTItOA==</ReceiptHandle>
<MessageBodyMD5>C5DD56A39F5F7BB8B3337C6D11B6****</MessageBodyMD5>
<MessageBody>This is test message 1.</MessageBody>
<EnqueueTime>1250700979248</EnqueueTime>
<NextVisibleTime>1250700799348</NextVisibleTime>
<FirstDequeueTime>1250700779318</FirstDequeueTime >
<DequeueCount>1</DequeueCount>
<Priority>8</Priority>
</Message>
<Message>
<ReceiptHandle>1-ODU4OTkzNDU5My0xNDMyNzI3MjQwLTEtOA==</ReceiptHandle>
<MessageBodyMD5>C5DD56A39F5F7BB8B3337C6D11B6****</MessageBodyMD5>
<MessageBody>This is test message 2.</MessageBody>
<EnqueueTime>1250700979252</EnqueueTime>
<NextVisibleTime>1250700799350</NextVisibleTime>
<FirstDequeueTime>1250700779330</FirstDequeueTime >
<DequeueCount>1</DequeueCount>
<Priority>8</Priority>
</Message>
</Messages>
Error codes
Error code | Error message | HTTP status code | Description |
QueueNotExist | The queue name you provided is not exist. | 404 | The specified queue does not exist. You must create a queue. |
MessageNotExist | Message not exist. | 404 | No visible message exists in the queue. |