2

我正在使用 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

4

2 回答 2

3

我已经修复了这个错误并部署了一个新的 0.5.2-SNAPSHOT。所以它现在可以使用最新的快照。

我计划发布带有此修复程序的 0.5.2 最终版,并很快修复许多其他错误。

为了记录,命名参数实际上poolSize不是poolsize,但这不是问题的原因。(但这是我计划在 0.5.2 中修复的问题 - http://jira.codehaus.org/browse/GMOD-175。)

感谢 Tim Yates 报告了这个错误。

于 2011-03-10T00:14:00.093 回答
2

我相信这正如预期的那样。

查询在单独的线程中异步运行,但是在调用{ return Thread.currentThread().name }时闭包正在运行it.get()(在与主脚本运行的同一线程中)

希望这能解释

编辑

你是对的。

与运行

def http = new AsyncHTTPBuilder( poolsize:5,
                                 uri:'http://ajax.googleapis.com/ajax/services/search/web' )

没有按预期工作......并将其更改为:

def http = new AsyncHTTPBuilder( threadPool:java.util.concurrent.Executors.newFixedThreadPool( 5 ),
                                 uri:'http://ajax.googleapis.com/ajax/services/search/web' )

让它工作。甚至尝试使用 0.5.2-SNAPSHOT,但遇到了同样的问题

我已将其报告为错误:http: //jira.codehaus.org/browse/GMOD-174

手指交叉它将在 v 0.5.2 之前得到修复(或者我们会找出我们做错的原因)

于 2011-03-08T10:44:03.417 回答