Java – Number of messages in Amazon SQS

amazon-sqsamazon-web-servicesjava

I am trying to learn Amazon SQS. I am sending multiple message requests to my SQS like

    sendMessage(sqs, qUrl, "message 1");
    sendMessage(sqs, qUrl, "message 2");
    sendMessage(sqs, qUrl, "message 3");
    sendMessage(sqs, qUrl, "message 4");
    sendMessage(sqs, qUrl, "message 5");

Note: In the parameters I pass, sqs and qUrl are all the same.

//sendMessage method:

public void sendMessage(AmazonSQS sqs, String queueUrl, String msg){
        SendMessageRequest smr = new SendMessageRequest(queueUrl, msg);
        sqs.sendMessage(smr);
    }

But still when I try to count the numberOfRequests in the queue, it is 1 and not 5.

public int countRequests(AmazonSQS sqs, String queueUrl){
        // Receive messages
        int numberOfMessages=0;
        System.out.println("Receiving messages");
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
        messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
        for (Message message : messages) {
            numberOfMessages++;
            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());
            }
        }
        return numberOfMessages;
    }

Here,

ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
  messages = sqs.receiveMessage(receiveMessageRequest).getMessages();

"messages" receive only the last message and not the first 4.

My understanding is that I should be having 5 message requests in my queue. What am I missing out? Or Have I understood wrongly? Please correct me.

Best Solution

By default ReceiveMessage will only retrieve a single message. You can pass in the MaxNumberOfMessages option to retrieve more than one message in a single call.

However, be aware that even if you've written five messages to the queue, you might not get all of them back with a single ReceiveMessage call even if you set MaxNumberOfMessages to the maximum value of 10. This is because of the way SQS achieves guaranteed delivery - the system will actually store many copies of your message across many servers to ensure that your message will still get through even if a server goes down. Each message you post has to be replicated within the SQS system across many servers, and that replication is not instantaneous. This is why messages can arrive out of order, why ApproximateNumberOfMessages is approximate, and why receiveMessage may not always return as many messages as you expect. This is counter-intuitive at first, but doesn't matter in practice for many use-cases if you design your system appropriate, and it's how SQS is able to provide such high availability and scalability.

If you call receiveMessage repeatedly, you will receive all of your messages.

Related Question