Example usage for org.apache.http.impl.conn.tsccm BasicPoolEntry isExpired

List of usage examples for org.apache.http.impl.conn.tsccm BasicPoolEntry isExpired

Introduction

In this page you can find the example usage for org.apache.http.impl.conn.tsccm BasicPoolEntry isExpired.

Prototype

public boolean isExpired(final long now) 

Source Link

Usage

From source file:org.apache.http.impl.conn.tsccm.ConnPoolByRoute.java

/**
 * If available, get a free pool entry for a route.
 *
 * @param rospl       the route-specific pool from which to get an entry
 *
 * @return  an available pool entry for the given route, or
 *          <code>null</code> if none is available
 *///from   w ww.j ava2s  .c om
protected BasicPoolEntry getFreeEntry(final RouteSpecificPool rospl, final Object state) {

    BasicPoolEntry entry = null;
    poolLock.lock();
    try {
        boolean done = false;
        while (!done) {

            entry = rospl.allocEntry(state);

            if (entry != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Getting free connection" + " [" + rospl.getRoute() + "][" + state + "]");

                }
                freeConnections.remove(entry);
                if (entry.isExpired(System.currentTimeMillis())) {
                    // If the free entry isn't valid anymore, get rid of it
                    // and loop to find another one that might be valid.
                    if (log.isDebugEnabled()) {
                        log.debug("Closing expired free connection" + " [" + rospl.getRoute() + "][" + state
                                + "]");
                    }
                    closeConnection(entry);
                    // We use dropEntry instead of deleteEntry because the entry
                    // is no longer "free" (we just allocated it), and deleteEntry
                    // can only be used to delete free entries.
                    rospl.dropEntry();
                    numConnections--;
                } else {
                    leasedConnections.add(entry);
                    done = true;
                }

            } else {
                done = true;
                if (log.isDebugEnabled()) {
                    log.debug("No free connections" + " [" + rospl.getRoute() + "][" + state + "]");
                }
            }
        }
    } finally {
        poolLock.unlock();
    }
    return entry;
}

From source file:org.apache.http.impl.conn.tsccm.ConnPoolByRoute.java

@Override
public void closeExpiredConnections() {
    log.debug("Closing expired connections");
    final long now = System.currentTimeMillis();

    poolLock.lock();/*  w  w  w .j  a v a 2s .c  o  m*/
    try {
        final Iterator<BasicPoolEntry> iter = freeConnections.iterator();
        while (iter.hasNext()) {
            final BasicPoolEntry entry = iter.next();
            if (entry.isExpired(now)) {
                if (log.isDebugEnabled()) {
                    log.debug("Closing connection expired @ " + new Date(entry.getExpiry()));
                }
                iter.remove();
                deleteEntry(entry);
            }
        }
    } finally {
        poolLock.unlock();
    }
}