0

When I send multiple messages to my SQS and read it like

//Sending message to queue

    SendMessageRequest smr = new SendMessageRequest(queueUrl, "one");
    sqs.sendMessage(smr);
    smr = new SendMessageRequest(queueUrl, "two");
    sqs.sendMessage(smr);
    smr = new SendMessageRequest(queueUrl, "three");
    sqs.sendMessage(smr);
    Thread.sleep(5000);

//Reading Queue

ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();

I have only one message in my List.

When I repeat the "Reading Queue" for the second time, I get the second message and I when repeat that for the third time I get the third message. The messages being retrieved from queue are in random order. But why am I not getting all the 3 messages in the List<messages>?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Prasanna
  • 2,593
  • 7
  • 39
  • 53

3 Answers3

1

You need to set the maxNumberOfMessages in the request.

Try

ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl)
          .withMaxNumberOfMessages(3);

And see if you get all of them.

EDIT
Actually I just saw this question and I think you will NOT get your messages all in once. You will need to call the method more than once.

Community
  • 1
  • 1
joaonlima
  • 618
  • 5
  • 15
0

we set the wait timeout to 20 and get multiples. You may be running out of time before you get them all.

Randy Avis
  • 75
  • 9
0

Deleting the message after reading it will allow you to access other messages in the queue. so you'd have to make multiple calls to view all messages...

This worked for me : sqs.deleteMessage(SQSQueueURL, message.getReceiptHandle());

Gavin
  • 1,725
  • 21
  • 34
Tebogo
  • 1