Created
January 28, 2012 16:41
-
-
Save martintrojer/1694970 to your computer and use it in GitHub Desktop.
async-clojure
This file contains hidden or 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
(async | |
(for [i (range 10 80)] | |
(http-request | |
{:method :get | |
:url (format "http://fssnip.net/%d" i)})))) |
This file contains hidden or 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
(def result (agent [])) | |
(for [i (range 10 80)] | |
(let [response | |
(http-request | |
{:method :get | |
:url (format "http://fssnip.net/%d" i)})] | |
(on-success | |
response | |
(fn[r] (send result #(conj % (:body r))))))) |
This file contains hidden or 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
(defn fetch-url [adr] | |
(let [url (java.net.URL. adr)] | |
(with-open [stream (.openStream url)] | |
(let [buf (java.io.BufferedReader. (java.io.InputStreamReader. stream))] | |
(apply str (line-seq buf)))))) | |
(future (fetch-url "http://www.google.co.uk/")) |
This file contains hidden or 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
let download url = async { | |
let request = HttpWebRequest.Create(Uri(url)) | |
let! response = request.AsyncGetResponse() | |
use stream = response.GetResponseStream() | |
let! res = asyncReadToEnd stream | |
return res | |
} |
This file contains hidden or 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
(def ctr (atom 0)) | |
(def result (agent [])) | |
; 3 get result | |
(def response-handler | |
(reify ChannelUpstreamHandler | |
(handleUpstream [_ ctx evt] | |
(do | |
(swap! ctr inc) | |
(when (instance? MessageEvent evt) | |
(let [resp (.getMessage evt) | |
cont (.getContent resp) | |
heads (.getHeaders resp)] | |
(send result #(conj % cont)))))))) | |
; 2 send request | |
(defn connection-ok [uri] | |
(let [host (.getHost uri)] | |
(reify ChannelFutureListener | |
(operationComplete [_ fut] | |
(if (.isSuccess fut) | |
(let [ch (.getChannel fut) | |
pipe (.getPipeline ch) | |
req (DefaultHttpRequest. | |
(HttpVersion/HTTP_1_1) | |
(HttpMethod/GET) | |
(.toASCIIString uri))] | |
(.setHeader req "Host" host) | |
(.setHeader req "Connection" "close") | |
(.addLast pipe "codec" (HttpClientCodec.)) | |
(.addLast pipe "handler" response-handler) | |
(.write ch req) | |
nil)))))) | |
; 1 connect | |
(def bootstrap | |
(ClientBootstrap. | |
(NioClientSocketChannelFactory. | |
(Executors/newCachedThreadPool) | |
(Executors/newCachedThreadPool)))) | |
(for [i (range 10 80)] | |
(let [name (format "http://fssnip.net/%d" i) | |
uri (java.net.URI. name) | |
port 80 | |
host (.getHost uri) | |
con-future (.connect bootstrap (java.net.InetSocketAddress. host port))] | |
(.addListener | |
con-future | |
(connection-ok uri)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment