org.apache.http.conn.ConnectionPoolTimeoutException:等待池httpClient连接超时

时间:2018-08-10 01:09:57

标签: c#

当尝试使用Httpclient连接到另一个应用程序时,我们的应用程序中出现低于ERROR的堆

  

org.apache.http.conn.ConnectionPoolTimeoutException:等待超时   用于从池连接   org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:286)     在   org.apache.http.impl.conn.PoolingHttpClientConnectionManager $ 1.get(PoolingHttpClientConnectionManager.java:263)     在   org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:190)     在   org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)     在   org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)     在   org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)     在   org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)     在   org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)     在   org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)

我正在使用的代码如下

...
    public static final String ACCEPT_HEADER = "application/json";

    public static final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000)
            .setConnectionRequestTimeout(1000).setConnectTimeout(1000).build();


    public static PoolingHttpClientConnectionManager connectionManager = null;

    static {
        connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(500);
        connectionManager.setDefaultMaxPerRoute(100);
    }

    ....
    public static String doSomething(params ...) throws IOException {
        String urlString = "<Some valid URL>";

        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(connectionManager)
                .setConnectionManagerShared(true).build();

        try {
            HttpGet getRequest = new HttpGet(urlString);

            getRequest.setHeader(HttpHeaders.ACCEPT, ACCEPT_HEADER);
            getRequest.setHeader(HttpHeaders.ACCEPT_CHARSET, StandardCharsets.UTF_8.name());
            getRequest.setHeader(HttpHeaders.ORIGIN, "https:<some_valid_origin>");
            getRequest.setHeader(HttpHeaders.AUTHORIZATION, accessToken);

            getRequest.setConfig(requestConfig);

            CloseableHttpResponse response = httpClient.execute(getRequest);

            if (response != null && response.getStatusLine() != null && response.getStatusLine().getStatusCode() == 200) {

                HttpEntity responseEntity = response.getEntity();
                String responseJson = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
                // For making sure to release the connection
                EntityUtils.consumeQuietly(responseEntity);

                <do something else>

            } else {
                log.error("failed");
                throw new RuntimeException("failed");
            }

        } finally {
            httpClient.close();
        }
    }

    ....

有人可以帮助找出导致此问题的原因吗?是由于某些连接泄漏还是由于其他应用程序超时导致的?

请提供一些线索以寻求解决方案,因为这与我们的产品系统有关。

0 个答案:

没有答案