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 package org.apache.commons.httpclient.util;
30
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34
35 import org.apache.commons.httpclient.HttpConnectionManager;
36
37 /***
38 * A utility class for periodically closing idle connections.
39 *
40 * @see org.apache.commons.httpclient.HttpConnectionManager#closeIdleConnections(long)
41 *
42 * @since 3.0
43 */
44 public class IdleConnectionTimeoutThread extends Thread {
45
46 private List connectionManagers = new ArrayList();
47
48 private boolean shutdown = false;
49
50 private long timeoutInterval = 1000;
51
52 private long connectionTimeout = 3000;
53
54 public IdleConnectionTimeoutThread() {
55 setDaemon(true);
56 }
57
58 /***
59 * Adds a connection manager to be handled by this class.
60 * {@link HttpConnectionManager#closeIdleConnections(long)} will be called on the connection
61 * manager every {@link #setTimeoutInterval(long) timeoutInterval} milliseconds.
62 *
63 * @param connectionManager The connection manager to add
64 */
65 public synchronized void addConnectionManager(HttpConnectionManager connectionManager) {
66 if (shutdown) {
67 throw new IllegalStateException("IdleConnectionTimeoutThread has been shutdown");
68 }
69 this.connectionManagers.add(connectionManager);
70 }
71
72 /***
73 * Removes the connection manager from this class. The idle connections from the connection
74 * manager will no longer be automatically closed by this class.
75 *
76 * @param connectionManager The connection manager to remove
77 */
78 public synchronized void removeConnectionManager(HttpConnectionManager connectionManager) {
79 if (shutdown) {
80 throw new IllegalStateException("IdleConnectionTimeoutThread has been shutdown");
81 }
82 this.connectionManagers.remove(connectionManager);
83 }
84
85 /***
86 * Closes idle connections.
87 */
88 public synchronized void run() {
89 while (!shutdown) {
90 Iterator iter = connectionManagers.iterator();
91
92 while (iter.hasNext()) {
93 HttpConnectionManager connectionManager = (HttpConnectionManager) iter.next();
94 connectionManager.closeIdleConnections(connectionTimeout);
95 }
96
97 try {
98 this.wait(timeoutInterval);
99 } catch (InterruptedException e) {
100 }
101 }
102
103 this.connectionManagers.clear();
104 }
105
106 /***
107 * Stops the thread used to close idle connections. This class cannot be used once shutdown.
108 */
109 public synchronized void shutdown() {
110 this.shutdown = true;
111 }
112
113 /***
114 * Sets the timeout value to use when testing for idle connections.
115 *
116 * @param connectionTimeout The connection timeout in milliseconds
117 *
118 * @see HttpConnectionManager#closeIdleConnections(long)
119 */
120 public void setConnectionTimeout(long connectionTimeout) {
121 if (shutdown) {
122 throw new IllegalStateException("IdleConnectionTimeoutThread has been shutdown");
123 }
124 this.connectionTimeout = connectionTimeout;
125 }
126 /***
127 * Sets the interval used by this class between closing idle connections. Idle
128 * connections will be closed every <code>timeoutInterval</code> milliseconds.
129 *
130 * @param timeoutInterval The timeout interval in milliseconds
131 */
132 public void setTimeoutInterval(long timeoutInterval) {
133 if (shutdown) {
134 throw new IllegalStateException("IdleConnectionTimeoutThread has been shutdown");
135 }
136 this.timeoutInterval = timeoutInterval;
137 }
138
139 }