I have a Java Spring Boot app where I would like to listen to SQS messages as they come in. Ideally I want to do this in the most efficent manner possible. Currently the only solution I can see is to have a while loop the runs every few seconds and polls the SQS queue, but I feel there is a better solution out there. Ideally using the AWS Java SDK v2
I tried using @SqsListener but this does not seem to work.
@SqsListener(SQS_SMS_QUEUE_URL)
public static void loadMessageFromSQS(String message) {
log.info("message from SQS Queue {}", message);
}
while(true){
ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder()
.queueUrl(WORKY_SQS_SMS_QUEUE_URL)
.maxNumberOfMessages(7)
.build();
final List<Message> messages = sqsClient.receiveMessage(receiveMessageRequest).messages();
log.info("num of messages: {}", messages.size());
messages.forEach(message -> log.info("the conents: {} ", message.toString()));
}
From Amazon SQS short and long polling - Amazon Simple Queue Service:
With short polling, the
ReceiveMessage
request queries only a subset of the servers (based on a weighted random distribution) to find messages that are available to include in the response. Amazon SQS sends the response right away, even if the query found no messages.With long polling, the
ReceiveMessage
request queries all of the servers for messages. Amazon SQS sends a response after it collects at least one available message, up to the maximum number of messages specified in the request. Amazon SQS sends an empty response only if the polling wait time expires.... When you consume messages from a queue using short polling, Amazon SQS samples a subset of its servers (based on a weighted random distribution) and returns messages from only those servers. Thus, a particular
ReceiveMessage
request might not return all of your messages.
Therefore, to receive more messages, you can use Long Polling.
From ReceiveMessageRequest (AWS SDK for Java):
waitTimeSeconds()
: The duration (in seconds) for which the call waits for a message to arrive in the queue before returning.
When passing a value for waitTimeSeconds
(maximum of 20 seconds), the call will block while there are no messages available in the queue. However, if there is a message (or a message comes during that time period), it will immediately return with messages.
This means that if there are no messages available, then your app will only be calling the queue every 20 seconds.
See: Setting Up Long Poll