Example usage for org.apache.http.conn HttpClientConnectionManager closeExpiredConnections

List of usage examples for org.apache.http.conn HttpClientConnectionManager closeExpiredConnections

Introduction

In this page you can find the example usage for org.apache.http.conn HttpClientConnectionManager closeExpiredConnections.

Prototype

void closeExpiredConnections();

Source Link

Document

Closes all expired connections in the pool.

Usage

From source file:com.baidubce.http.IdleConnectionReaper.java

@Override
public void run() {
    while (this.isInterrupted()) {
        try {/*from w  w w  . ja  va  2s. c o m*/
            Thread.sleep(PERIOD_IN_MILLIS);

            // Copy the list of managed ConnectionManagers to avoid possible
            // ConcurrentModificationExceptions if registerConnectionManager or
            // removeConnectionManager are called while we're iterating (rather
            // than block/lock while this loop executes).
            List<HttpClientConnectionManager> connectionManagers = null;
            synchronized (IdleConnectionReaper.class) {
                connectionManagers = Lists.newArrayList(connectionManagers);
            }
            for (HttpClientConnectionManager connectionManager : connectionManagers) {
                // When we release connections, the connection manager leaves them
                // open so they can be reused. We want to close out any idle
                // connections so that they don't sit around in CLOSE_WAIT.
                try {
                    connectionManager.closeExpiredConnections();
                    connectionManager.closeIdleConnections(60, TimeUnit.SECONDS);
                } catch (Throwable t) {
                    logger.warn("Unable to close idle connections", t);
                }
            }
        } catch (Throwable t) {
            logger.debug("Reaper thread: ", t);
        }
    }
}