diff --git a/src/main/clojure/cemerick/bandalore.clj b/src/main/clojure/cemerick/bandalore.clj index d97e9e4..39d9cc9 100644 --- a/src/main/clojure/cemerick/bandalore.clj +++ b/src/main/clojure/cemerick/bandalore.clj @@ -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 diff --git a/src/test/clojure/cemerick/bandalore_test.clj b/src/test/clojure/cemerick/bandalore_test.clj index b69338f..379e731 100644 --- a/src/test/clojure/cemerick/bandalore_test.clj +++ b/src/test/clojure/cemerick/bandalore_test.clj @@ -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")))