You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by "shicheng31604@gmail.com" <sh...@gmail.com> on 2019/08/15 05:37:27 UTC

query response slowly on high concurrency condition

Hi:
    I use HttpClient4.x in one SpringBoot applicaion. In one component, there is only one shared instance of HttpClient. But when I  apply  it in one method on high cocurrency condition in witch the qps reaches about 200.
It takes a long time from the start of the calling method to the return of the result. I am definitely not a problem with computers and networks, because I use tools directly, such as PostMan, which takes only a few milliseconds.
    Any solutions?Thanks!

Bellow is key point of my code:
  private void check(){

        if(client==null ){
            ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
                @Override
                public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                    HeaderElementIterator it = new BasicHeaderElementIterator
                            (response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                    while (it.hasNext()) {
                        HeaderElement he = it.nextElement();
                        String param = he.getName();
                        String value = he.getValue();
                        if (value != null && param.equalsIgnoreCase
                                ("timeout")) {
                            return Long.parseLong(value) * 1000;
                        }
                    }
                    return 60 * 1000;
                }
            };

            PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
            manager.setMaxTotal(concurrentConfig.getHttpQueryMax());

//            manager.setDefaultMaxPerRoute(1000);
//            manager.setMaxPerRoute(new HttpRoute(new HttpHost("172.16.40.104:7070")),300);

            client=HttpClients.custom().setConnectionManager(manager).setKeepAliveStrategy(myStrategy).setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).build();
        }

    }
public String query(String url, HttpServletRequest request,String bodyString) throws Exception{
    String queryUrl=url+"/kylin/api/query";
    HttpPost post = new HttpPost(queryUrl);
    HttpUtil.copyPost(post,request, bodyString);
    check();
    try {

        long star = System.currentTimeMillis();
        HttpResponse response = client.execute(post);

        System.out.println(System.currentTimeMillis()-star);
        String resString = EntityUtils.toString(response.getEntity());


        return resString;


    } catch (Exception e) {
        e.printStackTrace();
       
    }finally {
        post.releaseConnection();
    }


}


shicheng31604@gmail.com