Reputation: 610
I'm trying to interface some Java code with an SQS queue I've made on AWS. I've begun by writing a test to get the behaviour I want (i.e. writing and receiving messages) and it seems to be behaving very strangely. Here's my test code:
@Test
public void testMessagesAreSentToQueue() {
sqs.sendMessage(queueUrl, "TEST MESSAGE1");
sqs.sendMessage(queueUrl, "TEST MESSAGE2");
sqs.sendMessage(queueUrl, "TEST MESSAGE3");
ReceiveMessageRequest req = new ReceiveMessageRequest().withQueueUrl(queueUrl).withWaitTimeSeconds(20);
List<Message> messagesInQueue = sqs.receiveMessage(req).getMessages();
assertThat(messagesInQueue.size()).isEqualTo(3);
assertThat(messagesInQueue.get(0).getBody()).isEqualTo("TEST MESSAGE1");
assertThat(messagesInQueue.get(1).getBody()).isEqualTo("TEST MESSAGE2");
assertThat(messagesInQueue.get(2).getBody()).isEqualTo("TEST MESSAGE3");
}
Now, if I comment out messages 2 and 3, and assert that only one message is returned, the test passes. But when I have more than one message being sent, the test fails, saying that the size of messagesInQueue
was 1. It seems that no matter how many messages I send using sendMessage
, I always get just one message back in receiveMessage().getMessages()
.
Am I misunderstanding the way SQS works here? I'm expecting to able to send and receive back any number of messages I want.
EDIT - I'm using the maxNumberOfMessages and repeating long poll requests until the queue is drained (more on that here - Amazon SQS Long Polling not returning all messages)
Upvotes: 1
Views: 914
Reputation: 14550
Looking at the API docs for ReceiveMessageRequest it looks like it defaults to retrieving at most 1 message per call. Increasing this number using new ReceiveMessageRequest().withMaxNumberOfMessages(3)
may solve your issue, However it may still not work as you expect, as the docs also state:
If the number of messages in the queue is small (less than 1000), it is likely you will get fewer messages than you requested per ReceiveMessage call. If the number of messages in the queue is extremely small, you might not receive any messages in a particular ReceiveMessage response; in which case you should repeat the request.
Upvotes: 3