1 30 package org.apache.commons.httpclient.util; 31 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 36 import org.apache.commons.httpclient.HttpConnection; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 49 public class IdleConnectionHandler { 50 51 private static final Log LOG = LogFactory.getLog(IdleConnectionHandler.class); 52 53 54 private Map connectionToAdded = new HashMap (); 55 56 59 public IdleConnectionHandler() { 60 super(); 61 } 62 63 71 public void add(HttpConnection connection) { 72 73 Long timeAdded = new Long (System.currentTimeMillis()); 74 75 if (LOG.isDebugEnabled()) { 76 LOG.debug("Adding connection at: " + timeAdded); 77 } 78 79 connectionToAdded.put(connection, timeAdded); 80 } 81 82 86 public void remove(HttpConnection connection) { 87 connectionToAdded.remove(connection); 88 } 89 90 93 public void removeAll() { 94 this.connectionToAdded.clear(); 95 } 96 97 102 public void closeIdleConnections(long idleTime) { 103 104 long idleTimeout = System.currentTimeMillis() - idleTime; 106 107 if (LOG.isDebugEnabled()) { 108 LOG.debug("Checking for connections, idleTimeout: " + idleTimeout); 109 } 110 111 Iterator connectionIter = connectionToAdded.keySet().iterator(); 112 113 while (connectionIter.hasNext()) { 114 HttpConnection conn = (HttpConnection) connectionIter.next(); 115 Long connectionTime = (Long ) connectionToAdded.get(conn); 116 if (connectionTime.longValue() <= idleTimeout) { 117 if (LOG.isDebugEnabled()) { 118 LOG.debug("Closing connection, connection time: " + connectionTime); 119 } 120 connectionIter.remove(); 121 conn.close(); 122 } 123 } 124 } 125 } 126 | Popular Tags |