1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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  /**
39   * A utility class for periodically closing idle connections.
40   * 
41   * @see org.apache.commons.httpclient.HttpConnectionManager#closeIdleConnections(long)
42   * 
43   * @since 3.0
44   */
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      /**
60       * Adds a connection manager to be handled by this class.  
61       * {@link HttpConnectionManager#closeIdleConnections(long)} will be called on the connection
62       * manager every {@link #setTimeoutInterval(long) timeoutInterval} milliseconds.
63       * 
64       * @param connectionManager The connection manager to add
65       */
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      /**
74       * Removes the connection manager from this class.  The idle connections from the connection
75       * manager will no longer be automatically closed by this class.
76       * 
77       * @param connectionManager The connection manager to remove
78       */
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      /**
87       * Handles calling {@link HttpConnectionManager#closeIdleConnections(long) closeIdleConnections()}
88       * and doing any other cleanup work on the given connection mangaer.
89       * @param connectionManager The connection manager to close idle connections for
90       */
91      protected void handleCloseIdleConnections(HttpConnectionManager connectionManager) {
92          connectionManager.closeIdleConnections(connectionTimeout);
93      }
94      
95      /**
96       * Closes idle connections.
97       */
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         
113         this.connectionManagers.clear();
114     }
115     
116     /**
117      * Stops the thread used to close idle connections.  This class cannot be used once shutdown.
118      */
119     public synchronized void shutdown() {
120         this.shutdown = true;
121         this.notifyAll();
122     }
123     
124     /**
125      * Sets the timeout value to use when testing for idle connections.
126      * 
127      * @param connectionTimeout The connection timeout in milliseconds
128      * 
129      * @see HttpConnectionManager#closeIdleConnections(long)
130      */
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     /**
138      * Sets the interval used by this class between closing idle connections.  Idle 
139      * connections will be closed every <code>timeoutInterval</code> milliseconds.
140      *  
141      * @param timeoutInterval The timeout interval in milliseconds
142      */
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 }