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

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

Introduction

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

Prototype

byte WHEN_EXHAUSTED_FAIL

To view the source code for org.apache.commons.pool.impl GenericObjectPool WHEN_EXHAUSTED_FAIL.

Click Source Link

Document

A "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), the #borrowObject method should fail, throwing a NoSuchElementException .

Usage

From source file:org.mule.transport.ftp.FtpConnector.java

protected GenericObjectPool createPool(FtpConnectionFactory connectionFactory) {
    GenericObjectPool genericPool = new GenericObjectPool(connectionFactory);
    byte poolExhaustedAction = ThreadingProfile.DEFAULT_POOL_EXHAUST_ACTION;

    ThreadingProfile receiverThreadingProfile = this.getReceiverThreadingProfile();
    if (receiverThreadingProfile != null) {
        int threadingProfilePoolExhaustedAction = receiverThreadingProfile.getPoolExhaustedAction();
        if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_WAIT) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
        } else if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_ABORT) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
        } else if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_RUN) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
        }//from w w  w  .  j  a  va2  s.  c  o  m
    }

    genericPool.setWhenExhaustedAction(poolExhaustedAction);
    genericPool.setTestOnBorrow(isValidateConnections());
    return genericPool;
}

From source file:org.mule.transport.ftp.FtpNamespaceHandlerTestCase.java

@Test
public void testReceiverFtpConnector() throws EndpointException {
    FtpConnector c = (FtpConnector) muleContext.getRegistry().lookupConnector("receiverFtpConnector");
    assertNotNull(c);//  w  w  w .  j  av a  2s  .  com

    MuleEndpointURI uri = new MuleEndpointURI("http://localhost", null);
    GenericObjectPool objectPool = (GenericObjectPool) c.getFtpPool(uri);
    assertEquals(GenericObjectPool.WHEN_EXHAUSTED_FAIL, objectPool.getWhenExhaustedAction());
}

From source file:org.mule.transport.ftps.FtpsConnector.java

protected GenericObjectPool createPool(FtpsConnectionFactory connectionFactory) {
    GenericObjectPool genericPool = new GenericObjectPool(connectionFactory);
    byte poolExhaustedAction = ThreadingProfile.DEFAULT_POOL_EXHAUST_ACTION;

    ThreadingProfile receiverThreadingProfile = this.getReceiverThreadingProfile();
    if (receiverThreadingProfile != null) {
        int threadingProfilePoolExhaustedAction = receiverThreadingProfile.getPoolExhaustedAction();
        if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_WAIT) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
        } else if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_ABORT) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
        } else if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_RUN) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
        }//ww w  .j  a va2s  .  c  o m
    }

    genericPool.setWhenExhaustedAction(poolExhaustedAction);
    genericPool.setTestOnBorrow(isValidateConnections());
    return genericPool;
}

From source file:org.onexus.collection.store.mysql.internal.MysqlCollectionStore.java

protected DataSource newDataSource() {

    try {//from  ww w.j  a  va 2s . co  m
        Class.forName(Driver.class.getName());
    } catch (Exception e) {
        LOGGER.error("Exception: " + e.getMessage());
    }

    // Config parameters
    int maxActive = 8;
    try {
        maxActive = Integer.valueOf(getPoolMaxActive().trim()).intValue();
    } catch (Exception e) {
        LOGGER.error("Malformed config parameter 'poolMaxActive'");
    }

    byte whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
    try {
        if (getPoolWhenExhausted().trim().equalsIgnoreCase("FAIL")) {
            whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
        }
        if (getPoolWhenExhausted().trim().equalsIgnoreCase("GROW")) {
            whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_GROW;
        }
    } catch (Exception e) {
        LOGGER.error("Malformed config parameter 'poolWhenExhausted'");
    }

    long maxWait = GenericObjectPool.DEFAULT_MAX_WAIT;
    try {
        maxWait = Long.valueOf(getPoolMaxWait().trim()).longValue();
    } catch (Exception e) {
        LOGGER.error("Malformed config parameter 'poolMaxWait'");
    }

    // Initialize the DataSource with a connection pool
    ConnectionFactory connectionFactory = new MysqlConnectionFactory();
    GenericObjectPool connectionPool = new GenericObjectPool(null, maxActive, whenExhausted, maxWait);
    connectionPool.setTestWhileIdle(true);
    connectionPool.setTimeBetweenEvictionRunsMillis(3600);
    connectionPool.setMinEvictableIdleTimeMillis(3600);

    @SuppressWarnings("unused")
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);
    poolableConnectionFactory.setValidationQuery("SELECT 1");
    return new PoolingDataSource(connectionPool);

}

From source file:org.opencms.db.CmsDbPool.java

/**
 * Creates a JDBC DriverManager based DBCP connection pool.<p>
 * //  w w  w  . j  a  va  2  s .c o  m
 * @param config the configuration (opencms.properties)
 * @param key the key of the database pool in the configuration
 * @return String the URL to access the created DBCP pool
 * @throws Exception if the pool could not be initialized
 */
public static PoolingDriver createDriverManagerConnectionPool(CmsParameterConfiguration config, String key)
        throws Exception {

    // read the values of the pool configuration specified by the given key
    String jdbcDriver = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_DRIVER);
    String jdbcUrl = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_URL);
    String jdbcUrlParams = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_URL_PARAMS);
    int maxActive = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_ACTIVE, 10);
    int maxWait = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_WAIT, 2000);
    int maxIdle = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_IDLE, 5);
    int minEvictableIdleTime = config
            .getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MIN_EVICTABLE_IDLE_TIME, 1800000);
    int minIdle = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MIN_IDLE, 0);
    int numTestsPerEvictionRun = config
            .getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_NUM_TESTS_PER_EVICTION_RUN, 3);
    int timeBetweenEvictionRuns = config
            .getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TIME_BETWEEN_EVICTION_RUNS, 3600000);
    String testQuery = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_QUERY);
    String username = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_USERNAME);
    String password = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_PASSWORD);
    String poolUrl = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_POOL_URL);
    String whenExhaustedActionValue = config
            .get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_WHEN_EXHAUSTED_ACTION).trim();
    byte whenExhaustedAction = 0;
    boolean testOnBorrow = Boolean
            .valueOf(config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_ON_BORROW, "false").trim())
            .booleanValue();
    boolean testWhileIdle = Boolean
            .valueOf(
                    config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_WHILE_IDLE, "false").trim())
            .booleanValue();

    if ("block".equalsIgnoreCase(whenExhaustedActionValue)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
    } else if ("fail".equalsIgnoreCase(whenExhaustedActionValue)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
    } else if ("grow".equalsIgnoreCase(whenExhaustedActionValue)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
    } else {
        whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION;
    }

    if ("".equals(testQuery)) {
        testQuery = null;
    }

    if (username == null) {
        username = "";
    }

    if (password == null) {
        password = "";
    }

    // read the values of the statement pool configuration specified by the given key
    boolean poolingStmts = Boolean.valueOf(config
            .getString(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_POOLING, CmsStringUtil.TRUE).trim())
            .booleanValue();
    int maxActiveStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_ACTIVE, 25);
    int maxWaitStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_WAIT, 250);
    int maxIdleStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_IDLE, 15);
    String whenStmtsExhaustedActionValue = config
            .get(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_WHEN_EXHAUSTED_ACTION);
    byte whenStmtsExhaustedAction = GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW;
    if (whenStmtsExhaustedActionValue != null) {
        whenStmtsExhaustedActionValue = whenStmtsExhaustedActionValue.trim();
        whenStmtsExhaustedAction = ("block".equalsIgnoreCase(whenStmtsExhaustedActionValue))
                ? GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK
                : ("fail".equalsIgnoreCase(whenStmtsExhaustedActionValue))
                        ? GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL
                        : GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW;
    }

    int connectionAttempts = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_CONNECT_ATTEMTS, 10);
    int connetionsWait = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_CONNECT_WAITS, 5000);

    // create an instance of the JDBC driver
    Class.forName(jdbcDriver).newInstance();

    // initialize a keyed object pool to store connections
    GenericObjectPool connectionPool = new GenericObjectPool(null);

    /* Abandoned pool configuration:
     *  
     * In case the systems encounters "pool exhaustion" (runs out of connections),
     * comment the above line with "new GenericObjectPool(null)" and uncomment the 
     * 5 lines below. This will generate an "abandoned pool" configuration that logs 
     * abandoned connections to the System.out. Unfortunatly this code is deprecated,
     * so to avoid code warnings it's also disabled here. 
     * Tested with commons-pool v 1.2.
     */

    //        AbandonedConfig abandonedConfig = new AbandonedConfig();
    //        abandonedConfig.setLogAbandoned(true);
    //        abandonedConfig.setRemoveAbandoned(true);
    //        abandonedConfig.setRemoveAbandonedTimeout(5);
    //        GenericObjectPool connectionPool = new AbandonedObjectPool(null, abandonedConfig);
    // initialize an object pool to store connections
    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxIdle(maxIdle);
    connectionPool.setMinIdle(minIdle);
    connectionPool.setMaxWait(maxWait);
    connectionPool.setWhenExhaustedAction(whenExhaustedAction);

    if (testQuery != null) {
        connectionPool.setTestOnBorrow(testOnBorrow);
        connectionPool.setTestWhileIdle(testWhileIdle);
        connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRuns);
        connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTime);
    }

    // initialize a connection factory to make the DriverManager taking connections from the pool
    if (jdbcUrlParams != null) {
        jdbcUrl += jdbcUrlParams;
    }

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, username, password);

    // Set up statement pool, if desired
    GenericKeyedObjectPoolFactory statementFactory = null;
    if (poolingStmts) {
        statementFactory = new GenericKeyedObjectPoolFactory(null, maxActiveStmts, whenStmtsExhaustedAction,
                maxWaitStmts, maxIdleStmts);
    }

    // initialize a factory to obtain pooled connections and prepared statements
    new PoolableConnectionFactory(connectionFactory, connectionPool, statementFactory, testQuery, false, true);

    // initialize a new pooling driver using the pool
    PoolingDriver driver = new PoolingDriver();
    driver.registerPool(poolUrl, connectionPool);

    Connection con = null;
    boolean connect = false;
    int connectionTests = 0;

    // try to connect once to the database to ensure it can be connected to at all
    // if the conection cannot be established, multiple attempts will be done to connect
    // just in cast the database was not fast enough to start before OpenCms was started

    do {
        try {
            // try to connect
            con = connectionFactory.createConnection();
            connect = true;
        } catch (Exception e) {
            // connection failed, increase attempts, sleept for some seconds and log a message
            connectionTests++;
            if (CmsLog.INIT.isInfoEnabled()) {
                CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_WAIT_FOR_DB_4, new Object[] {
                        poolUrl, jdbcUrl, new Integer(connectionTests), new Integer(connetionsWait) }));
            }
            Thread.sleep(connetionsWait);
        } finally {
            if (con != null) {
                con.close();
            }
        }
    } while (!connect && (connectionTests < connectionAttempts));

    if (CmsLog.INIT.isInfoEnabled()) {
        CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_JDBC_POOL_2, poolUrl, jdbcUrl));
    }
    return driver;
}

From source file:org.quickserver.net.server.impl.BasicPoolManager.java

public ObjectPool makeClientPool(PoolableObjectFactory factory, PoolConfig opConfig) {
    GenericObjectPool.Config bconfig = configurePool(opConfig);
    bconfig.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
    return new GenericObjectPool(factory, bconfig);
}

From source file:org.quickserver.net.server.impl.BasicPoolManager.java

public ObjectPool makeClientHandlerPool(PoolableObjectFactory factory, PoolConfig opConfig) {
    GenericObjectPool.Config bconfig = configurePool(opConfig);
    bconfig.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
    return new GenericObjectPool(factory, bconfig);
}

From source file:org.quickserver.net.server.impl.BasicPoolManager.java

public ObjectPool makeClientDataPool(PoolableObjectFactory factory, PoolConfig opConfig) {
    GenericObjectPool.Config bconfig = configurePool(opConfig);
    bconfig.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
    return new GenericObjectPool(factory, bconfig);
}

From source file:org.sakaiproject.nakamura.lite.storage.AbstractClientConnectionPool.java

@Activate
public void activate(Map<String, Object> properties) throws ClassNotFoundException {
    // for testing purposes
    if (configuration == null) {
        configuration = (Configuration) properties.get(Configuration.class.getName());
    }/*from ww w .ja  v a  2  s.  c  o  m*/
    indexColums = ImmutableSet.of(configuration.getIndexColumnNames());
    int maxActive = StorageClientUtils.getSetting(properties.get(MAX_ACTIVE), 200);
    byte whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION;
    String whenExhausted = (String) properties.get(WHEN_EHAUSTED);
    if ("fail".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
    } else if ("grow".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
    } else if ("block".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
    }
    long maxWait = StorageClientUtils.getSetting(properties.get(MAX_WAIT), 10L);
    int maxIdle = StorageClientUtils.getSetting(properties.get(MAX_IDLE), 5);
    boolean testOnBorrow = StorageClientUtils.getSetting(properties.get(TEST_ON_BORROW), true);
    boolean testOnReturn = StorageClientUtils.getSetting(properties.get(TEST_ON_RETURN), true);
    long timeBetweenEvictionRunsMillis = StorageClientUtils
            .getSetting(properties.get(TIME_BETWEEN_EVICTION_RUNS_MILLIS), 60000L);
    int numTestsPerEvictionRun = StorageClientUtils.getSetting(properties.get(NUM_TESTS_PER_EVICTION_RUN),
            1000);
    long minEvictableIdleTimeMillis = StorageClientUtils
            .getSetting(properties.get(MIN_EVICTABLE_IDLE_TIME_MILLIS), 10000L);
    boolean testWhileIdle = StorageClientUtils.getSetting(properties.get(TEST_WHILE_IDLE), false);

    pool = new GenericObjectPool(getConnectionPoolFactory(), maxActive, whenExhaustedAction, maxWait, maxIdle,
            testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun,
            minEvictableIdleTimeMillis, testWhileIdle);

    // set the maximum size of a string, if this is not 0, strings over this size will become files.
    StringType.setLengthLimit(StorageClientUtils.getSetting(properties.get(LONG_STRING_SIZE), 0));

}

From source file:org.sakaiproject.nakamura.lite.storage.spi.AbstractClientConnectionPool.java

@Activate
public void activate(Map<String, Object> properties) throws ClassNotFoundException {
    // for testing purposes
    if (configuration == null) {
        configuration = (Configuration) properties.get(Configuration.class.getName());
    }//from   w w w.  jav  a 2 s .c  o  m
    indexColumns = ImmutableSet.copyOf(configuration.getIndexColumnNames());
    indexColumnsTypes = ImmutableSet.copyOf(configuration.getIndexColumnTypes());
    int maxActive = StorageClientUtils.getSetting(properties.get(MAX_ACTIVE), 200);
    byte whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION;
    String whenExhausted = (String) properties.get(WHEN_EHAUSTED);
    if ("fail".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
    } else if ("grow".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
    } else if ("block".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
    }
    long maxWait = StorageClientUtils.getSetting(properties.get(MAX_WAIT), 10L);
    int maxIdle = StorageClientUtils.getSetting(properties.get(MAX_IDLE), 5);
    boolean testOnBorrow = StorageClientUtils.getSetting(properties.get(TEST_ON_BORROW), true);
    boolean testOnReturn = StorageClientUtils.getSetting(properties.get(TEST_ON_RETURN), true);
    long timeBetweenEvictionRunsMillis = StorageClientUtils
            .getSetting(properties.get(TIME_BETWEEN_EVICTION_RUNS_MILLIS), 60000L);
    int numTestsPerEvictionRun = StorageClientUtils.getSetting(properties.get(NUM_TESTS_PER_EVICTION_RUN),
            1000);
    long minEvictableIdleTimeMillis = StorageClientUtils
            .getSetting(properties.get(MIN_EVICTABLE_IDLE_TIME_MILLIS), 10000L);
    boolean testWhileIdle = StorageClientUtils.getSetting(properties.get(TEST_WHILE_IDLE), false);

    pool = new GenericObjectPool(getConnectionPoolFactory(), maxActive, whenExhaustedAction, maxWait, maxIdle,
            testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun,
            minEvictableIdleTimeMillis, testWhileIdle);

    // set the maximum size of a string, if this is not 0, strings over this size will become files.
    StringType.setLengthLimit(
            StorageClientUtils.getSetting(properties.get(LONG_STRING_SIZE), DEFAULT_LONG_STRING_SIZE));
    // location of the long string store.
    LongString.setBase(StorageClientUtils.getSetting(properties.get(LONG_STRING_STORE_BASE),
            StorageClientUtils.getSetting(properties.get(FS_STORE_BASE_DIR), DEFAULT_FILE_STORE)));

}