FileDocCategorySizeDatePackage
IdleConnectionHandler.javaAPI DocAndroid 1.5 API6785Wed May 06 22:41:10 BST 2009org.apache.http.impl.conn

IdleConnectionHandler

public class IdleConnectionHandler extends Object
A helper class for connection managers to track idle connections.

This class is not synchronized.

see
org.apache.http.conn.ClientConnectionManager#closeIdleConnections
since
4.0

Fields Summary
private final Log
log
private final Map
connectionToTimes
Holds connections and the time they were added.
Constructors Summary
public IdleConnectionHandler()

    

      
        super();
        connectionToTimes = new HashMap<HttpConnection,TimeValues>();
    
Methods Summary
public voidadd(org.apache.http.HttpConnection connection, long validDuration, java.util.concurrent.TimeUnit unit)
Registers the given connection with this handler. The connection will be held until {@link #remove} or {@link #closeIdleConnections} is called.

param
connection the connection to add
see
#remove

        
        Long timeAdded = Long.valueOf(System.currentTimeMillis());
        
        if (log.isDebugEnabled()) {
            log.debug("Adding connection at: " + timeAdded);
        }
        
        connectionToTimes.put(connection, new TimeValues(timeAdded, validDuration, unit));
    
public voidcloseExpiredConnections()

        long now = System.currentTimeMillis();
        if (log.isDebugEnabled()) {
            log.debug("Checking for expired connections, now: "  + now);
        }
        
        Iterator<HttpConnection> connectionIter =
            connectionToTimes.keySet().iterator();
        
        while (connectionIter.hasNext()) {
            HttpConnection conn = connectionIter.next();
            TimeValues times = connectionToTimes.get(conn);
            if(times.timeExpires <= now) {
                if (log.isDebugEnabled()) {
                    log.debug("Closing connection, expired @: "  + times.timeExpires);
                }
                connectionIter.remove();
                try {
                    conn.close();
                } catch (IOException ex) {
                    log.debug("I/O error closing connection", ex);
                }
            }
        }        
    
public voidcloseIdleConnections(long idleTime)
Closes connections that have been idle for at least the given amount of time.

param
idleTime the minimum idle time, in milliseconds, for connections to be closed

        
        // the latest time for which connections will be closed
        long idleTimeout = System.currentTimeMillis() - idleTime;

        if (log.isDebugEnabled()) {
            log.debug("Checking for connections, idleTimeout: "  + idleTimeout);
        }
        
        Iterator<HttpConnection> connectionIter =
            connectionToTimes.keySet().iterator();
        
        while (connectionIter.hasNext()) {
            HttpConnection conn = connectionIter.next();
            TimeValues times = connectionToTimes.get(conn);
            Long connectionTime = times.timeAdded;
            if (connectionTime.longValue() <= idleTimeout) {
                if (log.isDebugEnabled()) {
                    log.debug("Closing connection, connection time: "  + connectionTime);
                }
                connectionIter.remove();
                try {
                    conn.close();
                } catch (IOException ex) {
                    log.debug("I/O error closing connection", ex);
                }
            }
        }
    
public booleanremove(org.apache.http.HttpConnection connection)
Removes the given connection from the list of connections to be closed when idle. This will return true if the connection is still valid, and false if the connection should be considered expired and not used.

param
connection
return
True if the connection is still valid.

        TimeValues times = connectionToTimes.remove(connection);
        if(times == null) {
            log.warn("Removing a connection that never existed!");
            return true;
        } else {
            return System.currentTimeMillis() <= times.timeExpires;
        }
    
public voidremoveAll()
Removes all connections referenced by this handler.

        this.connectionToTimes.clear();