newbie
newbie

Reputation: 95

Spring boot AWS SqsListener sync operation

I have few sqs listener consuming from some standard sqs queue.

These listeners are responsible to call upon some service method which in turn talk to some3rd-party data provider.

When there are several messages consumed within short span of time, the load on service call to 3rd-party reaches it's limit(crosses rate limit).

1st listener
    @SqsListener(value = "${cloud.aws.queue-1}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
    public void handleQueue1(final String message, @Header RequestType type, @Header("MessageId") String messageId, Acknowledgment acknowledgment) throws JsonProcessingException {
        ..
        ...
        
        synchronized (this) {
            // call to some common service method
        }
        
        ...
        ..
    }

2nd listener
    @SqsListener(value = "${cloud.aws.queue-2}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
    public void handleQueue2(final String message, @Header RequestType type, @Header("MessageId") String messageId, Acknowledgment acknowledgment) throws JsonProcessingException {
        ..
        ...
        
        synchronized (this) {
            // call to some common service method
        }
        
        ...
        ..
    }

My question is how can I make sure that each sqs listener reaches the service call one after another assuming each requires data from 3rd-party call.

I tried adding synchronized block but I'm unable to figure out if this is okay to have it.

Upvotes: 1

Views: 1245

Answers (1)

MarkOfHall
MarkOfHall

Reputation: 3353

While it is possible to do what you're trying to do with Java synchronization it's a really bad idea for a number of reasons.

Suggested solutions:

  1. Use the JMSListener concurrency configuration to restrict the number or worker threads running for each listener. @JmsListener
  2. So, what. Let the 3rd party api fail and throw an exception. SQS will automatically retry. You will want to configure your SQS queue's visibility timeout window and retry limits with your 3rd party API limits in mind. SQS Visibility Time Out
  3. Work with the 3rd party to increase your limits.

Upvotes: 1

Related Questions