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