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

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

Introduction

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

Prototype

void close() throws Exception;

Source Link

Usage

From source file:com.spokentech.speechdown.server.util.pool.AbstractPoolableObjectFactory.java

/**
 * Initializes an {@link org.apache.commons.pool.ObjectPool} by borrowing each object from the
 * pool (thereby triggering activation) and then returning all the objects back to the pool. 
 * @param pool the object pool to be initialized.
 * @throws InstantiationException if borrowing (or returning) an object from the pool triggers an exception.
 *///from w w  w  . j  a  v  a 2 s  .  c  o m
public static void initPool(ObjectPool pool) throws InstantiationException {
    try {
        List<Object> objects = new ArrayList<Object>();
        while (true)
            try {
                objects.add(pool.borrowObject());
            } catch (NoSuchElementException e) {
                // ignore, max active reached
                break;
            }
        for (Object obj : objects) {
            pool.returnObject(obj);
        }
    } catch (Exception e) {
        try {
            pool.close();
        } catch (Exception e1) {
            _logger.warn("Encounter expception while attempting to close object pool!", e1);
        }
        throw (InstantiationException) new InstantiationException(e.getMessage()).initCause(e);
    }

}

From source file:com.honnix.jaxo.core.internal.services.PoolableCoreServiceImpl.java

@Override
public void close() {
    for (ObjectPool objectPool : objectPoolMap.values()) {
        try {//from  ww  w  .j  a v a  2s  .  c o  m
            objectPool.close();
        } catch (Exception ignored) {
        }
    }
    objectPoolMap.clear();

    for (ObjectPool objectPool : validatorObjectPoolMap.values()) {
        try {
            objectPool.close();
        } catch (Exception ignored) {
        }
    }
    validatorObjectPoolMap.clear();
}

From source file:net.sf.hajdbc.pool.generic.GenericObjectPoolFactory.java

@Override
public <T, E extends Exception> Pool<T, E> createPool(final PoolProvider<T, E> provider) {
    PoolableObjectFactory<T> factory = new PoolableObjectFactory<T>() {
        @Override/*from  ww w .j ava  2s .c o m*/
        public void destroyObject(T object) {
            provider.close(object);
        }

        @Override
        public T makeObject() throws Exception {
            return provider.create();
        }

        @Override
        public boolean validateObject(T object) {
            return provider.isValid(object);
        }

        @Override
        public void activateObject(T object) {
        }

        @Override
        public void passivateObject(T object) {
        }
    };

    final ObjectPool<T> pool = new GenericObjectPool<T>(factory, this.config);

    return new Pool<T, E>() {
        @Override
        public void close() {
            try {
                pool.close();
            } catch (Exception e) {
                logger.log(Level.WARN, e, e.getMessage());
            }
        }

        @Override
        public void release(T item) {
            try {
                pool.returnObject(item);
            } catch (Exception e) {
                logger.log(Level.WARN, e, e.getMessage());
            }
        }

        @Override
        public T take() throws E {
            try {
                return pool.borrowObject();
            } catch (NoSuchElementException e) {
                return provider.create();
            } catch (IllegalStateException e) {
                throw e;
            } catch (Exception e) {
                throw provider.getExceptionClass().cast(e);
            }
        }
    };
}

From source file:com.honnix.jaxo.core.internal.services.PoolableCoreServiceImpl.java

@Override
public void clearValidators(Schema schema) {
    ObjectPool<Validator> validatorObjectPool = validatorObjectPoolMap.remove(schema);
    try {/*from w  ww .j  ava  2s . c om*/
        validatorObjectPool.close();
    } catch (Exception ignored) {
    }
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestBaseObjectPool.java

public void testClose() throws Exception {
    ObjectPool pool = new BaseObjectPool() {
        public Object borrowObject() {
            return null;
        }/*from  ww  w .  ja va2 s.c  o m*/

        public void returnObject(Object obj) {
        }

        public void invalidateObject(Object obj) {
        }
    };

    pool.close();
    pool.close(); // should not error as of Pool 2.0.
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestObjectPool.java

public void testPOFCloseUsages() throws Exception {
    final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
    ObjectPool pool;
    try {//from ww  w  .  j  ava 2  s.c o m
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();

    /// Test correct behavior code paths
    PoolUtils.prefill(pool, 5);
    pool.close();

    //// Test exception handling close should swallow failures
    try {
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    reset(pool, factory, expectedMethods);
    factory.setDestroyObjectFail(true);
    PoolUtils.prefill(pool, 5);
    pool.close();
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestObjectPool.java

public void testClosedPoolBehavior() throws Exception {
    final ObjectPool pool;
    try {/*from  w w w  . ja v  a 2 s . c om*/
        pool = makeEmptyPool(new MethodCallPoolableObjectFactory());
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    Object o1 = pool.borrowObject();
    Object o2 = pool.borrowObject();

    pool.close();

    try {
        pool.addObject();
        fail("A closed pool must throw an IllegalStateException when addObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    try {
        pool.borrowObject();
        fail("A closed pool must throw an IllegalStateException when borrowObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    // The following should not throw exceptions just because the pool is closed.
    if (pool.getNumIdle() >= 0) {
        assertEquals("A closed pool shouldn't have any idle objects.", 0, pool.getNumIdle());
    }
    if (pool.getNumActive() >= 0) {
        assertEquals("A closed pool should still keep count of active objects.", 2, pool.getNumActive());
    }
    pool.returnObject(o1);
    if (pool.getNumIdle() >= 0) {
        assertEquals("returnObject should not add items back into the idle object pool for a closed pool.", 0,
                pool.getNumIdle());
    }
    if (pool.getNumActive() >= 0) {
        assertEquals("A closed pool should still keep count of active objects.", 1, pool.getNumActive());
    }
    pool.invalidateObject(o2);
    if (pool.getNumIdle() >= 0) {
        assertEquals("invalidateObject must not add items back into the idle object pool.", 0,
                pool.getNumIdle());
    }
    if (pool.getNumActive() >= 0) {
        assertEquals("A closed pool should still keep count of active objects.", 0, pool.getNumActive());
    }
    pool.clear();
    pool.close();
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java

public void testAddObject() throws Exception {
    assertEquals("should be zero idle", 0, pool.getNumIdle());
    pool.addObject();//from  w  ww . j a v  a2s .co m
    assertEquals("should be one idle", 1, pool.getNumIdle());
    assertEquals("should be zero active", 0, pool.getNumActive());
    Object obj = pool.borrowObject();
    assertEquals("should be zero idle", 0, pool.getNumIdle());
    assertEquals("should be one active", 1, pool.getNumActive());
    pool.returnObject(obj);
    assertEquals("should be one idle", 1, pool.getNumIdle());
    assertEquals("should be zero active", 0, pool.getNumActive());

    ObjectPool op = new GenericObjectPool();
    try {
        op.addObject();
        fail("Expected IllegalStateException when there is no factory.");
    } catch (IllegalStateException ise) {
        // expected
    }
    op.close();
}

From source file:org.apache.cayenne.conf.CustomDBCPDataSourceFactory.java

/**
 *
 *//* ww w. ja  v  a2s .  c  o m*/
@Override
public void tearDown() {
    try {
        DataSource dataSource = CustomDataSourceUtil.getThreadDataSource();
        if (dataSource instanceof ThreadPoolingDataSource) {
            ThreadPoolingDataSource poolingDataSource = (ThreadPoolingDataSource) dataSource;
            if (poolingDataSource != null) {
                ObjectPool pool = poolingDataSource.getPool();
                if (pool != null) {
                    try {
                        pool.close();
                    } catch (Throwable t) {
                        //
                    }
                }
            }
        }
    } catch (Throwable t) {
    }
}

From source file:org.apache.ojb.broker.accesslayer.ConnectionFactoryDBCPImpl.java

/**
 * Closes all managed pools.//from ww  w . j a  v  a  2  s  . com
 */
public void releaseAllResources() {
    super.releaseAllResources();
    synchronized (poolSynch) {
        if (!poolMap.isEmpty()) {
            Collection pools = poolMap.values();
            Iterator iterator = pools.iterator();
            ObjectPool op = null;
            while (iterator.hasNext()) {
                try {
                    op = (ObjectPool) iterator.next();
                    op.close();
                } catch (Exception e) {
                    log.error("Exception occured while closing ObjectPool " + op, e);
                }
            }
            poolMap.clear();
        }
        dsMap.clear();
    }
}