Example usage for org.apache.commons.pool ObjectPool returnObject

List of usage examples for org.apache.commons.pool ObjectPool returnObject

Introduction

In this page you can find the example usage for org.apache.commons.pool ObjectPool returnObject.

Prototype

void returnObject(Object p0) throws Exception;

Source Link

Usage

From source file:org.wso2.carbon.webapp.authenticator.framework.test.WebappAuthenticatorFrameworkUtilTest.java

@Test
public void testOAuthTokenValidatorStubPool() {
    ObjectPool stubs = null;
    OAuth2TokenValidationServiceStub stub = null;

    try {/*from w  w w  .j a v a2  s .c o  m*/
        stubs = new GenericObjectPool(new OAuthTokenValidationStubFactory(TOKEN_VALIDATION_SERVICE_URL,
                ADMIN_USERNAME, ADMIN_PASSWORD, PROPERTIES));

        stub = (OAuth2TokenValidationServiceStub) stubs.borrowObject();
        Assert.assertNotNull(stub);
    } catch (Exception e) {
        String msg = "Error occurred while borrowing an oauth validator service stub instance from the pool";
        log.error(msg, e);
        Assert.fail(msg, e);
    } finally {
        if (stubs != null) {
            try {
                if (stub != null) {
                    stubs.returnObject(stub);
                }
            } catch (Exception e) {
                log.warn("Error occurred while returning oauth validator service stub instance to the pool", e);
            }

            /* Checks if the stub instance used above has been properly returned to the pool */
            Assert.assertEquals(stubs.getNumIdle(), 1);
            /* Verifies that there's no hanging connections after the operation performed above */
            Assert.assertEquals(stubs.getNumActive(), 0);

            try {
                stubs.close();
            } catch (Exception e) {
                log.warn("Error occurred while closing the object pool", e);
            }
        }
    }
}

From source file:org.xlcloud.ssh.SshPoolableSessionFactory.java

private void returnSession(Session session, ObjectPool pool) {
    try {/*from  ww  w  .  ja va2 s  .  c  om*/
        pool.returnObject(session);
    } catch (Exception e) {
        String host = "unknown";
        if (session != null) {
            host = session.getHost();
        }
        LOG.error("Could not return SSH session from pool for cluster: " + host, e);
    }
}