DD Jin
DD Jin

Reputation: 355

How can I know if SQS has a new message?

I am writing an application that use lambda function that send request to a spring boot application which will call other service. I have to use sqs (required). So sqs is between lambda and spring. The question is how do my spring application know if there is new message in sqs.

  1. I heard about long pooling, but I don't know if this is what I need.

  2. Do I need to set a loop that open the long pooling forever or something?

  3. Is it efficient? I mean if there are 10 message in sqs, The connection will be opened ten times?

I aslo find using while loop here: Check for an incoming message in aws sqs

Thanks

Upvotes: 1

Views: 3179

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 270104

The answer you linked is accurate.

You must write a program that polls SQS for a message (or up to 10 messages). It is more efficient to use long polling because you require less calls.

If you wish to know about a message very quickly, then you will need to poll continually. That is, as soon as it comes back and says "nothing to receive", you should call it again. To reduce the frequency of these calls, you can set long polling, up to a maximum of 20 seconds. This means that, if there are no messages in the queue, the ReceiveMessages() option will take 20 seconds before it returns a response of "no messages". If, however, a message arrives in the meantime, it will respond immediately. The long polling option is specified when making the ReceiveMessages() request.

If you do not require instant notification, your application could call less often (eg every minute, or every few minutes). This would involve less calls to Amazon SQS.

When making the ReceiveMessages() call, your application can request up to 10 messages. This means that multiple messages might be returned.

Once your application has finished processing a message, it must call DeleteMessage() to have the message removed from the queue. This is a failsafe that will automatically put the message back on the queue if there is a problem with the application and the message doesn't get correctly processed.

This is a great video from the AWS re:Invent conference that explains Amazon SQS (and Amazon SNS) in detail: AWS re:Invent SVC 105: AWS Messaging

Upvotes: 4

Related Questions