View Javadoc

    1   /*
    2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/util/IdleConnectionHandler.java,v 1.2 2004/05/13 02:40:36 mbecke Exp $
    3    * $Revision: 480424 $
    4    * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    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.HashMap;
    33  import java.util.Iterator;
    34  import java.util.Map;
    35
    36  import org.apache.commons.httpclient.HttpConnection;
    37  import org.apache.commons.logging.Log;
    38  import org.apache.commons.logging.LogFactory;
    39
    40  /**
    41   * A helper class for connection managers to track idle connections.
    42   *
    43   * <p>This class is not synchronized.</p>
    44   *
    45   * @see org.apache.commons.httpclient.HttpConnectionManager#closeIdleConnections(long)
    46   *
    47   * @since 3.0
    48   */
    49  public class IdleConnectionHandler {
    50
    51      private static final Log LOG = LogFactory.getLog(IdleConnectionHandler.class);
    52
    53      /** Holds connections and the time they were added. */
    54      private Map connectionToAdded = new HashMap();
    55
    56      /**
    57       *
    58       */
    59      public IdleConnectionHandler() {
    60          super();
    61      }
    62
    63      /**
    64       * Registers the given connection with this handler.  The connection will be held until
    65       * {@link #remove(HttpConnection)} or {@link #closeIdleConnections(long)} is called.
    66       *
    67       * @param connection the connection to add
    68       *
    69       * @see #remove(HttpConnection)
    70       */
    71      public void add(HttpConnection connection) {
    72
    73          Long timeAdded = new Long(System.currentTimeMillis());
    74
    75          if (LOG.isDebugEnabled()) {
    76              LOG.debug("Adding connection at: " + timeAdded);
    77          }
    78
    79          connectionToAdded.put(connection, timeAdded);
    80      }
    81
    82      /**
    83       * Removes the given connection from the list of connections to be closed when idle.
    84       * @param connection
    85       */
    86      public void remove(HttpConnection connection) {
    87          connectionToAdded.remove(connection);
    88      }
    89
    90      /**
    91       * Removes all connections referenced by this handler.
    92       */
    93      public void removeAll() {
    94          this.connectionToAdded.clear();
    95      }
    96
    97      /**
    98       * Closes connections that have been idle for at least the given amount of time.
    99       *
    100      * @param idleTime the minimum idle time, in milliseconds, for connections to be closed
    101      */
    102     public void closeIdleConnections(long idleTime) {
    103
    104         // the latest time for which connections will be closed
    105         long idleTimeout = System.currentTimeMillis() - idleTime;
    106
    107         if (LOG.isDebugEnabled()) {
    108             LOG.debug("Checking for connections, idleTimeout: "  + idleTimeout);
    109         }
    110
    111         Iterator connectionIter = connectionToAdded.keySet().iterator();
    112
    113         while (connectionIter.hasNext()) {
    114             HttpConnection conn = (HttpConnection) connectionIter.next();
    115             Long connectionTime = (Long) connectionToAdded.get(conn);
    116             if (connectionTime.longValue() <= idleTimeout) {
    117                 if (LOG.isDebugEnabled()) {
    118                     LOG.debug("Closing connection, connection time: "  + connectionTime);
    119                 }
    120                 connectionIter.remove();
    121                 conn.close();
    122             }
    123         }
    124     }
    125 }