Beispiel #1
0
 /**
  * Closes the connection if stale.
  *
  * @return <code>true</code> if the connection was stale and therefore closed, <code>false</code>
  *     otherwise.
  * @see #isStale()
  * @since 3.0
  */
 public boolean closeIfStale() throws IOException {
   if (isOpen && isStale()) {
     LOG.debug("Connection is stale, closing...");
     close();
     return true;
   }
   return false;
 }
 /**
  * Since the same connection is about to be reused, make sure the previous request was completely
  * processed, and if not consume it now.
  *
  * @param conn The connection
  */
 static void finishLastResponse(HttpConnection conn) {
   InputStream lastResponse = conn.getLastResponseInputStream();
   if (lastResponse != null) {
     conn.setLastResponseInputStream(null);
     try {
       lastResponse.close();
     } catch (IOException ioe) {
       // FIXME: badness - close to force reconnect.
       conn.close();
     }
   }
 }
  /**
   * Closes connections that have been idle for at least the given amount of time.
   *
   * @param idleTime the minimum idle time, in milliseconds, for connections to be closed
   */
  public void closeIdleConnections(long idleTime) {

    // the latest time for which connections will be closed
    long idleTimeout = System.currentTimeMillis() - idleTime;

    if (LOG.isDebugEnabled()) {
      LOG.debug("Checking for connections, idleTimeout: " + idleTimeout);
    }

    Iterator connectionIter = connectionToAdded.keySet().iterator();

    while (connectionIter.hasNext()) {
      HttpConnection conn = (HttpConnection) connectionIter.next();
      Long connectionTime = (Long) connectionToAdded.get(conn);
      if (connectionTime.longValue() <= idleTimeout) {
        if (LOG.isDebugEnabled()) {
          LOG.debug("Closing connection, connection time: " + connectionTime);
        }
        connectionIter.remove();
        conn.close();
      }
    }
  }
  public void testDeleteClosedConnections() {

    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();

    HttpConnection conn = manager.getConnection(client.getHostConfiguration());

    assertEquals("connectionsInPool", manager.getConnectionsInPool(), 1);
    assertEquals(
        "connectionsInPool(host)", manager.getConnectionsInPool(client.getHostConfiguration()), 1);

    conn.close();
    conn.releaseConnection();

    assertEquals("connectionsInPool", manager.getConnectionsInPool(), 1);
    assertEquals(
        "connectionsInPool(host)", manager.getConnectionsInPool(client.getHostConfiguration()), 1);

    manager.deleteClosedConnections();

    assertEquals("connectionsInPool", manager.getConnectionsInPool(), 0);
    assertEquals(
        "connectionsInPool(host)", manager.getConnectionsInPool(client.getHostConfiguration()), 0);
  }
  public HttpConnection getConnection(HostConfiguration hostConfiguration) {

    // make sure the host and proxy are correct for this connection
    // close it and set the values if they are not
    if (!hostConfiguration.hostEquals(connection) || !hostConfiguration.proxyEquals(connection)) {

      if (connection.isOpen()) {
        connection.close();
      }

      connection.setHost(hostConfiguration.getHost());
      connection.setPort(hostConfiguration.getPort());
      connection.setProtocol(hostConfiguration.getProtocol());
      connection.setLocalAddress(hostConfiguration.getLocalAddress());

      connection.setProxyHost(hostConfiguration.getProxyHost());
      connection.setProxyPort(hostConfiguration.getProxyPort());
    } else {
      finishLastResponse(connection);
    }

    connectionReleased = false;
    return connection;
  }