有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Apache异步HttpClient不快速

我对ApacheHTTP客户端相当陌生,正在尝试从网站获取状态代码。在ApacheHTTP教程中找到以下示例

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

}

它是真正的异步调用还是串行调用。必须做些什么才能使调用异步且更快


共 (2) 个答案

  1. # 1 楼答案

    首先也是最重要的:CloseableHttpAsyncClient实例非常昂贵。请不要为每个请求创建一个新的CloseableHttpAsyncClient。这就像为每次点击链接创建一个新的浏览器过程,完全是浪费,而且非常缓慢。强烈建议在逻辑组件的整个生命周期内使用相同的CloseableHttpAsyncClient实例

    几乎在所有情况下,阻塞客户机都可能比非阻塞(基于NIO)客户机快得多(只要并发请求数低于1000)。除非您正在构建某种类型的代理,否则使用诸如Apache HttpClient之类的阻塞HTTP客户端可能会更好

  2. # 2 楼答案

    由于请求通过相同的路径,因此必须进行以下更改

    CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .setMaxConnPerRoute(1000)
                .setMaxConnTotal(1000)
                .build();