View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/util/IdleConnectionTimeoutThread.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 19:29:41 +0100 (Sat, 22 Dec 2012) $
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  the License.  You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   *  Unless required by applicable law or agreed to in writing, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
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         // clear out the connection managers now that we're shutdown
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 }