Adds long polling support

Adds long polling support to receive message requests.
The option 'wait-time-seconds' has been added to the 'receive' function.
This option behaves exactly the same as the 'WaitTimeSeconds' parameter
of the ReceiveMessageRequest class in the AWS Java SDK.

The following text is shamelessly taken from the (docs
http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html):

Long polling allows the Amazon SQS service to wait until a message is
available in the queue before sending a response. This helps reduce the
amount of empty responses, thus reducing costs.

Another benefit is reducing the false empty reponses, where messages are
available in the queue but are not included in the response.
This happens when Amazon SQS uses short (standard) polling, the default
behavior, where only a subset of the servers (based on a weighted random
distribution) are queried to see if any messages are available to
include in the response. On the other hand, when long polling is
enabled, Amazon SQS queries all of the servers.
This commit is contained in:
Daniel Alberto Cañas 2014-06-24 21:18:06 -06:00
parent 98cb434787
commit ce8c02bdc4
2 changed files with 18 additions and 1 deletions

View file

@ -106,6 +106,11 @@
Defaults to the empty set (i.e. no attributes will be included in
received messages).
See the SQS documentation for all support message attributes.
:wait-time-seconds - enables long poll support. time is in seconds, bewteen
0 (default - no long polling) and 20.
Allows Amazon SQS service to wait until a message is available
in the queue before sending a response.
See the SQS documentation at (http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html)
Returns a seq of maps with these slots:
@ -116,12 +121,16 @@
:receipt-handle - the ID used to delete the message from the queue after
it has been fully processed.
:source-queue - the URL of the queue from which the message was received"
[^AmazonSQSClient client queue-url & {:keys [limit visibility ^java.util.Collection attributes]
[^AmazonSQSClient client queue-url & {:keys [limit
visibility
wait-time-seconds
^java.util.Collection attributes]
:or {limit 1
attributes #{}}}]
(let [req (-> (ReceiveMessageRequest. queue-url)
(.withMaxNumberOfMessages (-> limit (min 10) (max 1) int Integer.))
(.withAttributeNames attributes))
req (if wait-time-seconds (.withWaitTimeSeconds req (Integer. (int wait-time-seconds))) req)
req (if visibility (.withVisibilityTimeout req (Integer. (int visibility))) req)]
(->> (.receiveMessage client req)
.getMessages

View file

@ -121,3 +121,11 @@
(let [v (-> (receive client *test-queue-url* :visibility 5) first :body read-string)]
(is (some #(= v (-> % :body read-string)) (polling-receive client *test-queue-url* :max-wait 10000)))))
(defsqstest test-receive-long-polling
(let [q *test-queue-url*
no-poll (future (receive client q))
long-poll (future (receive client q :wait-time-seconds 20))]
(Thread/sleep 10000)
(send client q "1")
(is (== 0 (count @no-poll)) "Should not return messages")
(is (== 1 (count @long-poll)) "Should return 1 message")))