| Did this page help you? Yes No Tell us about it... |
Now that a message is in the queue, you can receive it (retrieve it from the queue). When requesting to get a message from the queue, you can't specify which message to get. You simply specify the maximum number of messages you want to get (up to 10), and SQS returns up to that maximum number. Because SQS is a distributed system and the particular queue we're working with here has very few messages in it, the response to the receive request might be empty. Therefore, you should rerun the sample until you get the message. You should design your own application so that it continues to poll until it gets one or more messages.
Amazon SQS doesn't automatically delete the message after returning it to you, in case you don't actually receive the message (the receiving component could fail or lose its connection). You must send a separate request to delete the message, which acknowledges that you've successfully received and processed the message. For more information, see Deleting a Message.
To receive a message
In the AWS Management Console select a queue.

Select View/Delete Messages from the Queue Actions drop-down list.
![]() | Note |
|---|---|
The Queue Actions drop-down list is available only if a queue is selected. |

Click Start Polling for Messages to receive a message from the queue.

![]() | Note |
|---|---|
The Start Polling for Messages dialog box will not appear if you have previously selected the Don't show this again checkbox. |
The View/Delete Messages in MyQueue dialog box displays a message from the queue.

A yellow progress bar at the bottom of the dialog box displays the status of the message's visibility timeout. While the bar is yellow, the message is not visible to other consumers. When the bar turns green, the visibility timeout is complete and the message is once again visible to other consumers.

Click Close to close the View/Delete Messages in MyQueue dialog box.
To run the sample
In the scratchpad, select ReceiveMessage from the Explore API list box.
Enter the queue URL in the Queue URL field.
Leave the Max Number Of Messages and the Visibility Timeout fields blank.
Select one of the following:
To invoke the request, click Invoke Request. Amazon SQS returns a response.
To view the signed URL, click Display Signed URL. Then, copy and paste the signed URL into a browser. Amazon SQS returns a response.
To view the string to sign, click Display String to Sign.
To run the sample
Open SimpleQueueServiceSample.java.
The following section of the code receives a message from your queue:
System.out.println("Receiving messages from MyQueue.\n");
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
for (Message message : messages) {
System.out.println(" Message");
System.out.println(" MessageId: " + message.getMessageId());
System.out.println(" ReceiptHandle: " + message.getReceiptHandle());
System.out.println(" MD5OfBody: " + message.getMD5OfBody());
System.out.println(" Body: " + message.getBody());
for (Entry<String, String> entry : message.getAttributes().entrySet()) {
System.out.println(" Attribute");
System.out.println(" Name: " + entry.getKey());
System.out.println(" Value: " + entry.getValue());
}
}
System.out.println();Compile and run the sample.
The MyQueue queue is polled for messages and returns 0 or more messages. The sample prints the following items:
The message ID that you received when you sent the message to the queue
The receipt handle (which you use later to delete the message)
An MD5 digest of the message body (for information about MD5, go to http://faqs.org/rfcs/rfc1321.html)
The message body
The request ID that Amazon SQS assigned to your request
If no messages are received in this particular call, the response includes only the request ID.
To run the sample
Open Program.cs.
The following section of the code receives a message from your queue:
//Receiving a message
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
receiveMessageRequest.QueueUrl = myQueueUrl;
ReceiveMessageResponse receiveMessageResponse = sqs.ReceiveMessage(receiveMessageRequest);
if (receiveMessageResponse.IsSetReceiveMessageResult())
{
Console.WriteLine("Printing received message.\n");
ReceiveMessageResult receiveMessageResult = receiveMessageResponse.ReceiveMessageResult;
foreach (Message message in receiveMessageResult.Message)
{
Console.WriteLine(" Message");
if (message.IsSetMessageId())
{
Console.WriteLine(" MessageId: {0}", message.MessageId);
}
if (message.IsSetReceiptHandle())
{
Console.WriteLine(" ReceiptHandle: {0}", message.ReceiptHandle);
}
if (message.IsSetMD5OfBody())
{
Console.WriteLine(" MD5OfBody: {0}", message.MD5OfBody);
}
if (message.IsSetBody())
{
Console.WriteLine(" Body: {0}", message.Body);
}
foreach (Amazon.SQS.Model.Attribute attribute in message.Attribute)
{
Console.WriteLine(" Attribute");
if (attribute.IsSetName())
{
Console.WriteLine(" Name: {0}", attribute.Name);
}
if (attribute.IsSetValue())
{
Console.WriteLine(" Value: {0}", attribute.Value);
}
}
}
}
String messageRecieptHandle = receiveMessageResponse.ReceiveMessageResult.Message[0].ReceiptHandle;Run the sample.
The MyQueue queue is polled for messages and returns 0 or more messages. The sample prints the following items:
The message ID that you received when you sent the message to the queue
The receipt handle (which you use later to delete the message)
An MD5 digest of the message body (for information about MD5, go to http://faqs.org/rfcs/rfc1321.html)
The message body
The request ID that Amazon SQS assigned to your request
If no messages are received in this particular call, the response includes only the request ID.
To run the sample
Open ReceiveMessageSample.pl.
Locate the following line.
# invokeReceiveMessage($service, $request);
Replace the line with the following new lines of code.
my $request = Amazon::SQS::Model::ReceiveMessageRequest->new({
QueueUrl => "queue URL you received from CreateQueue call"
});
invokeReceiveMessage($service, $request);Run the sample.
The MyQueue queue is polled for messages and returns 0 or more messages. The sample prints the following items:
The message ID that you received when you sent the message to the queue
The receipt handle (which you use later to delete the message)
An MD5 digest of the message body (for information about MD5, go to http://faqs.org/rfcs/rfc1321.html)
The message body
The request ID that Amazon SQS assigned to your request
If no messages are received in this particular call, the response includes only the request ID.
To run the sample
Open ReceiveMessageSample.php.
Locate the following line.
// invokeReceiveMessage($service, $request);
Replace the line with the following new lines of code.
require_once ('Amazon/SQS/Model/ReceiveMessageRequest.php');
$request = new Amazon_SQS_Model_ReceiveMessageRequest();
$request->setQueueUrl('queue URL you received from CreateQueue call');
invokeReceiveMessage($service, $request);Run the sample.
The MyQueue queue is polled for messages and returns 0 or more messages. The sample prints the following items:
The message ID that you received when you sent the message to the queue
The receipt handle (which you use later to delete the message)
An MD5 digest of the message body (for information about MD5, go to http://faqs.org/rfcs/rfc1321.html)
The message body
The request ID that Amazon SQS assigned to your request
If no messages are received in this particular call, the response includes only the request ID.