1 30 package org.apache.commons.httpclient.util; 31 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 import org.apache.commons.httpclient.HttpConnectionManager; 37 38 45 public class IdleConnectionTimeoutThread extends Thread { 46 47 private List connectionManagers = new ArrayList (); 48 49 private boolean shutdown = false; 50 51 private long timeoutInterval = 1000; 52 53 private long connectionTimeout = 3000; 54 55 public IdleConnectionTimeoutThread() { 56 setDaemon(true); 57 } 58 59 66 public synchronized void addConnectionManager(HttpConnectionManager connectionManager) { 67 if (shutdown) { 68 throw new IllegalStateException ("IdleConnectionTimeoutThread has been shutdown"); 69 } 70 this.connectionManagers.add(connectionManager); 71 } 72 73 79 public synchronized void removeConnectionManager(HttpConnectionManager connectionManager) { 80 if (shutdown) { 81 throw new IllegalStateException ("IdleConnectionTimeoutThread has been shutdown"); 82 } 83 this.connectionManagers.remove(connectionManager); 84 } 85 86 91 protected void handleCloseIdleConnections(HttpConnectionManager connectionManager) { 92 connectionManager.closeIdleConnections(connectionTimeout); 93 } 94 95 98 public synchronized void run() { 99 while (!shutdown) { 100 Iterator iter = connectionManagers.iterator(); 101 102 while (iter.hasNext()) { 103 HttpConnectionManager connectionManager = (HttpConnectionManager) iter.next(); 104 handleCloseIdleConnections(connectionManager); 105 } 106 107 try { 108 this.wait(timeoutInterval); 109 } catch (InterruptedException e) { 110 } 111 } 112 this.connectionManagers.clear(); 114 } 115 116 119 public synchronized void shutdown() { 120 this.shutdown = true; 121 this.notifyAll(); 122 } 123 124 131 public synchronized void setConnectionTimeout(long connectionTimeout) { 132 if (shutdown) { 133 throw new IllegalStateException ("IdleConnectionTimeoutThread has been shutdown"); 134 } 135 this.connectionTimeout = connectionTimeout; 136 } 137 143 public synchronized void setTimeoutInterval(long timeoutInterval) { 144 if (shutdown) { 145 throw new IllegalStateException ("IdleConnectionTimeoutThread has been shutdown"); 146 } 147 this.timeoutInterval = timeoutInterval; 148 } 149 150 } 151 | Popular Tags |