View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/util/IdleConnectionTimeoutThread.java,v 1.2 2004/05/13 02:40:36 mbecke Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
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         // clear out the connection managers now that we're shutdown
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 }