Skip to content
Browse files

Add parsing of URL-style AMQP settings.

  • Loading branch information...
1 parent 09b27d4 commit ed96e82972f263eabc014def29ab1b779e270ed7 @technomancy committed May 17, 2012
Showing with 20 additions and 3 deletions.
  1. +1 −0 project.clj
  2. +12 −2 src/die/roboter.clj
  3. +7 −1 test/die/test/roboter.clj
View
1 project.clj
@@ -1,5 +1,6 @@
(defproject die-roboter "1.0.0-SNAPSHOT"
:description "The robots get your work done in an straightforward way."
+ :url "https://github.com/technomancy/die-roboter"
:dependencies [[org.clojure/clojure "[1.2.0,1.3.0]"]
[commons-codec "1.5"]
[com.mefesto/wabbitmq "0.1.4"
View
14 src/die/roboter.clj
@@ -6,6 +6,7 @@
[clojure.java.io :as io]
[clojure.walk :as walk])
(:import (java.util UUID)
+ (java.net URI)
(java.util.concurrent Executors TimeUnit TimeoutException)
(java.lang.management ManagementFactory)
(java.io FilterInputStream ObjectInputStream ObjectOutputStream
@@ -46,12 +47,20 @@
(def ^{:dynamic true} *config* nil)
+(defn ^{:internal true} parse-url [{url :url :as config}]
+ (if-let [{:keys [host userInfo path port]} (and url (bean (URI. url)))]
+ (let [[user pass] (.split userInfo ":")
+ port (if (pos? port) port (:port config))]
+ (assoc config :username user :password pass
+ :host host :port port :vhost path))
+ config))
+
(defmacro with-robots [config & body]
;; :implicit should only start a new connection if there's none active.
`(if (or (and *config* (:implicit ~config))
(= *config* ~config)) ; avoid redundant nesting
(do ~@body)
- (binding [*config* ~config]
+ (binding [*config* (parse-url ~config)]
(wabbit/with-broker ~config
(wabbit/with-channel ~config
(init ~config)
@@ -183,7 +192,8 @@
others)))
(defn -main [& {:as opts}]
- (let [opts (into {:workers (or (System/getenv "WORKER_COUNT") 4)}
+ (let [opts (into {:workers (or (System/getenv "WORKER_COUNT") 4)
+ :url (System/getenv "RABBITMQ_URL")}
(walk/keywordize-keys opts))]
(println "Starting" (:workers opts) "workers.")
(dotimes [n (Integer. (:workers opts))] (add-worker opts))))
View
8 test/die/test/roboter.clj
@@ -130,4 +130,10 @@
#_(prn (try @(send-back '(throw (java.io.IOException.)))
(catch ExecutionException e
e)))
- (finally (future-cancel worker)))))
+ (finally (future-cancel worker)))))
+
+(deftest test-parse-uri
+ (is (= {:vhost "/vhost", :port 12,
+ :host "cloud.com", :password "pass", :username "user",}
+ (dissoc (parse-uri {:uri "amqp://user:pass@cloud.com:12/vhost"})
+ :uri))))

0 comments on commit ed96e82

Please sign in to comment.
Something went wrong with that request. Please try again.