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 run the sample
Open ReceiveMessageSample.java.
Locate the following lines.
ReceiveMessageRequest request = new ReceiveMessageRequest();
// @TODO: set request parameters here
// invokeReceiveMessage(service, request);Replace the lines with the following new lines of code.
ReceiveMessageRequest request = new ReceiveMessageRequest();
request.setQueueName("MyQueue");
invokeReceiveMessage(service, request);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 AmazonSQSSamples.cs.
Comment out the code you added in the preceding section (that code consisted of the following lines).
SendMessageRequest request = new SendMessageRequest(); request.QueueName = "MyQueue"; request.MessageBody = "This is my message text."; SendMessageSample.InvokeSendMessage(service, request);
Locate the following lines.
// ReceiveMessageRequest request = new ReceiveMessageRequest(); // @TODO: set request parameters here // ReceiveMessageSample.InvokeReceiveMessage(service, request);
Replace the lines with the following new lines of code.
ReceiveMessage request = new ReceiveMessageRequest(); request.QueueName = "MyQueue"; ReceiveMessageSample.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.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({
QueueName => "MyQueue"
});
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->setQueueName('MyQueue');
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 AmazonSQSSamples.vb.
Comment out the code you added in the previous section (that code consisted of the following lines).
Dim request As New SendMessageRequest() request.QueueName = "MyQueue" request.MessageBody = "This is my message text." SendMessageSample.InvokeSendMessage(service, request)
Locate the following lines.
' Dim request As New ReceiveMessageRequest() ' @TODO: set request parameters here ' ReceiveMessageSample.InvokeReceiveMessage(service, request)
Replace the lines with the following new lines of code.
Dim request As New ReceiveMessageRequest() request.QueueName = "MyQueue" ReceiveMessageSample.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
In the scratchpad, select ReceiveMessage from the Explore API list box.
Enter MyQueue in the Queue Name 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.