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);
}
- there is nothing being returned from the above example.
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()));
}
- Without the While Loop this is working but is only returning 2 first messages, but it is not continuously returning new messages added to the queue. Its essentially only retrieving the first few messages once.
From Amazon SQS short and long polling - Amazon Simple Queue Service:
Therefore, to receive more messages, you can use Long Polling.
From ReceiveMessageRequest (AWS SDK for Java):
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