Java Code Examples for org.apache.commons.pool2.PooledObject#invalidate()

The following examples show how to use org.apache.commons.pool2.PooledObject#invalidate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PooledSocketFactory.java    From timely with Apache License 2.0 6 votes vote down vote up
@Override
public void destroyObject(PooledObject<Socket> pooledObject) throws Exception {
    try {
        Collectd.logDebug("Shutting down connection to Timely at " + host + ":" + port);
        Socket socket = (Socket) pooledObject.getObject();
        if (socket != null) {
            socket.close();
        }
    } catch (Exception e) {
        Collectd.logError(
                "Error closing connection to Timely at " + host + ":" + port + ". Error: " + e.getMessage());
    } finally {
        pooledObject.invalidate();
        ;
    }
}
 
Example 2
Source File: PooledCloseableHttpClientFactory.java    From timely with Apache License 2.0 5 votes vote down vote up
@Override
public void destroyObject(PooledObject<CloseableHttpClient> pooledObject) throws Exception {
    try {
        pooledObject.getObject().close();
    } catch (Exception e) {
        // do nothing
    } finally {
        pooledObject.invalidate();
        ;
    }
}
 
Example 3
Source File: GenericKeyedObjectPool.java    From commons-pool with Apache License 2.0 5 votes vote down vote up
/**
 * Destroy the wrapped, pooled object.
 *
 * @param key The key associated with the object to destroy.
 * @param toDestroy The wrapped object to be destroyed
 * @param always Should the object be destroyed even if it is not currently
 *               in the set of idle objects for the given key
 * @return {@code true} if the object was destroyed, otherwise {@code false}
 * @throws Exception If the object destruction failed
 */
private boolean destroy(final K key, final PooledObject<T> toDestroy, final boolean always)
        throws Exception {

    final ObjectDeque<T> objectDeque = register(key);

    try {
        boolean isIdle;
        synchronized(toDestroy) {
            // Check idle state directly
            isIdle = toDestroy.getState().equals(PooledObjectState.IDLE);
            // If idle, not under eviction test, or always is true, remove instance,
            // updating isIdle if instance is found in idle objects
            if (isIdle || always) {
                isIdle = objectDeque.getIdleObjects().remove(toDestroy);
            }
        }
        if (isIdle || always) {
            objectDeque.getAllObjects().remove(new IdentityWrapper<>(toDestroy.getObject()));
            toDestroy.invalidate();

            try {
                factory.destroyObject(key, toDestroy);
            } finally {
                objectDeque.getCreateCount().decrementAndGet();
                destroyedCount.incrementAndGet();
                numTotal.decrementAndGet();
            }
            return true;
        }
        return false;
    } finally {
        deregister(key);
    }
}
 
Example 4
Source File: GenericObjectPool.java    From commons-pool with Apache License 2.0 5 votes vote down vote up
/**
 * Destroys a wrapped pooled object.
 *
 * @param toDestroy The wrapped pooled object to destroy
 *
 * @throws Exception If the factory fails to destroy the pooled object
 *                   cleanly
 */
private void destroy(final PooledObject<T> toDestroy) throws Exception {
    toDestroy.invalidate();
    idleObjects.remove(toDestroy);
    allObjects.remove(new IdentityWrapper<>(toDestroy.getObject()));
    try {
        factory.destroyObject(toDestroy);
    } finally {
        destroyedCount.incrementAndGet();
        createCount.decrementAndGet();
    }
}