AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.
Retrieves one or more messages (up to 10), from the specified queue. Using the WaitTimeSeconds
parameter enables long-poll support. For more information, see Amazon
SQS Long Polling in the Amazon SQS Developer Guide.
Short poll is the default behavior where a weighted random set of machines is sampled
on a ReceiveMessage
call. Thus, only the messages on the sampled machines are
returned. If the number of messages in the queue is small (fewer than 1,000), you
most likely get fewer messages than you requested per ReceiveMessage
call.
If the number of messages in the queue is extremely small, you might not receive any
messages in a particular ReceiveMessage
response. If this happens, repeat the
request.
For each message returned, the response includes the following:
The message body.
An MD5 digest of the message body. For information about MD5, see RFC1321.
The MessageId
you received when you sent the message to the queue.
The receipt handle.
The message attributes.
An MD5 digest of the message attributes.
The receipt handle is the identifier you must provide when deleting the message. For more information, see Queue and Message Identifiers in the Amazon SQS Developer Guide.
You can provide the VisibilityTimeout
parameter in your request. The parameter
is applied to the messages that Amazon SQS returns in the response. If you don't include
the parameter, the overall visibility timeout for the queue is used for the returned
messages. For more information, see Visibility
Timeout in the Amazon SQS Developer Guide.
A message that isn't deleted or a message whose visibility isn't extended before the visibility timeout expires counts as a failed receive. Depending on the configuration of the queue, the message might be sent to the dead-letter queue.
In the future, new attributes might be added. If you write code that calls this action, we recommend that you structure your code so that it can handle new attributes gracefully.
For .NET Core this operation is only available in asynchronous form. Please refer to ReceiveMessageAsync.
Namespace: Amazon.SQS
Assembly: AWSSDK.SQS.dll
Version: 3.x.y.z
public virtual ReceiveMessageResponse ReceiveMessage( ReceiveMessageRequest request )
Container for the necessary parameters to execute the ReceiveMessage service method.
Exception | Condition |
---|---|
InvalidAddressException | The accountId is invalid. |
InvalidSecurityException | When the request to a queue is not HTTPS and SigV4. |
KmsAccessDeniedException | The caller doesn't have the required KMS access. |
KmsDisabledException | The request was denied due to request throttling. |
KmsInvalidKeyUsageException | The request was rejected for one of the following reasons: The KeyUsage value of the KMS key is incompatible with the API operation. The encryption algorithm or signing algorithm specified for the operation is incompatible with the type of key material in the KMS key (KeySpec). |
KmsInvalidStateException | The request was rejected because the state of the specified resource is not valid for this request. |
KmsNotFoundException | The request was rejected because the specified entity or resource could not be found. |
KmsOptInRequiredException | The request was rejected because the specified key policy isn't syntactically or semantically correct. |
KmsThrottledException | Amazon Web Services KMS throttles requests for the following conditions. |
OverLimitException | The specified action violates a limit. For example, ReceiveMessage returns this error if the maximum number of in flight messages is reached and AddPermission returns this error if the maximum number of permissions for the queue is reached. |
QueueDoesNotExistException | The specified queue doesn't exist. |
RequestThrottledException | The request was denied due to request throttling. The rate of requests per second exceeds the Amazon Web Services KMS request quota for an account and Region. A burst or sustained high rate of requests to change the state of the same KMS key. This condition is often known as a "hot key." Requests for operations on KMS keys in a Amazon Web Services CloudHSM key store might be throttled at a lower-than-expected rate when the Amazon Web Services CloudHSM cluster associated with the Amazon Web Services CloudHSM key store is processing numerous commands, including those unrelated to the Amazon Web Services CloudHSM key store. |
UnsupportedOperationException | Error code 400. Unsupported operation. |
This example shows how to receive a message.
var client = new AmazonSQSClient(); var request = new ReceiveMessageRequest { AttributeNames = new List<string>() { "All" }, MaxNumberOfMessages = 5, QueueUrl = "https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyTestQueue", VisibilityTimeout = (int)TimeSpan.FromMinutes(10).TotalSeconds, WaitTimeSeconds = (int)TimeSpan.FromSeconds(5).TotalSeconds }; var response = client.ReceiveMessage(request); if (response.Messages.Count > 0) { foreach (var message in response.Messages) { Console.WriteLine("For message ID '" + message.MessageId + "':"); Console.WriteLine(" Body: " + message.Body); Console.WriteLine(" Receipt handle: " + message.ReceiptHandle); Console.WriteLine(" MD5 of body: " + message.MD5OfBody); Console.WriteLine(" MD5 of message attributes: " + message.MD5OfMessageAttributes); Console.WriteLine(" Attributes:"); foreach (var attr in message.Attributes) { Console.WriteLine(" " + attr.Key + ": " + attr.Value); } } } else { Console.WriteLine("No messages received."); }
.NET Framework:
Supported in: 4.5 and newer, 3.5