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

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

Introduction

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

Prototype

public synchronized void setMaxActive(int maxActive) 

Source Link

Document

Sets the cap on the number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time.

Usage

From source file:launcher.multithread.Extract.java

/**
 * Starts//from   www.ja v  a 2s  .  c o m
 */
protected void go() {
    GenericObjectPool pool = new GenericObjectPool(new ExtractorFactory());
    pool.setMaxActive(15);
    // wait forever
    pool.setMaxWait(-1);

    // hacktastic ;o)
    /*
    int[] rapIds = {218,67,92,26,497,374,1858,1039,429,716};      
    for(int i=0; i<rapIds.length; i++) {
       int rapId = rapIds[i];
    */
    for (int rapId = 0; rapId < 2000; rapId++) {

        // these have been done... 
        if (rapId == 218 || rapId == 67 || rapId == 92 || rapId == 26 || rapId == 497 || rapId == 374
                || rapId == 1858 || rapId == 1039 || rapId == 429 || rapId == 716) {
            continue;
        }

        logger.info("Queing up Id: " + rapId);
        SequenceProcessor workflow = (SequenceProcessor) context.getBean("GBIF:INDEX:1.0:extractOccurrence");
        Map<String, Object> seed = new HashMap<String, Object>();
        GregorianCalendar cal = new GregorianCalendar();
        cal.add(Calendar.MONTH, -6);
        seed.put("pageFrom", cal.getTime());
        seed.put("resourceAccessPointId", new Long(rapId));
        Thread t = new Thread(new Runner(seed, workflow, pool));
        t.start();
    }
}

From source file:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImplTest.java

@Test
public void testFailWithStaleConnection() throws Exception {
    ConnectionFactory driverConnectionFactory = createConnectionFactory(false);

    GenericObjectPool genericObjectPool = new GenericObjectPool(null);
    genericObjectPool.setMaxActive(1);

    PoolingDataSource poolingDataSource = createPoolingDataSource(driverConnectionFactory, genericObjectPool);
    try {//from  ww  w  .  java 2  s .com
        runTwoSqlStatementsWithTwoConnections(poolingDataSource);
    } catch (Exception e) {
        assertTrue(e.getClass().getName().contains("CommunicationsException"));
    }
}

From source file:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImplTest.java

@Test
public void testHandleStaleConnections() throws Exception {
    ConnectionFactory driverConnectionFactory = createConnectionFactory(true);

    GenericObjectPool genericObjectPool = new GenericObjectPool(null);
    genericObjectPool.setMaxActive(1);

    genericObjectPool.setTestOnBorrow(true);
    /*//from w ww .  j  a v  a  2s  .  co m
            genericObjectPool.setTestWhileIdle(true);   // Test idle instances visited by the pool maintenance thread and destroy any that fail validation
            genericObjectPool.setTimeBetweenEvictionRunsMillis(10000);      // Test every 10 seconds
            genericObjectPool.setMaxWait(10000);
    */

    PoolingDataSource poolingDataSource = createPoolingDataSource(driverConnectionFactory, genericObjectPool);
    runTwoSqlStatementsWithTwoConnections(poolingDataSource);
}

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

/**
 * Constructor for BasicDataSource./*w ww. j  av  a2 s.  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:Pool162.java

public void testWhenExhaustedBlockInterupt() throws Exception {
    GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
    //Set this value to 1 to get the deadlock. No deadlock when it sets to 
    //other values.
    pool.setMaxActive(2);
    pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);

    Object obj1 = pool.borrowObject();

    // Make sure one object was obtained
    if (obj1 == null) {
        throw new Exception("obj1 is null");
    }/*  w  w w  .j  av  a 2  s.  c o m*/

    // Create a separate thread to try and borrow another object
    WaitingTestThread wtt = new WaitingTestThread(pool, 200);
    wtt.start();
    // Give wtt time to start
    Thread.sleep(200);
    //bug trigger #1
    wtt.interrupt();
    //bug trigger #1
    // Give interrupt time to take effect
    Thread.sleep(200);
    // Return object to the pool
    pool.returnObject(obj1);
    Object obj2 = null;
    obj2 = pool.borrowObject();
    if (obj2 == null) {
        throw new Exception("obj2 is null");
    }
    pool.returnObject(obj2);
    pool.close();
}

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

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

    try {/*from w  w w . j  a v a  2 s .  c  o  m*/
        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: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);
    }//  ww w  . ja va  2  s .c o m

    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

protected ObjectPool makeEmptyPool(int mincap) {
    GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
    pool.setMaxActive(mincap);
    pool.setMaxIdle(mincap);/*from  www  .j  a  v a2  s  . c  o m*/
    return pool;
}

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

/**
 * Tests addObject contention between ensureMinIdle triggered by the Evictor
 * with minIdle > 0 and borrowObject.
 *//*from   w w w .  java  2 s . c om*/
public void testEvictAddObjects() throws Exception {
    SimpleFactory factory = new SimpleFactory();
    factory.setMakeLatency(300);
    factory.setMaxActive(2);
    GenericObjectPool pool = new GenericObjectPool(factory);
    pool.setMaxActive(2);
    pool.setMinIdle(1);
    pool.borrowObject(); // numActive = 1, numIdle = 0
    // Create a test thread that will run once and try a borrow after
    // 150ms fixed delay
    TestThread borrower = new TestThread(pool, 1, 150, false);
    Thread borrowerThread = new Thread(borrower);
    // Set evictor to run in 100 ms - will create idle instance
    pool.setTimeBetweenEvictionRunsMillis(100);
    borrowerThread.start(); // Off to the races
    borrowerThread.join();
    assertTrue(!borrower.failed());
    pool.close();
}

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 .  co  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.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());
    }
}