微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在使用PoolingHttpClientConnectionManager时,第一次调用总是很慢吗?

如何解决在使用PoolingHttpClientConnectionManager时,第一次调用总是很慢吗?

我正在使用PoolingHttpClientConnectionManager,但是第一次调用url时,我的响应速度为1000ms,但是下一次调用时间减少为200ms,300ms,500ms?

init:

PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
// Increase max total connection
connManager.setMaxTotal(maxTotalConnections);
// Increase default max connection per route
connManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);

// config timeout
RequestConfig config = RequestConfig.custom().setConnectTimeout(connectTimeout)
        .setConnectionRequestTimeout(waitTimeout).setSocketTimeout(readTimeout).build();

String proxyH = System.getProperty("https.proxyHost");
int proxyP = Integer.parseInt(System.getProperty("https.proxyPort"));

CloseableHttpClient httpClient = HttpClients.custom().setKeepAliveStrategy(keepAliveStrategy).setConnectionManager(connManager)
            .setDefaultRequestConfig(config).setProxy(new HttpHost(proxyH,proxyP)).build();

发送doPost:

    try {
        StringEntity requestEntity = new StringEntity(this.reqData,DEFAULT_CHARSET);
        postMethod = new HttpPost(url);
        postMethod.setEntity(requestEntity);
        response = execute(postMethod);

        int statusCode = response.getStatusLine().getStatusCode();
        SimpleHttpResponse simpleResponse = new SimpleHttpResponse();
        simpleResponse.setStatusCode(statusCode);
        httpentity entity = response.getEntity();
        if (entity != null) {
            String ctype = "application/json; charset=UTF-8";
            String charset = getResponseCharset(ctype);
            String content = EntityUtils.toString(entity,charset);

            simpleResponse.setContent(content);
        }
        EntityUtils.consume(entity);
        String respData = simpleResponse.getContent();
    } catch (Exception e) {
        e.printstacktrace();
    } finally {
        postMethod.releaseConnection();
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                e.printstacktrace();
            }
        }
    }

感谢查看。请帮帮我!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。