简体   繁体   English

Apache异步HttpClient不快

[英]Apache async HttpClient not fast

I am pretty new to Apache http client and am trying to get status code from a website. 我是Apache http客户端的新手,我正试图从网站获取状态代码。 Found the following example on Apache http tutorial. 在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. 必须采取哪些措施才能使调用异步且速度更快。

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();

First and foremost: CloseableHttpAsyncClient instances are very expensive. 首先: CloseableHttpAsyncClient实例非常昂贵。 Please do NOT create a new CloseableHttpAsyncClient for each and every request. 不要创建一个新的CloseableHttpAsyncClient为每一个请求。 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实例。

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). 几乎在所有情况下,阻塞客户端可能比非阻塞(基于NIO)的客户端快得多(只要并发请求的数量低于1000,例如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. 除非您正在构建某种类型的代理,否则阻止HTTP客户端(如Apache HttpClient)可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM