2
 private void init() {
   #Reactor 
   ioReactorConfig = IOReactorConfig.custom()                    
          .setIoThreadCount(Runtime.getRuntime().availableProcessors())
                            .setConnectTimeout(30000)
                            .setSoTimeout(30000)
                            .build();

   try {
       ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
   } catch (IOReactorException e) {
       e.printStackTrace();
       //TODO handle exception
    }
    connManager = new PoolingNHttpClientConnectionManager(ioReactor);
    httpClient = HttpAsyncClients.custom().setConnectionManager(connManager).build();

}

  private  ZCResponse httPost(URI uri, Object object,List<NameValuePair> params, Map<String,String> headers) {
        HttpPost postRequest = new HttpPost(uri);
        HttpResponse httpResponse = null;
        try {
            addHeaders(postRequest,headers);
            addPostParams(postRequest,object,params);
            Future<HttpResponse> futureResponse = httpClient.execute(postRequest, null);
            httpResponse = futureResponse.get();
            response = **readResponse(httpResponse);**
   }

private  String readResponse(HttpResponse response) throws IOException {
            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));

            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
            return result.toString();
        }

I have the following doubts about the code using Apache Http Async client

  1. What is the role of reactor with NPoolingConnectionManager.
  2. Currently, the response body is read from from post request's stream.And not using NIO or non-blocking way of reading the response body.Is it the right way.
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jily
  • 344
  • 2
  • 11

0 Answers0