Created
January 5, 2015 10:09
-
-
Save leewin12/8b6518a17d301e7ae5d1 to your computer and use it in GitHub Desktop.
Groovy AsyncHttpClient
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
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1') | |
import groovyx.net.http.AsyncHTTPBuilder | |
import groovyx.net.http.URIBuilder | |
import static groovyx.net.http.Method.POST | |
import static groovyx.net.http.ContentType.TEXT | |
import groovy.json.JsonSlurper | |
def http = new AsyncHTTPBuilder( poolSize:100 ) | |
def slurper = new JsonSlurper() | |
def reader = new File(args[0]) | |
reader.eachLine { line, lineNumber -> | |
def uri = new URIBuilder(line); | |
def response = http.post(uri:uri) { req,TEXT -> | |
} | |
http.handler.success = { resp, text -> | |
println "num:${lineNumber} DONE:${uri.path}:${resp.statusLine.statusCode}:${text}" | |
} | |
http.handler.failure = { resp,text -> | |
println "num:${lineNumber} ERROR:${resp.statusLine.statusCode}:${text}" | |
} | |
} |
// No handler style
def reader = new File(args[0])
reader.eachLine { line, lineNumber ->
def uri = new URIBuilder(line);
def response = http.post(uri:uri) { resp, text ->
println "${lineNumber} ${resp.getStatus()} ${text}"
}
}
Thread.sleep(4000)
http.shutdown()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@see http://groovy.codehaus.org/modules/http-builder/doc/async.html
Not documented well, so I make it.
It was tested on Groovy Version: 2.3.7 JVM: 1.7.0_60 Vendor: Oracle Corporation OS: Linux