AWS SQS Java. Not all messages are retrieved from the SQS queue

Question:

I have been trying several approaches to retrieve all messages from the SQS queue by using AWS SDK for Java to no avail. I have read about the distributed nature of the AWS SQS and that messages are stored on the different servers. But what I do not understand is why this architecture is not hidden from the end user. What tricks do I have to apply in Java code to retrieve all messages and be 100% sure that no one was missed?

I tried this with the “Long Polling”:

And this with Request Batching / Client-Side Buffering:

But I am still unable to retrieve all messages.

Any idea?

AWS Forum keeps silence on my post.

Answer:

When receiving messages from an SQS queue, you need to repeatedly call sqs:ReceiveMessage.

On each call to sqs:ReceiveMessage, you will get 0 or more messages from the queue which you’ll need to iterate through. For each message, you’ll also need to call sqs:DeleteMessage to remove the message from the queue when you’re done processing each message.

Add a loop around your “Long Polling” sample above to receive all messages.

Also note that you may receive the same message more than once. So allow your work to “reprocess” the same message, or detect a repeated message.

Leave a Reply