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

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

Introduction

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

Prototype

void closeIdleConnections(long idletime, TimeUnit tunit);

Source Link

Document

Closes idle connections in the pool.

Usage

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

@SuppressWarnings("unchecked")
@Override/*w  w  w .jav a  2  s. co  m*/
public void run() {
    while (true) {
        if (shuttingDown) {
            log.debug("Shutting down reaper thread.");
            return;
        }
        try {
            Thread.sleep(PERIOD_MILLISECONDS);

            // 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 = (List<HttpClientConnectionManager>) IdleConnectionReaper.connectionManagers
                        .clone();
            }
            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.closeIdleConnections(60, TimeUnit.SECONDS);
                } catch (Exception t) {
                    log.warn("Unable to close idle connections", t);
                }
            }
        } catch (Throwable t) {
            log.debug("Reaper thread: ", t);
        }
    }
}

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

@Override
public void run() {
    while (this.isInterrupted()) {
        try {/*from  w  w w.  ja v  a2 s .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);
        }
    }
}