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

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

Introduction

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

Prototype

public Object getState() 

Source Link

Document

Returns the state object associated with this pool entry.

Usage

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

@Override
public void freeEntry(final BasicPoolEntry entry, final boolean reusable, final long validDuration,
        final TimeUnit timeUnit) {

    final HttpRoute route = entry.getPlannedRoute();
    if (log.isDebugEnabled()) {
        log.debug("Releasing connection" + " [" + route + "][" + entry.getState() + "]");
    }//from ww  w .j a v  a2s  . c  om

    poolLock.lock();
    try {
        if (shutdown) {
            // the pool is shut down, release the
            // connection's resources and get out of here
            closeConnection(entry);
            return;
        }

        // no longer issued, we keep a hard reference now
        leasedConnections.remove(entry);

        final RouteSpecificPool rospl = getRoutePool(route, true);

        if (reusable && rospl.getCapacity() >= 0) {
            if (log.isDebugEnabled()) {
                final String s;
                if (validDuration > 0) {
                    s = "for " + validDuration + " " + timeUnit;
                } else {
                    s = "indefinitely";
                }
                log.debug("Pooling connection" + " [" + route + "][" + entry.getState() + "]; keep alive " + s);
            }
            rospl.freeEntry(entry);
            entry.updateExpiry(validDuration, timeUnit);
            freeConnections.add(entry);
        } else {
            closeConnection(entry);
            rospl.dropEntry();
            numConnections--;
        }

        notifyWaitingThread(rospl);

    } finally {
        poolLock.unlock();
    }
}

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

/**
 * Deletes a given pool entry.//ww w . j a  v a2s . c o  m
 * This closes the pooled connection and removes all references,
 * so that it can be GCed.
 *
 * <p><b>Note:</b> Does not remove the entry from the freeConnections list.
 * It is assumed that the caller has already handled this step.</p>
 * <!-- @@@ is that a good idea? or rather fix it? -->
 *
 * @param entry         the pool entry for the connection to delete
 */
protected void deleteEntry(final BasicPoolEntry entry) {

    final HttpRoute route = entry.getPlannedRoute();

    if (log.isDebugEnabled()) {
        log.debug("Deleting connection" + " [" + route + "][" + entry.getState() + "]");
    }

    poolLock.lock();
    try {

        closeConnection(entry);

        final RouteSpecificPool rospl = getRoutePool(route, true);
        rospl.deleteEntry(entry);
        numConnections--;
        if (rospl.isUnused()) {
            routeToPool.remove(route);
        }

    } finally {
        poolLock.unlock();
    }
}

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

@Override
public void shutdown() {
    poolLock.lock();//from w w  w .  jav a  2  s  .c o  m
    try {
        if (shutdown) {
            return;
        }
        shutdown = true;

        // close all connections that are issued to an application
        final Iterator<BasicPoolEntry> iter1 = leasedConnections.iterator();
        while (iter1.hasNext()) {
            final BasicPoolEntry entry = iter1.next();
            iter1.remove();
            closeConnection(entry);
        }

        // close all free connections
        final Iterator<BasicPoolEntry> iter2 = freeConnections.iterator();
        while (iter2.hasNext()) {
            final BasicPoolEntry entry = iter2.next();
            iter2.remove();

            if (log.isDebugEnabled()) {
                log.debug(
                        "Closing connection" + " [" + entry.getPlannedRoute() + "][" + entry.getState() + "]");
            }
            closeConnection(entry);
        }

        // wake up all waiting threads
        final Iterator<WaitingThread> iwth = waitingThreads.iterator();
        while (iwth.hasNext()) {
            final WaitingThread waiter = iwth.next();
            iwth.remove();
            waiter.wakeup();
        }

        routeToPool.clear();

    } finally {
        poolLock.unlock();
    }
}

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

/**
 * Obtains a free entry from this pool, if one is available.
 *
 * @return an available pool entry, or <code>null</code> if there is none
 *//*from w w  w.j  av a  2s  . c  om*/
public BasicPoolEntry allocEntry(final Object state) {
    if (!freeEntries.isEmpty()) {
        final ListIterator<BasicPoolEntry> it = freeEntries.listIterator(freeEntries.size());
        while (it.hasPrevious()) {
            final BasicPoolEntry entry = it.previous();
            if (entry.getState() == null || LangUtils.equals(state, entry.getState())) {
                it.remove();
                return entry;
            }
        }
    }
    if (getCapacity() == 0 && !freeEntries.isEmpty()) {
        final BasicPoolEntry entry = freeEntries.remove();
        entry.shutdownEntry();
        final OperatedClientConnection conn = entry.getConnection();
        try {
            conn.close();
        } catch (final IOException ex) {
            log.debug("I/O error closing connection", ex);
        }
        return entry;
    }
    return null;
}