Reputation: 95
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).
@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
}
...
..
}
@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
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:
Upvotes: 1