コード例 #1
0
  public void testCloseExpiredConnections() throws Exception {

    HttpParams mgrpar = defaultParams.copy();
    ConnManagerParams.setMaxTotalConnections(mgrpar, 1);

    SingleClientConnManager mgr = createSCCM(mgrpar, null);

    final HttpHost target = getServerHttp();
    final HttpRoute route = new HttpRoute(target, null, false);

    ManagedClientConnection conn = mgr.getConnection(route, null);
    conn.open(route, httpContext, defaultParams);
    mgr.releaseConnection(conn, 100, TimeUnit.MILLISECONDS);

    mgr.closeExpiredConnections();

    conn = mgr.getConnection(route, null);
    assertTrue(conn.isOpen());
    mgr.releaseConnection(conn, 100, TimeUnit.MILLISECONDS);

    Thread.sleep(150);
    mgr.closeExpiredConnections();
    conn = mgr.getConnection(route, null);
    assertFalse(conn.isOpen());

    mgr.shutdown();
  }
コード例 #2
0
  /**
   * Obtains a connection.
   *
   * @param route where the connection should point to
   * @return a connection that can be used to communicate along the given route
   */
  public ManagedClientConnection getConnection(final HttpRoute route, final Object state) {
    Args.notNull(route, "Route");
    assertStillUp();

    if (log.isDebugEnabled()) {
      log.debug("Get connection for route " + route);
    }

    synchronized (this) {
      Asserts.check(managedConn == null, MISUSE_MESSAGE);

      // check re-usability of the connection
      boolean recreate = false;
      boolean shutdown = false;

      // Kill the connection if it expired.
      closeExpiredConnections();

      if (uniquePoolEntry.connection.isOpen()) {
        final RouteTracker tracker = uniquePoolEntry.tracker;
        shutdown =
            (tracker == null
                || // can happen if method is aborted
                !tracker.toRoute().equals(route));
      } else {
        // If the connection is not open, create a new PoolEntry,
        // as the connection may have been marked not reusable,
        // due to aborts -- and the PoolEntry should not be reused
        // either.  There's no harm in recreating an entry if
        // the connection is closed.
        recreate = true;
      }

      if (shutdown) {
        recreate = true;
        try {
          uniquePoolEntry.shutdown();
        } catch (final IOException iox) {
          log.debug("Problem shutting down connection.", iox);
        }
      }

      if (recreate) {
        uniquePoolEntry = new PoolEntry();
      }

      managedConn = new ConnAdapter(uniquePoolEntry, route);

      return managedConn;
    }
  }