我正在使用 AsyncHTTPBuilder (v0.5.1) 但是,我无法让它工作,所以它异步执行请求。请检查下面的代码。看起来所有请求都是从同一个线程完成的:
@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()
}
输出:..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-线程 1 池 3 线程 1 池 3 线程 1 池 3 线程 1 池 3 线程 1 池 3 线程 1 池 3 线程 1 池 3 线程-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 池 3 线程 1 池 3 线程 1 池 3 线程 1 池 3 线程 1 池 3 线程 1 池 3 线程 1 池 3 线程 1池 3 线程 1 池 3 线程 1