Java Apache异步HttpClient速度不快
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21727604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache async HttpClient not fast
提问by Harshit
I am pretty new to Apache http client and am trying to get status code from a website. Found the following example on Apache http tutorial.
我对 Apache http 客户端很陌生,正在尝试从网站获取状态代码。在 Apache http 教程中找到了以下示例。
import java.util.concurrent.CountDownLatch;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
public class Abc {
static long d2;
public static void main(final String[] args) throws Exception {
d2=System.currentTimeMillis();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(3000)
.setConnectTimeout(3000).build();
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();
try {
httpclient.start();
final HttpGet[] requests = new HttpGet[] {
new HttpGet("http://192.168.26.175:8080/examples/eye/abc10000.jsp")
};
final CountDownLatch latch = new CountDownLatch(1);
for (int v=0;v<1000;v++) {
httpclient.execute(requests[0], new FutureCallback<HttpResponse>() {
public void completed(final HttpResponse response) {
latch.countDown();
System.out.println(requests[0].getRequestLine() + "->" + response.getStatusLine());
}
public void failed(final Exception ex) {
latch.countDown();
System.out.println(requests[0].getRequestLine() + "->" + ex);
}
public void cancelled() {
latch.countDown();
System.out.println(requests[0].getRequestLine() + " cancelled");
}
});
}
latch.await();
System.out.println("Shutting down");
} finally {
httpclient.close();
}
System.out.println("Done");
long d1=System.currentTimeMillis();
System.out.println(d1-d2);
}
}
Is it really asynchronous or are the calls made serially. What has to be done to make calls asynchronous and faster.
它真的是异步的还是串行调用的。必须做什么才能使调用异步且更快。
采纳答案by Harshit
As the requests are going through the same route following changes will have to be made.
由于请求通过相同的路线,因此必须进行以下更改。
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(requestConfig)
.setMaxConnPerRoute(1000)
.setMaxConnTotal(1000)
.build();
回答by ok2c
First and foremost: CloseableHttpAsyncClient
instances are very expensive. Please do NOTcreate a new CloseableHttpAsyncClient
for each and every request. It is like creating a new browser process for each link click, is completely wasteful and is very slow. It is strongly recommended to use the same CloseableHttpAsyncClient
instance for the entire lifespan of a logical component.
首先也是最重要的:CloseableHttpAsyncClient
实例非常昂贵。请不要不创建一个新CloseableHttpAsyncClient
的每一个请求。就像为每次链接点击创建一个新的浏览器进程一样,完全是浪费而且非常慢。强烈建议在CloseableHttpAsyncClient
逻辑组件的整个生命周期内使用相同的实例。
Pretty much in all cases a blocking client is likely to be considerably faster than a non-blocking (NIO based) one (as long as the number of concurrent requests is below, say, 1000). Unless you are building a proxy of some sort you might be much better served by a blocking HTTP client such as Apache HttpClient.
几乎在所有情况下,阻塞客户端可能比非阻塞(基于 NIO)客户端快得多(只要并发请求的数量低于 1000)。除非您正在构建某种代理,否则阻塞 HTTP 客户端(例如 Apache HttpClient)可能会更好地为您提供服务。