spring-attic / spring-cloud-aws

All development has moved to https://github.com/awspring/spring-cloud-aws Integration for Amazon Web Services APIs with Spring
https://awspring.io/
Apache License 2.0
591 stars 373 forks source link

Enable long polling in QueueMessagingTemplate #646

Open budaimartin opened 4 years ago

budaimartin commented 4 years ago

Background

I'm using QueueMessagingTemplate to send and receive messages to/from an SQS queue. Receive happens in an infinite loop that regularly polls the queue. However, when there are no messages on the queue, a large amount of empty receives happens which increases costs. I set ReceiveMessageWaitTimeSeconds on the queue in order to benefit from the long polling funcitonality of SQS, but the situation didn't get better.

I realized that QueueMessagingTemplate::receive delegates to QueueMessageChannel::receive without a parameter and then a hardcoded 0 value is then set in the ReceiveMessageRequest here: https://github.com/spring-cloud/spring-cloud-aws/blob/master/spring-cloud-aws-messaging/src/main/java/org/springframework/cloud/aws/messaging/core/QueueMessageChannel.java#L220-L241. This overwrites the default setting and makes it impossible to use long polling.

Suggestion

Please consider the following to make long polling possible:

Alternatives considered

I tried using @SqsListener where ReceiveMessageWaitTimeSeconds could be set via SimpleMessageListenerContainerFactory, but the architecture of my application would require dynamic creation of the queue listener and preferably outside of Spring ApplicationContext. A similar flexible solution could be provided for QueueMessagingTemplate.

maciejwalkowiak commented 4 years ago

Thanks for reporting and suggestions!