Skip to content

Instantly share code, notes, and snippets.

@leewin12
Created January 5, 2015 10:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leewin12/8b6518a17d301e7ae5d1 to your computer and use it in GitHub Desktop.
Save leewin12/8b6518a17d301e7ae5d1 to your computer and use it in GitHub Desktop.
Groovy AsyncHttpClient
@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}"
}
}
@leewin12
Copy link
Author

leewin12 commented Jan 5, 2015

@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

@leewin12
Copy link
Author

// 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