Compare commits

..

No commits in common. "master" and "bandalore-0.0.4" have entirely different histories.

4 changed files with 12 additions and 54 deletions

View file

@ -14,14 +14,14 @@ Bandalore is available in Maven central. Add it to your Maven project's `pom.xm
<dependency> <dependency>
<groupId>com.cemerick</groupId> <groupId>com.cemerick</groupId>
<artifactId>bandalore</artifactId> <artifactId>bandalore</artifactId>
<version>0.0.6</version> <version>0.0.4</version>
</dependency> </dependency>
``` ```
or your leiningen project.clj: or your leiningen project.clj:
```clojure ```clojure
[com.cemerick/bandalore "0.0.6"] [com.cemerick/bandalore "0.0.4"]
``` ```
Bandalore is compatible with Clojure 1.2.0+. Bandalore is compatible with Clojure 1.2.0+.
@ -56,7 +56,7 @@ to do anything:
**Security Note** If your application using Bandalore is deployed to EC2, _you **Security Note** If your application using Bandalore is deployed to EC2, _you
should not put your AWS credentials on those EC2 nodes_. Rather, should not put your AWS credentials on those EC2 nodes_. Rather,
[give your EC2 instances IAM roles](http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/java-dg-roles.html), [give your EC2 instances IAM roles](http://docs.aws.amazon.com/IAM/latest/UserGuide/role-usecase-ec2app.html),
and use the nullary arity of `create-client`: and use the nullary arity of `create-client`:
```clojure ```clojure
@ -108,28 +108,6 @@ That's cleaner than having to interop directly with the Java SDK, but it's all
pretty pedestrian stuff. You can do more interesting things with some pretty pedestrian stuff. You can do more interesting things with some
simple higher-order functions and other nifty Clojure facilities. simple higher-order functions and other nifty Clojure facilities.
### Enabling SQS Long Polling
[Long polling](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html) reduces the number of empty responses by allowing Amazon SQS service to wait until a message is available in the queue before sending a response. You can enable long polling on an individual receive request by supplying the optional kwarg `:wait-time-seconds`:
:wait-time-seconds - time in seconds (bewteen 0 and 20) for SQS to wait if there are no messages in the queue. A value of 0 indicates no long polling.
```clojure
; ensure our queue is empty to start
#> (get (sqs/queue-attrs client q) "ApproximateNumberOfMessages")
"0"
#> (let [no-polling (future (sqs/receive client q))
long-polling (future (sqs/receive client q :wait-time-seconds 20))]
(Thread/sleep 10000) ;; Sleep 10s before sending message
(sqs/send client q "my message body")
(println (count @no-polling))
(println (count @long-polling)))
0
1
nil
```
### Sending and receiving Clojure values ### Sending and receiving Clojure values
SQS' message bodies are strings, so you can stuff anything in them that you can SQS' message bodies are strings, so you can stuff anything in them that you can

View file

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.cemerick</groupId> <groupId>com.cemerick</groupId>
<artifactId>bandalore</artifactId> <artifactId>bandalore</artifactId>
<version>0.0.7-SNAPSHOT</version> <version>0.0.4</version>
<name>bandalore</name> <name>bandalore</name>
<description>A Clojure library for Amazon's Simple Queue Service (SQS).</description> <description>A Clojure library for Amazon's Simple Queue Service (SQS).</description>
<url>http://github.com/cemerick/bandalore</url> <url>http://github.com/cemerick/bandalore</url>
@ -36,7 +36,7 @@
<dependency> <dependency>
<groupId>com.amazonaws</groupId> <groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId> <artifactId>aws-java-sdk</artifactId>
<version>1.8.0</version> <version>1.3.21.1</version>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -50,10 +50,9 @@
(.deleteQueue client (DeleteQueueRequest. queue-url))) (.deleteQueue client (DeleteQueueRequest. queue-url)))
(defn list-queues (defn list-queues
"Returns a seq of all queues' URL strings. Takes an optional string prefix "Returns a seq of all queues' URL strings."
argument to only list queues with names that start with the prefix." [^AmazonSQSClient client]
[^AmazonSQSClient client & {:keys [prefix]}] (->> (ListQueuesRequest.)
(->> (ListQueuesRequest. prefix)
(.listQueues client) (.listQueues client)
.getQueueUrls .getQueueUrls
seq)) seq))
@ -106,11 +105,6 @@
Defaults to the empty set (i.e. no attributes will be included in Defaults to the empty set (i.e. no attributes will be included in
received messages). received messages).
See the SQS documentation for all support message attributes. 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: Returns a seq of maps with these slots:
@ -121,17 +115,13 @@
:receipt-handle - the ID used to delete the message from the queue after :receipt-handle - the ID used to delete the message from the queue after
it has been fully processed. it has been fully processed.
:source-queue - the URL of the queue from which the message was received" :source-queue - the URL of the queue from which the message was received"
[^AmazonSQSClient client queue-url & {:keys [limit [^AmazonSQSClient client queue-url & {:keys [limit visibility ^java.util.Collection attributes]
visibility
wait-time-seconds
^java.util.Collection attributes]
:or {limit 1 :or {limit 1
attributes #{}}}] attributes #{}}}]
(let [req (-> (ReceiveMessageRequest. queue-url) (let [req (-> (ReceiveMessageRequest. queue-url)
(.withMaxNumberOfMessages (-> limit (min 10) (max 1) int Integer/valueOf)) (.withMaxNumberOfMessages (-> limit (min 10) (max 1) int Integer.))
(.withAttributeNames attributes)) (.withAttributeNames attributes))
req (if wait-time-seconds (.withWaitTimeSeconds req (Integer/valueOf (int wait-time-seconds))) req) req (if visibility (.withVisibilityTimeout req (Integer. (int visibility))) req)]
req (if visibility (.withVisibilityTimeout req (Integer/valueOf (int visibility))) req)]
(->> (.receiveMessage client req) (->> (.receiveMessage client req)
.getMessages .getMessages
(map (partial message-map queue-url))))) (map (partial message-map queue-url)))))

View file

@ -59,9 +59,7 @@
; sending a msg seems to "force" the queue's existence in listings ; sending a msg seems to "force" the queue's existence in listings
(send client *test-queue-url* msg) (send client *test-queue-url* msg)
(wait-for-condition #((set (list-queues client)) *test-queue-url*) (wait-for-condition #((set (list-queues client)) *test-queue-url*)
"Created queue not visible in result of list-queues") "Created queue not visible in result of list-queues")))
(wait-for-condition #((set (list-queues client :prefix test-queue-name-prefix)) *test-queue-url*)
"Created queue not visible in result of list-queues with prefix")))
(defsqstest test-queue-attrs (defsqstest test-queue-attrs
(let [{:strs [MaximumMessageSize] :as base-attrs} (queue-attrs client *test-queue-url*) (let [{:strs [MaximumMessageSize] :as base-attrs} (queue-attrs client *test-queue-url*)
@ -121,11 +119,3 @@
(let [v (-> (receive client *test-queue-url* :visibility 5) first :body read-string)] (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))))) (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")))