-1

I am using CloseableHttpAsyncClient API of apache to hit multiple requests without caring for response.

My code :-

//Setting HTTP client :-

 IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
                .setIoThreadCount(Runtime.getRuntime().availableProcessors())
                .setConnectTimeout(connectTimeOut)
                .setSoTimeout(socketTimeOut)
                .build();

ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);

PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
connManager.setMaxTotal(maxTotalConnection);

CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
        .setConnectionManager(connManager)
        .setDefaultCredentialsProvider(provider)
        .build();


httpclient.start();

//Hit multiple request :- 
int totalRequestCount  = 1500;
for(int i = 1; i < totalRequestCount : i++) {
    final HttpPost postRequest = new HttpPost(url);

    StringEntity stringEntity;
    try {
        stringEntity = new StringEntity(requestString);
        stringEntity.setContentType("application/json");
        postRequest.setEntity(stringEntity);
    } catch (UnsupportedEncodingException e) {
    }

    long startTime = System.currentTimeMillis();

    httpclient.execute(postRequest, new FutureCallback<HttpResponse>() {

        public void completed(final HttpResponse response) {
            long endTime = System.currentTimeMillis();
            long duation = endTime - startTime;
        }

        public void failed(final Exception ex) {
            postRequest.releaseConnection();
        }

        public void cancelled() {
            postRequest.releaseConnection();
        }
    });
}

But duration in multiple request in not coming properly. Any suggestion ?

Rahul Jain
  • 658
  • 5
  • 20
  • I think you shouldn't releaseConnection from apache 4.x. I would assume durations are not coming because you are not consuming response entity. This can be done by EntityUtils.consume(httpResponse.getEntity()); . I think more detailed answer can be found here: https://stackoverflow.com/questions/30889984/whats-the-difference-between-closeablehttpresponse-close-and-httppost-release – DonatasD Sep 18 '17 at 09:38
  • 1
    What do you mean by duration "not coming properly"? What do you expect and what do you get? – algrid Sep 18 '17 at 12:13
  • Typo: duation → duration – Cœur Jul 09 '19 at 07:34
  • @Rahul Jain Does the http request send out? – Lin Chen Dec 26 '19 at 13:13

1 Answers1

0

If you don't wait for all of the responses to arrive, you cannot measure how long it takes for them to all arrive.

You need to provide a non-dummy implementation for FutureCallback that (at least) records that a reply has been received. Then you need to wait for the replies.

When you do that, you will also need to deal with the possibility that some requests may be dropped at the server end.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216