Example usage for org.apache.commons.pool.impl GenericObjectPool setMinEvictableIdleTimeMillis

List of usage examples for org.apache.commons.pool.impl GenericObjectPool setMinEvictableIdleTimeMillis

Introduction

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

Prototype

public synchronized void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) 

Source Link

Document

Sets the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any).

Usage

From source file:TestObjectPool.java

 public static void main(String args[]) throws Exception {

   GenericObjectPool pool = new GenericObjectPool();
   pool.setFactory(new EmployeeFactory());

   /*First way of initializing pool
   pool.setMinIdle(5);/*from w w  w .j  a v a2s.  c  o m*/
   pool.setTimeBetweenEvictionRunsMillis(500);
   Thread.currentThread().sleep(600);*/

   /* second, and preferred way */
   for(int i = 0; i < 5; ++i) {
      pool.addObject();
   }

   // pool.setTestOnReturn(true);
   pool.setMinEvictableIdleTimeMillis(1000);
   pool.setTimeBetweenEvictionRunsMillis(600);

   System.err.println("Number of employees in pool: " + pool.getNumIdle());

     Thread.currentThread().sleep(1500);

   Employee employee = (Employee)pool.borrowObject();

   employee.setName("Fred Flintstone");
   employee.doWork();

   System.err.println("Employee: "  + employee);

   pool.returnObject(employee);

     System.err.println("Number of employees in pool: " + pool.getNumIdle());
}

From source file:com.qualogy.qafe.business.resource.rdb.DriverManagerDataSource.java

public void init(ApplicationContext context) {
    DriverManagerResource driverManagerResource = (DriverManagerResource) getBindResource();
    String userName = driverManagerResource.getUsername();
    String password = driverManagerResource.getPassword();
    String url = driverManagerResource.getUrl();
    String driverClassName = driverManagerResource.getDriverClassName();

    DataSource springDS = new org.springframework.jdbc.datasource.DriverManagerDataSource(url, userName,
            password);/*from w  w  w  .  ja va2 s. co  m*/
    ((org.springframework.jdbc.datasource.DriverManagerDataSource) springDS)
            .setDriverClassName(driverClassName);

    GenericObjectPool pool = new GenericObjectPool();
    pool.setMinEvictableIdleTimeMillis(300000);
    pool.setTimeBetweenEvictionRunsMillis(60000);
    PoolableConnectionFactory connectionFactory = new PoolableConnectionFactory(
            new DataSourceConnectionFactory(springDS), pool, null, null, false, true);

    PoolingDataSource poolingDataSource = new PoolingDataSource(pool);
    poolingDataSource.setAccessToUnderlyingConnectionAllowed(true);
    setDataSource(poolingDataSource);

    postInit(context);
}

From source file:com.totalchange.bunman.cddb.impl.CddbQuerierImpl.java

public CddbQuerierImpl(String hostname, int port, int maxConnections, long idleTimeout) {
    GenericObjectPool<CDDB> pool = new GenericObjectPool<CDDB>(new CddbPoolableObjectFactory(hostname, port));
    pool.setMaxActive(maxConnections);//from   w  w  w . ja  va 2  s. co  m
    pool.setMinEvictableIdleTimeMillis(idleTimeout);
    pool.setTimeBetweenEvictionRunsMillis(TIME_BETWEEN_EVICTION_RUNS);

    this.cddbPool = pool;
    this.executor = Executors.newFixedThreadPool(maxConnections);
}

From source file:edu.jhu.pha.vospace.DbPoolServlet.java

@Override
public void init() throws ServletException {
    ServletContext context = this.getServletContext();

    Configuration conf = (Configuration) context.getAttribute("configuration");

    try {//from w  w  w  .ja  v a2s  . c  o m
        Class.forName(conf.getString("db.driver"));
    } catch (ClassNotFoundException e) {
        logger.error(e);
        throw new ServletException(e);
    }

    GenericObjectPool pool = new GenericObjectPool(null);
    pool.setMinEvictableIdleTimeMillis(6 * 60 * 60 * 1000);
    pool.setTimeBetweenEvictionRunsMillis(30 * 60 * 1000);
    pool.setNumTestsPerEvictionRun(-1);

    DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory(conf.getString("db.url"),
            conf.getString("db.login"), conf.getString("db.password"));

    PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, pool, null, "SELECT * FROM mysql.db",
            false, true);
    new PoolingDriver().registerPool("dbPool", pool);
}

From source file:com.nesscomputing.syslog4j.impl.pool.generic.GenericSyslogPoolFactory.java

protected void configureGenericObjectPool(GenericObjectPool<AbstractSyslogWriter> genericObjectPool)
        throws SyslogRuntimeException {
    SyslogPoolConfigIF poolConfig = null;

    try {// w  ww .  j a  v  a 2 s. c  om
        poolConfig = (SyslogPoolConfigIF) this.syslog.getConfig();

    } catch (ClassCastException cce) {
        throw new SyslogRuntimeException("config must implement interface SyslogPoolConfigIF");
    }

    genericObjectPool.setMaxActive(poolConfig.getMaxActive());
    genericObjectPool.setMaxIdle(poolConfig.getMaxIdle());
    genericObjectPool.setMaxWait(poolConfig.getMaxWait());
    genericObjectPool.setMinEvictableIdleTimeMillis(poolConfig.getMinEvictableIdleTimeMillis());
    genericObjectPool.setMinIdle(poolConfig.getMinIdle());
    genericObjectPool.setNumTestsPerEvictionRun(poolConfig.getNumTestsPerEvictionRun());
    genericObjectPool.setSoftMinEvictableIdleTimeMillis(poolConfig.getSoftMinEvictableIdleTimeMillis());
    genericObjectPool.setTestOnBorrow(poolConfig.isTestOnBorrow());
    genericObjectPool.setTestOnReturn(poolConfig.isTestOnReturn());
    genericObjectPool.setTestWhileIdle(poolConfig.isTestWhileIdle());
    genericObjectPool.setTimeBetweenEvictionRunsMillis(poolConfig.getTimeBetweenEvictionRunsMillis());
    genericObjectPool.setWhenExhaustedAction(poolConfig.getWhenExhaustedAction());
}

From source file:lineage2.commons.dbcp.BasicDataSource.java

/**
 * Constructor for BasicDataSource.//from   ww w  . j a  v  a 2s .  com
 * @param driver String
 * @param connectURI String
 * @param uname String
 * @param passwd String
 * @param maxActive int
 * @param maxIdle int
 * @param idleTimeOut int
 * @param idleTestPeriod int
 * @param poolPreparedStatements boolean
 */
public BasicDataSource(String driver, String connectURI, String uname, String passwd, int maxActive,
        int maxIdle, int idleTimeOut, int idleTestPeriod, boolean poolPreparedStatements) {
    GenericObjectPool<?> connectionPool = new GenericObjectPool<>(null);
    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxIdle(maxIdle);
    connectionPool.setMinIdle(1);
    connectionPool.setMaxWait(-1L);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    connectionPool.setTestOnBorrow(false);
    connectionPool.setTestWhileIdle(true);
    connectionPool.setTimeBetweenEvictionRunsMillis(idleTestPeriod * 1000L);
    connectionPool.setNumTestsPerEvictionRun(maxActive);
    connectionPool.setMinEvictableIdleTimeMillis(idleTimeOut * 1000L);
    GenericKeyedObjectPoolFactory<?, ?> statementPoolFactory = null;
    if (poolPreparedStatements) {
        statementPoolFactory = new GenericKeyedObjectPoolFactory<>(null, -1,
                GenericObjectPool.WHEN_EXHAUSTED_FAIL, 0L, 1, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL);
    }
    Properties connectionProperties = new Properties();
    connectionProperties.put("user", uname);
    connectionProperties.put("password", passwd);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, connectionProperties);
    @SuppressWarnings("unused")
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, statementPoolFactory, "SELECT 1", false, true);
    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
    _connectionPool = connectionPool;
    _source = dataSource;
}

From source file:com.zotoh.core.db.JDBCPoolManager.java

private synchronized JDBCPool create(String pool, JDBCInfo param, Properties props) throws SQLException {
    if (existsPool(pool)) {
        throw new SQLException("Jdbc Pool already exists: " + pool);
    }/* w  w  w.ja va 2s . c om*/

    PoolableConnectionFactory pcf;
    DriverConnectionFactory dcf;
    GenericObjectPool gop;
    DBVendor dbv;
    ObjectPool p;
    Driver d;

    tlog().debug("JDBCPoolMgr: Driver : {}", param.getDriver());
    tlog().debug("JDBCPoolMgr: URL : {}", param.getUrl());

    //        Ute.loadDriver(param.getDriver());
    d = DriverManager.getDriver(param.getUrl());
    dbv = DBUte.getDBVendor(param);

    dcf = new DriverConnectionFactory(d, param.getUrl(), props);
    gop = new GenericObjectPool();
    gop.setMaxActive(asInt(props.getProperty("max-conns"), 10));
    gop.setTestOnBorrow(true);
    gop.setMaxIdle(gop.getMaxActive());
    gop.setMinIdle(asInt(props.getProperty("min-conns"), 2));
    gop.setMaxWait(asLong(props.getProperty("max-wait4-conn-millis"), 1500L));
    gop.setMinEvictableIdleTimeMillis(asLong(props.getProperty("evict-conn-ifidle-millis"), 300000L));
    gop.setTimeBetweenEvictionRunsMillis(asLong(props.getProperty("check-evict-every-millis"), 60000L));

    pcf = new PoolableConnectionFactory(dcf, gop, null, null, true, false);
    pcf.setDefaultReadOnly(false);
    p = pcf.getPool();

    JDBCPool j = new JDBCPool(dbv, param, p);
    _ps.put(pool, j);

    tlog().debug("JDBCPoolMgr: Added db pool: {}, info= {}", pool, param);
    return j;
}

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

private void checkEvictionOrder(boolean lifo) throws Exception {
    SimpleFactory factory = new SimpleFactory();
    GenericObjectPool pool = new GenericObjectPool(factory);
    pool.setNumTestsPerEvictionRun(2);/*from w w w. j  av  a 2 s  .  co  m*/
    pool.setMinEvictableIdleTimeMillis(100);
    pool.setLifo(lifo);
    for (int i = 0; i < 5; i++) {
        pool.addObject();
        Thread.sleep(100);
    }
    // Order, oldest to youngest, is "0", "1", ...,"4"
    pool.evict(); // Should evict "0" and "1"
    Object obj = pool.borrowObject();
    assertTrue("oldest not evicted", !obj.equals("0"));
    assertTrue("second oldest not evicted", !obj.equals("1"));
    // 2 should be next out for FIFO, 4 for LIFO
    assertEquals("Wrong instance returned", lifo ? "4" : "2", obj);

    // Two eviction runs in sequence
    factory = new SimpleFactory();
    pool = new GenericObjectPool(factory);
    pool.setNumTestsPerEvictionRun(2);
    pool.setMinEvictableIdleTimeMillis(100);
    pool.setLifo(lifo);
    for (int i = 0; i < 5; i++) {
        pool.addObject();
        Thread.sleep(100);
    }
    pool.evict(); // Should evict "0" and "1"
    pool.evict(); // Should evict "2" and "3"
    obj = pool.borrowObject();
    assertEquals("Wrong instance remaining in pool", "4", obj);
}

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

private void checkEvictorVisiting(boolean lifo) throws Exception {
    VisitTrackerFactory factory = new VisitTrackerFactory();
    GenericObjectPool pool = new GenericObjectPool(factory);
    pool.setNumTestsPerEvictionRun(2);//from w  ww  . j  a v a  2 s.co  m
    pool.setMinEvictableIdleTimeMillis(-1);
    pool.setTestWhileIdle(true);
    pool.setLifo(lifo);
    pool.setTestOnReturn(false);
    pool.setTestOnBorrow(false);
    for (int i = 0; i < 8; i++) {
        pool.addObject();
    }
    pool.evict(); // Visit oldest 2 - 0 and 1
    Object obj = pool.borrowObject();
    pool.returnObject(obj);
    obj = pool.borrowObject();
    pool.returnObject(obj);
    // borrow, return, borrow, return
    // FIFO will move 0 and 1 to end
    // LIFO, 7 out, then in, then out, then in
    pool.evict(); // Should visit 2 and 3 in either case
    for (int i = 0; i < 8; i++) {
        VisitTracker tracker = (VisitTracker) pool.borrowObject();
        if (tracker.getId() >= 4) {
            assertEquals("Unexpected instance visited " + tracker.getId(), 0, tracker.getValidateCount());
        } else {
            assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 1,
                    tracker.getValidateCount());
        }
    }

    factory = new VisitTrackerFactory();
    pool = new GenericObjectPool(factory);
    pool.setNumTestsPerEvictionRun(3);
    pool.setMinEvictableIdleTimeMillis(-1);
    pool.setTestWhileIdle(true);
    pool.setLifo(lifo);
    pool.setTestOnReturn(false);
    pool.setTestOnBorrow(false);
    for (int i = 0; i < 8; i++) {
        pool.addObject();
    }
    pool.evict(); // 0, 1, 2
    pool.evict(); // 3, 4, 5
    obj = pool.borrowObject();
    pool.returnObject(obj);
    obj = pool.borrowObject();
    pool.returnObject(obj);
    obj = pool.borrowObject();
    pool.returnObject(obj);
    // borrow, return, borrow, return
    // FIFO 3,4,5,6,7,0,1,2
    // LIFO 7,6,5,4,3,2,1,0
    // In either case, pointer should be at 6
    pool.evict();
    // Should hit 6,7,0 - 0 for second time
    for (int i = 0; i < 8; i++) {
        VisitTracker tracker = (VisitTracker) pool.borrowObject();
        if (tracker.getId() != 0) {
            assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 1,
                    tracker.getValidateCount());
        } else {
            assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 2,
                    tracker.getValidateCount());
        }
    }
    // Randomly generate a pools with random numTests
    // and make sure evictor cycles through elements appropriately
    int[] smallPrimes = { 2, 3, 5, 7 };
    Random random = new Random();
    random.setSeed(System.currentTimeMillis());
    for (int i = 0; i < 4; i++) {
        pool.setNumTestsPerEvictionRun(smallPrimes[i]);
        for (int j = 0; j < 5; j++) {
            pool = new GenericObjectPool(factory);
            pool.setNumTestsPerEvictionRun(3);
            pool.setMinEvictableIdleTimeMillis(-1);
            pool.setTestWhileIdle(true);
            pool.setLifo(lifo);
            pool.setTestOnReturn(false);
            pool.setTestOnBorrow(false);
            pool.setMaxIdle(-1);
            int instanceCount = 10 + random.nextInt(20);
            pool.setMaxActive(instanceCount);
            for (int k = 0; k < instanceCount; k++) {
                pool.addObject();
            }

            // Execute a random number of evictor runs
            int runs = 10 + random.nextInt(50);
            for (int k = 0; k < runs; k++) {
                pool.evict();
            }

            // Number of times evictor should have cycled through the pool
            int cycleCount = (runs * pool.getNumTestsPerEvictionRun()) / instanceCount;

            // Look at elements and make sure they are visited cycleCount
            // or cycleCount + 1 times
            VisitTracker tracker = null;
            int visitCount = 0;
            for (int k = 0; k < instanceCount; k++) {
                tracker = (VisitTracker) pool.borrowObject();
                assertTrue(pool.getNumActive() <= pool.getMaxActive());
                visitCount = tracker.getValidateCount();
                assertTrue(visitCount >= cycleCount && visitCount <= cycleCount + 1);
            }
        }
    }
}

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

public void testSettersAndGetters() throws Exception {
    GenericObjectPool pool = new GenericObjectPool();
    {/*from   w w w.j  a v  a2 s  .  c om*/
        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.setSoftMinEvictableIdleTimeMillis(12135L);
        assertEquals(12135L, pool.getSoftMinEvictableIdleTimeMillis());
    }
    {
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_FAIL, pool.getWhenExhaustedAction());
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
    }
}