javahttprequestasynchttpclientapache-httpasyncclient

Apache HttpAsynClient not sending multiple requests asynchronously


Following the Apache HttpAsyncClient example here, the HTTP GET requests are not all being fired at once but rather (mostly) synchronously.

The image below shows the requests coming in the same order it was sent (except for one). This remains true when increasing the number of requests.

enter image description here

I have used another library (AsynHttpClient) and the requests were sent much faster and in random order.

Is there any way to improve this code to truly execute asynchronously?

I've added the code used for reference.

public static void main(final String[] args) throws Exception {
    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://httpbin.org/ip"),
                new HttpGet("https://httpbin.org/ip"),
                new HttpGet("http://httpbin.org/headers")
        };
        final CountDownLatch latch = new CountDownLatch(requests.length);
        for (final HttpGet request: requests) {
            httpclient.execute(request, new FutureCallback<HttpResponse>() {

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

Solution

  • I think bumping the setMaxConnPerRoute and setMaxConnTotal might help since this will increase the max number of used connections

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