Created
May 5, 2012 04:05
-
-
Save mwmitchell/2599605 to your computer and use it in GitHub Desktop.
http-agent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns http-agent | |
(:import | |
(java.net URL | |
HttpURLConnection) | |
(java.io InputStreamReader | |
BufferedReader | |
InputStream))) | |
(defn GET | |
[{:keys [url on-success on-fail]}] | |
(try | |
(let [conn (.openConnection (URL. url))] | |
(doto conn (.setRequestMethod "GET") (.connect)) | |
(with-open [stream (BufferedReader. | |
(InputStreamReader. (.getInputStream conn)))] | |
(on-success (.toString (reduce #(.append %1 %2) | |
(StringBuffer.) (line-seq stream)))))) | |
(catch Throwable e (on-fail e)))) | |
(defn GET-batch [requests & {:keys [wait]}] | |
(let [agents (map agent requests)] | |
(doseq [agent agents] (send-off agent GET)) | |
(apply await-for (or wait 0) agents))) | |
(defn error [e] | |
(prn (str (class e) (.getMessage e)))) | |
(dotimes [_ 10] | |
(GET-batch | |
[{:url "http://clojure.org" | |
:on-success (fn [_] (prn "OK clojure.org")) | |
:on-fail error} | |
{:url "http://python.org" | |
:on-success (fn [_] (prn "OK python.org")) | |
:on-fail error} | |
{:url "http://a1s2d3f4.com" | |
:on-success (fn [_] (prn "OK a1s2d3f4.org")) | |
:on-fail error}] | |
:wait 1000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment