I am implementing a SQS subscriber using spring boot and after a few research on internet I have found the project spring-cloud
.
Use the annotation @SqsListener
looks pretty easy to receive messages from a topic but I would like to implement it as Long Pooling instead of short pooling that receives every message as soon it arrives.
@SqsListener(
value = ["queue"],
deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS
)
fun subscribeToSSmsg: String) {
....
}
This is working smooth but I would like to receive messages using long pooling. Is there any way to do it using spring-cloud?
With AWS Java SDK 1.X.
You need to define a custom org.springframework.cloud.aws.messaging.config.SimpleMessageListenerContainerFactory
bean which would need a com.amazonaws.services.sqs.AmazonSQSAsync
bean.
Add the below beans to your configuration.
@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSqs) {
SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
factory.setWaitTimeOut(20); //Long polling, the maximum value of 20 seconds
factory.setAmazonSqs(amazonSqs);
factory.setMaxNumberOfMessages(10); //The maximum number of messages you want to read in a single poll
return factory;
}
@Bean
@Primary
AmazonSQSAsync amazonSQSAsync(AWSCredentialsProvider credentialsProvider) {
return new AmazonSQSBufferedAsyncClient(
AmazonSQSAsyncClientBuilder
.standard()
.withRegion("region") //Set an appropriate region
.withCredentials(credentialsProvider)
.build());
}
Also ensure the HTTP clients you are using has a higher timeout than the value you set for the long polling.