AsyncHTTPBuilder

AsyncHTTPBuilder is very similar to HTTPBuilder, except that all requests are delegated to a thread pool and executed asynchronously. Requests return a java.util.concurrent.Future instance, which can be used to access the response data once it has completed.

import groovyx.net.http.AsyncHTTPBuilder
import static groovyx.net.http.ContentType.HTML

def http = new AsyncHTTPBuilder(
                poolSize : 4,
                uri : 'http://hc.apache.org',
                contentType : HTML )


def result = http.get(path:'/') { resp, html ->
    println ' got async response!'
    return html
}

assert result instanceof java.util.concurrent.Future

while ( ! result.done ) {
    println 'waiting...'
    Thread.sleep(2000)
}

/* The Future instance contains whatever is returned from the response
   closure above; in this case the parsed HTML data: */
def html = result.get()
assert html instanceof groovy.util.slurpersupport.GPathResult

In practice, it is very similar to the Ajax.Request class in Prototype.js.

This class also demonstrates the ease of which HTTPBuilder can be extended. The doRequest() method was simply overridden in order to execute requests from a ThreadPoolExecutor. You can see the full source code here.