1

Is there a way (and how) to know the status of a connection pool? Like, how many connections are being used, how many are available, ...

We are currently facing issues where the application cannot get a connection from the pool (ConnectionPoolTimeoutException: Timeout waiting for connection from pool) so to track down the cause we would like to log some pool stats each time a new connection is requested.

I have been browsing the Apache HTTPClient API but did not find a way to get this information.

We use PoolingClientConnectionManager.

2 Answers 2

3

You can use methods of the ConnPoolControl interface to control parameters of the internal pool

3

You can have a detailed information total and per route with the code below:

public static void main(String[] args) {
    PoolingHttpClientConnectionManager connectionManager = HttpClientUtils.getConnectionManager();
    System.out.println(createHttpInfo(connectionManager));
}

private static String createHttpInfo(PoolingHttpClientConnectionManager connectionManager) {
    StringBuilder sb = new StringBuilder();
    sb.append("=========================").append("\n");
    sb.append("General Info:").append("\n");
    sb.append("-------------------------").append("\n");
    sb.append("MaxTotal: ").append(connectionManager.getMaxTotal()).append("\n");
    sb.append("DefaultMaxPerRoute: ").append(connectionManager.getDefaultMaxPerRoute()).append("\n");
    sb.append("ValidateAfterInactivity: ").append(connectionManager.getValidateAfterInactivity()).append("\n");
    sb.append("=========================").append("\n");

    PoolStats totalStats = connectionManager.getTotalStats();
    sb.append(createPoolStatsInfo("Total Stats", totalStats));

    Set<HttpRoute> routes = connectionManager.getRoutes();

    if (routes != null) {
        for (HttpRoute route : routes) {
            sb.append(createRouteInfo(connectionManager, route));
        }
    }

    return sb.toString();
}

private static String createRouteInfo(PoolingHttpClientConnectionManager connectionManager, HttpRoute route) {
    PoolStats routeStats = connectionManager.getStats(route);
    String info = createPoolStatsInfo(route.getTargetHost().toURI(), routeStats);
    return info;
}

private static String createPoolStatsInfo(String title, PoolStats poolStats) {
    StringBuilder sb = new StringBuilder();

    sb.append(title + ":").append("\n");
    sb.append("-------------------------").append("\n");

    if (poolStats != null) {
        sb.append("Available: ").append(poolStats.getAvailable()).append("\n");
        sb.append("Leased: ").append(poolStats.getLeased()).append("\n");
        sb.append("Max: ").append(poolStats.getMax()).append("\n");
        sb.append("Pending: ").append(poolStats.getPending()).append("\n");
    }

    sb.append("=========================").append("\n");

    return sb.toString();
}

Update (2019-01-07)

The connection manager is retrieved from an utilitarian class I've created (you can create it differently):

public class HttpClientUtils {

    private static final PoolingHttpClientConnectionManager connectionManager = createConnectionManager();

    private static PoolingHttpClientConnectionManager createConnectionManager() {
        try {
            SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
                    SSLContext.getDefault(),
                    new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"},
                    null,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());
            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.INSTANCE)
                    .register("https", socketFactory)
                    .build();

            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
            cm.setMaxTotal(200);
            cm.setDefaultMaxPerRoute(20);

            return cm;
        } catch (NoSuchAlgorithmException | RuntimeException ex) {
            Logger.getLogger(HttpClientUtils.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public static PoolingHttpClientConnectionManager getConnectionManager() {
        return connectionManager;
    }
}
2
  • Looks like HttpClientUtils.getConnectionManager() is deprecated. Is there any other way?
    – Kriss
    Dec 14, 2018 at 15:44
  • @Kriss HttpClientUtils is a class I've created that instantiate the connection manager. I've updated the code with the class. I'm using httpclient-4.5.2 (from 2016, not so recent, but not so old; the last httpclient release that I see is 4.5.6). Jan 7, 2019 at 17:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.