Question: Say I have the following code:

```
    final ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
    receiveMessageRequest.withMaxNumberOfMessages(10);
    final List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
    for (final 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 (final Entry<String, String> entry : message.getAttributes().entrySet()) {
            System.out.println("Attribute");
            System.out.println("  Name:  " + entry.getKey());
            System.out.println("  Value: " + entry.getValue());
        }
    }

```

This code returns 10 messages from my understanding. If the SQS queue has 1000 messages then do I need another loop ?

Is there a way to know the SQS queue size?

Answer: 
Yes you would need another (outer) loop to continue getting messages in batches of 10, until there were no messages left. You don't need to know the queue size for that, just use a while loop and iterate until get you get a response with no messages.

Something like this:

```
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
receiveMessageRequest.withMaxNumberOfMessages(10);
List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
while (messages.size() > 0) {
    for (final 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 (final Entry<String, String> entry : message.getAttributes().entrySet()) {
            System.out.println("Attribute");
            System.out.println("  Name:  " + entry.getKey());
            System.out.println("  Value: " + entry.getValue());
        }
    }

    receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
    receiveMessageRequest.withMaxNumberOfMessages(10);
    messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
}

```

If you do need to get the number of messages still in the queue you can query the AWS SDK for the queue attribute `ApproximateNumberOfMessages`. like:

```
GetQueueAttributesResult attrResult = sqs.getQueueAttributes(queueUrl, ["ApproximateNumberOfMessages"]);
System.out.println("Messages in queue: " + attrResult.getAttributes()["ApproximateNumberOfMessages"]);

```
No messages / empty messages may be misguiding. The retrieval may not have happened due to different reasons like-
Visibility timeout due to another consumer polling
Distributed nature of SQS infrastructure