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:com.spokentech.speechdown.server.util.pool.ObjectPoolUtil.java

/**
 * TODOC//  w w  w  .j  a  v a  2  s.  com
 * @param maxActive
 * @return
 */
public static GenericObjectPool.Config getGenericObjectPoolConfig(int maxActive) {
    GenericObjectPool.Config config = new GenericObjectPool.Config();

    config.maxActive = maxActive;
    config.maxIdle = -1;
    config.maxWait = 200;
    config.minEvictableIdleTimeMillis = -1;
    config.minIdle = config.maxActive;
    config.numTestsPerEvictionRun = -1;
    //config.softMinEvictableIdleTimeMillis   = -1;
    config.testOnBorrow = false;
    config.testOnReturn = false;
    config.testWhileIdle = false;
    config.timeBetweenEvictionRunsMillis = -1;
    config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;

    return config;
}

From source file:com.lithium.flow.util.ConfigObjectPool.java

private static Config buildConfig(@Nonnull com.lithium.flow.config.Config config) {
    Config poolConfig = new Config();
    poolConfig.lifo = config.getBoolean("pool.lifo", true);
    poolConfig.maxActive = config.getInt("pool.maxActive", Runtime.getRuntime().availableProcessors());
    poolConfig.maxIdle = config.getInt("pool.maxIdle", -1);
    poolConfig.minIdle = config.getInt("pool.minIdle", 0);
    poolConfig.testOnBorrow = config.getBoolean("pool.testOnBorrow", false);
    poolConfig.testOnReturn = config.getBoolean("pool.testOnReturn", false);
    poolConfig.timeBetweenEvictionRunsMillis = config.getTime("pool.timeBetweenEvictionRunsMillis", "-1");
    poolConfig.minEvictableIdleTimeMillis = config.getTime("pool.minEvictableIdleTimeMillis", "30m");
    poolConfig.testWhileIdle = config.getBoolean("pool.testWhileIdle", false);
    poolConfig.softMinEvictableIdleTimeMillis = config.getTime("pool.softMinEvictableIdleTimeMillis", "-1");
    poolConfig.numTestsPerEvictionRun = config.getInt("pool.numTestsPerEvictionRun", 3);

    String action = config.getString("pool.whenExhaustedAction", "block");
    switch (action) {
    case "fail":
        poolConfig.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
        break;//from  w w w  .  j av  a 2s  . c o m
    case "block":
        poolConfig.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
        break;
    case "grow":
        poolConfig.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
        break;
    default:
        throw new IllegalConfigException("pool.whenExhaustedAction", action, "string", null);
    }

    return poolConfig;
}

From source file:com.centurylink.mdw.services.pooling.MDWConnectionPool.java

/**
 * This can be overriden to set pool size and borrow timeout
 * for each start/restart.//  w  ww  .ja  v a2s.c  om
 * The super method must be called at the end of the overriding method.
 * @throws Exception
 */
public synchronized void start() throws Exception {
    if (pool == null) {
        pool = new GenericObjectPool(new MDWPoolFactory());
    }
    pool.setMaxActive(pool_size);
    if (borrow_timeout < 0) {
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
        pool.setMaxWait(-1);
    } else if (borrow_timeout == 0) {
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
        pool.setMaxWait(0);
    } else {
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
        pool.setMaxWait(borrow_timeout * 1000);
    }
    setStarted(true);
}

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

/**
 * Constructor for BasicDataSource./*from   ww w. j  av  a  2  s  . c om*/
 * @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:de.hybris.platform.impex.jalo.PLA_12772_Test.java

ThreadPool createWorkerThreadPool() {
    final Tenant tenant = Registry.getCurrentTenantNoFallback();
    final ConfigIntf cfg = tenant.getConfig();
    final int poolSize = cfg.getInt("workers.maxthreads", 64);

    final ThreadPool ret = new ThreadPool(tenant.getTenantID(), poolSize);

    final GenericObjectPool.Config config = new GenericObjectPool.Config();
    config.maxActive = poolSize;/*from  www.  jav a  2s  .c  om*/
    config.maxIdle = poolSize;
    config.maxWait = -1;
    config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
    config.testOnBorrow = true;
    config.testOnReturn = true;
    config.timeBetweenEvictionRunsMillis = 30 * 1000; // keep idle threads for at most 30 sec
    ret.setConfig(config);

    return ret;
}

From source file:net.ontopia.topicmaps.impl.rdbms.RDBMSTopicMapReference.java

protected void init() {
    // store factory
    TopicMapStoreFactoryIF sfactory = new TopicMapStoreFactoryIF() {
        public TopicMapStoreIF createStore() {
            return _createStore(false);
        }//from  w ww. j  ava  2  s.c om
    };

    // initialize pool
    this.ofactory = new StorePoolableObjectFactory(sfactory);
    this.pool = new GenericObjectPool(ofactory);
    this.pool.setTestOnBorrow(true);

    Map properties = storage.getProperties();
    if (properties != null) {
        // Set minimum pool size (default: 0)
        String _minsize = PropertyUtils.getProperty(properties,
                "net.ontopia.topicmaps.impl.rdbms.StorePool.MinimumSize", false);
        int minsize = (_minsize == null ? 0 : Integer.parseInt(_minsize));
        log.debug("Setting StorePool.MinimumSize '" + minsize + "'");
        pool.setMinIdle(minsize); // 0 = no limit

        // Set maximum pool size (default: Integer.MAX_VALUE)
        String _maxsize = PropertyUtils.getProperty(properties,
                "net.ontopia.topicmaps.impl.rdbms.StorePool.MaximumSize", false);
        int maxsize = (_maxsize == null ? 8 : Integer.parseInt(_maxsize));
        log.debug("Setting StorePool.MaximumSize '" + maxsize + "'");
        pool.setMaxActive(maxsize); // 0 = no limit

        // Set soft maximum - emergency objects (default: false)
        boolean softmax = PropertyUtils.isTrue(properties,
                "net.ontopia.topicmaps.impl.rdbms.StorePool.SoftMaximum", false);
        log.debug("Setting StorePool.SoftMaximum '" + softmax + "'");
        if (softmax)
            pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
        else
            pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    }

    // allow the user to fully overwrite exhausted options
    String _whenExhaustedAction = PropertyUtils.getProperty(properties,
            "net.ontopia.topicmaps.impl.rdbms.StorePool.WhenExhaustedAction", false);
    if (EXHAUSED_BLOCK.equals(_whenExhaustedAction))
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    if (EXHAUSED_GROW.equals(_whenExhaustedAction))
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    if (EXHAUSED_FAIL.equals(_whenExhaustedAction))
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);

    if (pool.getWhenExhaustedAction() == GenericObjectPool.WHEN_EXHAUSTED_BLOCK)
        log.debug("Pool is set to block on exhaused");
    if (pool.getWhenExhaustedAction() == GenericObjectPool.WHEN_EXHAUSTED_GROW)
        log.debug("Pool is set to grow on exhaused");
    if (pool.getWhenExhaustedAction() == GenericObjectPool.WHEN_EXHAUSTED_FAIL)
        log.debug("Pool is set to fail on exhaused");

}

From source file:com.jfinal.ext.plugin.redis.JedisPlugin.java

private void parseSetting(String key, String value) {
    if ("timeout".equalsIgnoreCase(key)) {
        timeout = Integer.valueOf(value);
    } else if ("password".equalsIgnoreCase(key)) {
        password = value;/*  www .j  a va 2  s.  c  om*/
    } else if ("host".equalsIgnoreCase(key)) {
        host = value;
    } else if ("maxactive".equalsIgnoreCase(key)) {
        maxactive = Integer.valueOf(value);
    } else if ("maxidle".equalsIgnoreCase(key)) {
        maxidle = Integer.valueOf(value);
    } else if ("maxwait".equalsIgnoreCase(key)) {
        maxwait = Integer.valueOf(value);
    } else if ("minevictableidletimemillis".equalsIgnoreCase(key)) {
        minevictableidletimemillis = Long.valueOf(value);
    } else if ("minidle".equalsIgnoreCase(key)) {
        minidle = Integer.valueOf(value);
    } else if ("numtestsperevictionrun".equalsIgnoreCase(key)) {
        numtestsperevictionrun = Integer.valueOf(value);
    } else if ("softminevictableidletimemillis".equalsIgnoreCase(key)) {
        softminevictableidletimemillis = Long.valueOf(value);
    } else if ("timebetweenevictionrunsmillis".equalsIgnoreCase(key)) {
        timebetweenevictionrunsmillis = Long.valueOf(value);
    } else if ("whenexhaustedaction".equalsIgnoreCase(key)) {
        if ("WHEN_EXHAUSTED_BLOCK".equalsIgnoreCase(value)) {
            whenexhaustedaction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
        } else if ("WHEN_EXHAUSTED_FAIL".equalsIgnoreCase(value)) {
            whenexhaustedaction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
        } else if ("WHEN_EXHAUSTED_GROW".equalsIgnoreCase(value)) {
            whenexhaustedaction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
        }
    } else if ("testwhileidle".equalsIgnoreCase(key)) {
        testwhileidle = Boolean.getBoolean(value);
    } else if ("testonreturn".equalsIgnoreCase(key)) {
        testonreturn = Boolean.getBoolean(value);
    } else if ("testonborrow".equalsIgnoreCase(key)) {
        testonborrow = Boolean.getBoolean(value);
    }
}

From source file:com.att.cspd.SimpleSipServlet.java

@Override
public void init(ServletConfig servletConfig) throws ServletException {
    logger.info("the simple sip servlet has been started");
    super.init(servletConfig);

    RABBITMQ_CONN_URL = servletConfig.getInitParameter("rabbitmq_conn_url");
    EXCHANGE_NAME = servletConfig.getInitParameter("exchange_name");
    String pool_max_active = servletConfig.getInitParameter("rabbitmq_pool_max_active");
    String pool_max_idle = servletConfig.getInitParameter("rabbitmq_pool_max_idle");
    String pool_min_idle = servletConfig.getInitParameter("rabbitmq_pool_min_idle");
    String time_between_evication = servletConfig.getInitParameter("rabbitmq_pool_time_between_eviction");
    String idle_time_before_eviction = servletConfig
            .getInitParameter("rabbitmq_pool_idle_time_before_eviction");

    logger.info("INIT PARAM:  rabbitmq_conn_url = " + RABBITMQ_CONN_URL);
    logger.info("INIT PARAM : exchange name  = " + EXCHANGE_NAME);
    logger.info("INIT PARAM : pool max active = " + pool_max_active);
    logger.info("INIT PARAM : pool max idle  = " + pool_max_idle);
    logger.info("INIT PARAM : pool min idle = " + pool_min_idle);
    logger.info("INIT PARAM : time_between_evication  = " + time_between_evication);
    logger.info("INIT PARAM : idle_time_before_eviction  = " + idle_time_before_eviction);

    try {/*from  ww  w  .j a  v a 2 s . co  m*/
        POOL_MAX_ACTIVE = Integer.parseInt(pool_max_active);
    } catch (NumberFormatException e) {
        logger.error("Impossible to parse the pool max active : " + pool_max_active, e);
    }

    try {
        POOL_MAX_IDLE = Integer.parseInt(pool_max_idle);
    } catch (NumberFormatException e) {
        logger.error("Impossible to parse the pool max idle : " + pool_max_idle, e);
    }

    try {
        POOL_MIN_IDLE = Integer.parseInt(pool_min_idle);
    } catch (NumberFormatException e) {
        logger.error("Impossible to parse the pool min idle  : " + pool_min_idle, e);
    }

    try {
        TIME_BETWEEN_EVICTION = Integer.parseInt(time_between_evication);
    } catch (NumberFormatException e) {
        logger.error("Impossible to parse the time between eviction : " + time_between_evication, e);
    }
    try {
        IDL_TIME_BEFORE_EVICTION = Integer.parseInt(idle_time_before_eviction);
    } catch (NumberFormatException e) {
        logger.error("Impossible to parse idle time before eviction : " + idle_time_before_eviction, e);
    }

    /**
     * create static instance of rabbitmq connection pool
     */
    try {

        GenericObjectPool.Config config = new GenericObjectPool.Config();
        config.maxActive = POOL_MAX_ACTIVE;
        config.maxIdle = POOL_MAX_IDLE;
        config.minIdle = POOL_MIN_IDLE;
        config.timeBetweenEvictionRunsMillis = TIME_BETWEEN_EVICTION;
        config.minEvictableIdleTimeMillis = IDL_TIME_BEFORE_EVICTION;
        config.testOnBorrow = true;
        config.testOnReturn = true;
        config.lifo = GenericObjectPool.DEFAULT_LIFO;
        config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;

        pool = new GenericObjectPool<Channel>(new ConnectionPoolableObjectFactory(RABBITMQ_CONN_URL), config);

        //create an initial pool instances.
        /*
        int initSize = 25;
        for (int i =0; i < initSize; i++) {
           try {
              pool.addObject();
           } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
           }
        }
        */

        /*
        pool.setMaxActive(POOL_MAX_ACTIVE);   //maximum connection allowed in the pool (100). If reached, worker thread needs to be blocked to wait.
        pool.setMinIdle(POOL_MIN_IDLE);       //keep minimum idle connection (set to 3) in pool in all time.
        pool.setMaxIdle(POOL_MAX_IDLE);      //No minimum to create new connection when needed (set to -1).
        pool.setTimeBetweenEvictionRunsMillis(TIME_BETWEEN_EVICTION);   //wait up eviction thread in 10 second
        pool.setMinEvictableIdleTimeMillis(IDL_TIME_BEFORE_EVICTION);  //kill the idle connection that is 5 second old
        pool.setTestOnBorrow(true);   //sanity checking when getting connection from pool.
        pool.setTestOnReturn(true);   //sanity check when returning connection to pool.
        */

    } catch (IOException ex) {

        logger.error("RabbitMQ Pool failed to create. Error = " + ex.getMessage());
        throw new ServletException(ex);
    }

    //logger.info("HELLO... FINISHED LOADING THE RABBITMQ CONNECTION/CHANNEL POOL");

}

From source file:com.flexoodb.pool.ConnectionPool.java

public boolean reconnect() {
    boolean result = false;
    Connection conn = null;//from   w  w  w  .j  a v a  2 s. c o m

    try {
        if (!_reconnecting) {
            if (_autoreturn) {

                _borrowed.clear();

            }
            _reconnecting = true;

            if (connections != null) {
                disconnect();
            }
            connections = new GenericObjectPool(new ConnectionPoolFactory(driverClassName.trim(), dbURL.trim(),
                    user.trim(), password.trim()));
            connections.setTestWhileIdle(true);
            connections.setTimeBetweenEvictionRunsMillis(600000); // 30 seconds before it checks for evictable objects.
            connections.setMinEvictableIdleTimeMillis(600000); // connection pool can be idle for 2 minutes
            connections.setMinIdle(this.initconnections);
            connections.setMaxActive(this.maxconnections);
            connections.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);

            _connected = true;

            conn = (Connection) getConnection();

            _connected = (conn != null);
            _reconnecting = false;
        } else {
            //_log.warn("Database re-connection already on-going, re-connection ignored.");
        }
    } catch (Exception e) {
        result = false;
        _reconnecting = false;
        //_log.error(e);
        e.printStackTrace();
    } finally {
        if (_connected) {
            result = true;
            releaseConnection(conn);
        }
    }

    return result;

}

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

public void testWhenExhaustedFail() throws Exception {
    pool.setMaxActive(1);//  w ww.j a  v a 2  s . c  o m
    pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
    Object obj1 = pool.borrowObject();
    assertNotNull(obj1);
    try {
        pool.borrowObject();
        fail("Expected NoSuchElementException");
    } catch (NoSuchElementException e) {
        // expected
    }
    pool.returnObject(obj1);
    assertEquals(1, pool.getNumIdle());
    pool.close();
}