Java利用httpasyncclient进行异步HTTP请求(二)

2014-11-23 21:26:37 · 作者: · 浏览: 33
://www.verisign.com/"),
new HttpGet("http://www.google.com/"),
new HttpGet("http://www.baidu.com/") };
final CountDownLatch latch = new CountDownLatch(requests.length);
for (final HttpGet request : requests) {
httpclient.execute(request, new FutureCallback() {
//无论完成还是失败都调用countDown()
@Override
public void completed(final HttpResponse response) {
latch.countDown();
System.out.println(request.getRequestLine() + "->"
+ response.getStatusLine());
}
@Override
public void failed(final Exception ex) {
latch.countDown();
System.out.println(request.getRequestLine() + "->" + ex);
}
@Override
public void cancelled() {
latch.countDown();
System.out.println(request.getRequestLine()
+ " cancelled");
}
});
}
latch.await();
System.out.println("Shutting down");
} finally {
httpclient.close();
}
System.out.println("Done");
}
}


参考文档:http://hc.apache.org/httpcomponents-asyncclient-dev/examples.html