ReceiveMessage

Retrieves one or more messages from the specified queue, including the message body and message ID of each message. Messages returned by this action stay in the queue until you delete them. However, once a message is returned to a ReceiveMessage request, it is not returned on subsequent ReceiveMessage requests for the duration of the VisibilityTimeout. If you do not specify a VisibilityTimeout in the request, the overall visibility timeout for the queue is used for the returned messages. A default visibility timeout of 30 seconds is set when you create the queue. You can also set the visibility timeout for the queue by using SetQueueAttributes. See Visibility Timeout for more information.

[Note]Note

To view a message regardless of its visibility timeout state, use PeekMessage.

[Note]Note

Due to the distributed nature of the queue, a weighted random set of machines is sampled on a ReceiveMessage call. That means only the messages on the sampled machines are returned. If the number of messages in the queue is small (less than 1000), it is likely you will get fewer messages than you requested per ReceiveMessage call. You can always peek on any specific message in the queue by using the PeekMessage action.

The following table lists the special request parameters the action uses (in addition to the common request parameters listed in Common Request Parameters).

NameDescriptionRequired

NumberOfMessages

Maximum number of messages to return. If there are fewer messages in the queue than NumberOfMessages, the maximum number of messages returned is the current number of messages in the queue.

Not necessarily all the messages in the queue are returned.

Type: Integer from 1 to 256

Default: 1

No

VisibilityTimeout

The duration (in seconds) that the received messages are hidden from subsequent retrieve requests after being retrieved by a ReceiveMessage request. See Visibility Timeout for more information.

Type: Integer from 0 to 86400 (maximum 24 hours)

Default: The visibility timeout for the queue

No

The following table lists the response elements the action returns (in addition to the common response elements listed in The Structure of a Response). See the Sample Response.

NameDescription

Message

An element containing information about the message. See the example response below.

Ancestor: ReceiveMessageResponse

Child: MessageId, MessageBody

MessageBody

The message's contents.

Type: String

Ancestor: Message

MessageId

The message's system-assigned ID.

Type: String

Ancestor: Message

The following table lists the special errors the action returns (in addition to the common errors listed in Common Errors).

ErrorDescriptionHTTP Status Code

ReadCountOutOfRange

The value for NumberOfMessages is not valid (must be from 1 to 256).

400

The following example Query request receives messages from the specified queue.