Example usage for org.apache.commons.pool.impl GenericKeyedObjectPool setTestOnBorrow

List of usage examples for org.apache.commons.pool.impl GenericKeyedObjectPool setTestOnBorrow

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl GenericKeyedObjectPool setTestOnBorrow.

Prototype

public void setTestOnBorrow(boolean testOnBorrow) 

Source Link

Document

When true, objects will be org.apache.commons.pool.PoolableObjectFactory#validateObject validated before being returned by the #borrowObject method.

Usage

From source file:com.googlecode.jmxtrans.guice.JmxTransModule.java

private <K, V> GenericKeyedObjectPool getObjectPool(KeyedPoolableObjectFactory<K, V> factory, String poolName) {
    GenericKeyedObjectPool<K, V> pool = new GenericKeyedObjectPool<K, V>(factory);
    pool.setTestOnBorrow(true);
    pool.setMaxActive(-1);/*from  ww  w  . ja  v a  2s  .com*/
    pool.setMaxIdle(-1);
    pool.setTimeBetweenEvictionRunsMillis(MILLISECONDS.convert(5, MINUTES));
    pool.setMinEvictableIdleTimeMillis(MILLISECONDS.convert(5, MINUTES));

    try {
        ManagedGenericKeyedObjectPool mbean = new ManagedGenericKeyedObjectPool(pool, poolName);
        ManagementFactory.getPlatformMBeanServer().registerMBean(mbean, mbean.getObjectName());
    } catch (Exception e) {
        log.error("Could not register mbean for pool [{}]", poolName, e);
    }

    return pool;
}

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

public void testExceptionOnDestroyDuringBorrow() throws Exception {
    SimpleFactory factory = new SimpleFactory();
    factory.setThrowExceptionOnDestroy(true);
    factory.setValidationEnabled(true);/*  ww  w . j  ava2s  . c  o  m*/
    GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory);
    pool.setTestOnBorrow(true);
    pool.borrowObject("one");
    factory.setValid(false); // Make validation fail on next borrow attempt
    try {
        pool.borrowObject("one");
        fail("Expecting NoSuchElementException");
    } catch (NoSuchElementException ex) {
        // expected
    }
    assertEquals(1, pool.getNumActive("one"));
    assertEquals(0, pool.getNumIdle("one"));
    assertEquals(1, pool.getNumActive());
    assertEquals(0, pool.getNumIdle());
}

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

public void testSettersAndGetters() throws Exception {
    GenericKeyedObjectPool pool = new GenericKeyedObjectPool();
    {/*from  w  w w  .  j  a v  a 2  s.c o  m*/
        pool.setFactory(new SimpleFactory());
    }
    {
        pool.setMaxActive(123);
        assertEquals(123, pool.getMaxActive());
    }
    {
        pool.setMaxIdle(12);
        assertEquals(12, pool.getMaxIdle());
    }
    {
        pool.setMaxWait(1234L);
        assertEquals(1234L, pool.getMaxWait());
    }
    {
        pool.setMinEvictableIdleTimeMillis(12345L);
        assertEquals(12345L, pool.getMinEvictableIdleTimeMillis());
    }
    {
        pool.setNumTestsPerEvictionRun(11);
        assertEquals(11, pool.getNumTestsPerEvictionRun());
    }
    {
        pool.setTestOnBorrow(true);
        assertTrue(pool.getTestOnBorrow());
        pool.setTestOnBorrow(false);
        assertTrue(!pool.getTestOnBorrow());
    }
    {
        pool.setTestOnReturn(true);
        assertTrue(pool.getTestOnReturn());
        pool.setTestOnReturn(false);
        assertTrue(!pool.getTestOnReturn());
    }
    {
        pool.setTestWhileIdle(true);
        assertTrue(pool.getTestWhileIdle());
        pool.setTestWhileIdle(false);
        assertTrue(!pool.getTestWhileIdle());
    }
    {
        pool.setTimeBetweenEvictionRunsMillis(11235L);
        assertEquals(11235L, pool.getTimeBetweenEvictionRunsMillis());
    }
    {
        pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
        pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_FAIL, pool.getWhenExhaustedAction());
        pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
    }
}