0

I am doing some operations which will publish messages to an SQS, a standard SQS, not a FIFO one. After that I am trying to receive both the messages from SQS and delete both of them. I have written the following code for receiving and deleting messages from SQS.

private ReceiveMessageRequest getReceiveMessageRequest() {
        return new ReceiveMessageRequest(QUEUE_URL)
            .withMaxNumberOfMessages(5);
    }

private void deleteMessagesInIntegrationQueue(List<Message> messages) {

        messages.forEach(
            message -> testSQS.deleteMessage(new DeleteMessageRequest(QUEUE_URL, message.getReceiptHandle()))
        );
    }

List<Message> results = testSQS.receiveMessage(getReceiveMessageRequest()).getMessages();
SOPL(results);

deleteMessagesInQueue(results);

List<Message> newResults = testSQS.receiveMessage(getReceiveMessageRequest()).getMessages();
        SOPL(newResults);

I am having the following problems with my code.

  1. When I am printing results list, I am expecting 2 messages, but only one message is being printed and the list size is also printed as 1.
  2. After deleteMessagesInQueue function call, I expect all the messages to be deleted in the SQS and the newResults list size should be 0, but one message is being printed and queue size is printed as 1. I can do purgeQueue instead of delete, but I want to understand what I am doing wrong here?
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • sqs does not guarantee that you receive all messages and during testing especially you can sometimes accidentially reject a message that gets returned to the queue. That would only print 1 & delete 1. Have you tried to loop your code (for at least 30 seconds or whatever the visibility timeout is)? – zapl Oct 25 '22 at 21:37
  • @zapl, are you suggesting to run a loop and fetch messages every 30 seconds(assuming visibility timeout is 30 sec). – Conquistador Oct 26 '22 at 09:49
  • Sort of but no waiting 30 seconds. When consuming an SQS Queue you usually have a simple endless loop that constantly calls `receiveMessage` with a configured "WaitTimeSeconds" of 20. No additional delay. The receive request returns immediately if an empty queue suddenly has messages but it returns empty after 20 seconds if there are no messages (long polling). If you somehow received a message but didn't delete it, you won't see the message again for 30 seconds (or whatever the "VisibilityTimeout" of the message is). But to ensure you see all messages at some point you have to keep checking – zapl Oct 26 '22 at 13:24

0 Answers0