IdleConnectionHandlerpublic class IdleConnectionHandler extends Object A helper class for connection managers to track idle connections.
This class is not synchronized. |
Fields Summary |
---|
private final Log | log | private final Map | connectionToTimesHolds connections and the time they were added. |
Constructors Summary |
---|
public IdleConnectionHandler()
super();
connectionToTimes = new HashMap<HttpConnection,TimeValues>();
|
Methods Summary |
---|
public void | add(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.
Long timeAdded = Long.valueOf(System.currentTimeMillis());
if (log.isDebugEnabled()) {
log.debug("Adding connection at: " + timeAdded);
}
connectionToTimes.put(connection, new TimeValues(timeAdded, validDuration, unit));
| public void | closeExpiredConnections()
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 void | closeIdleConnections(long idleTime)Closes connections that have been idle for at least the given amount of time.
// 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 boolean | remove(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.
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 void | removeAll()Removes all connections referenced by this handler.
this.connectionToTimes.clear();
|
|