I'm playing with AsyncHTTPBuilder (v0.5.1) however, I can't get it working so it's execute requests asynchronously. Please, check the code below. Looks like all requests are done from the same thread:
@Test public void testPoolsizeAndQueueing() {
def http = new AsyncHTTPBuilder( poolSize : 5 ,
uri : 'http://ajax.googleapis.com/ajax/services/search/web' )
def responses = []
/* With one thread in the pool, responses will be sequential but should
* queue up w/o being rejected. */
10.times {
responses << http.get( query : [q:'Groovy', v:'1.0'] ) { return Thread.currentThread().name }
responses << http.get( query : [q:'Ruby', v:'1.0'] ) { return Thread.currentThread().name }
responses << http.get( query : [q:'Scala', v:'1.0'] ) { return Thread.currentThread().name }
}
def timeout = 60000
def time = 0
while ( true ) {
if ( responses.every{ it.done ? it.get() : 0 } ) break
print '.'
Thread.sleep 2000
time += 2000
if ( time > timeout ) assert false
}
responses.each { println it.get() }
http.shutdown()
}
Output: ..pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1 pool-3-thread-1
I believe this is as expected.
The queries are being run in separate threads asynchronously, but the { return Thread.currentThread().name }
closure is getting run when it.get()
is called (in the same thread as the main script is running)
Hope this explains it
EDIT
You're right.
Running with
def http = new AsyncHTTPBuilder( poolsize:5,
uri:'http://ajax.googleapis.com/ajax/services/search/web' )
doesn't work as expected...and changing it to:
def http = new AsyncHTTPBuilder( threadPool:java.util.concurrent.Executors.newFixedThreadPool( 5 ),
uri:'http://ajax.googleapis.com/ajax/services/search/web' )
makes it work. Even tried with 0.5.2-SNAPSHOT, but got the same issue
I have reported it as a bug: http://jira.codehaus.org/browse/GMOD-174
Fingers crossed it will get fixed (or we'll find out why we're doing it wrong) by v 0.5.2