lundi 7 septembre 2015

Amazon SQS queue send/receive unexpected behaviour with Java SDK - what am I doing wrong?

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.




Aucun commentaire:

Enregistrer un commentaire