Example usage for org.apache.commons.pool2.impl GenericObjectPoolConfig setMinIdle

List of usage examples for org.apache.commons.pool2.impl GenericObjectPoolConfig setMinIdle

Introduction

In this page you can find the example usage for org.apache.commons.pool2.impl GenericObjectPoolConfig setMinIdle.

Prototype

public void setMinIdle(int minIdle) 

Source Link

Document

Set the value for the minIdle configuration attribute for pools created with this configuration instance.

Usage

From source file:org.springframework.aop.target.CommonsPool2TargetSource.java

/**
 * Subclasses can override this if they want to return a specific Commons pool.
 * They should apply any configuration properties to the pool here.
 * <p>Default is a GenericObjectPool instance with the given pool size.
 * @return an empty Commons {@code ObjectPool}.
 * @see GenericObjectPool//from   w ww.j ava2  s .  c om
 * @see #setMaxSize
 */
protected ObjectPool createObjectPool() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(getMaxSize());
    config.setMaxIdle(getMaxIdle());
    config.setMinIdle(getMinIdle());
    config.setMaxWaitMillis(getMaxWait());
    config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
    config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
    config.setBlockWhenExhausted(isBlockWhenExhausted());
    return new GenericObjectPool(this, config);
}

From source file:org.zbus.pool.impl.CommonsPool2.java

public CommonsPool2(ObjectFactory<T> supportFactory, PoolConfig config) {
    Commons2PoolFactory factory = new Commons2PoolFactory(supportFactory);
    GenericObjectPoolConfig poolConfig = null;
    if (config.getSupport() instanceof GenericObjectPoolConfig) {
        poolConfig = (GenericObjectPoolConfig) config.getSupport();
    } else {/* ww  w  .j a v  a2 s .c om*/
        poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(config.getMaxTotal());
        poolConfig.setMaxIdle(config.getMaxIdle());
        poolConfig.setMinIdle(config.getMinIdle());
        poolConfig.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis());
    }

    this.support = new GenericObjectPool<T>(factory, poolConfig);
}

From source file:redis.client.jedis.CustomShardedJedisPoolTest.java

@BeforeClass
public void init() throws InterruptedException {
    List<JedisShardInfo> shards = RedisConfigUtils.parseRedisServerList(TestConfigUtils.getRedisServers(),
            TestConfigUtils.getTimeoutMillis());

    GenericObjectPoolConfig poolConfig = new JedisPoolConfig();
    // ?//from ww  w.ja va  2s.c  om
    poolConfig.setMaxTotal(TestConfigUtils.getMaxTotalNum());
    poolConfig.setMaxIdle(TestConfigUtils.getMaxIdleNum());
    // poolConfig.setMinIdle(TestConfigUtils.getMinIdleNum());
    poolConfig.setMinIdle(3); // local test
    // ?""
    boolean lifo = TestConfigUtils.getPoolBehaviour() == PoolBehaviour.LIFO ? true : false;
    poolConfig.setLifo(lifo);
    // ?
    poolConfig.setBlockWhenExhausted(false);
    // 
    // poolConfig.setBlockWhenExhausted(true);
    // poolConfig.setMaxWaitMillis(TimeUnit.MILLISECONDS.toMillis(10L));

    // ""?
    poolConfig.setTestOnBorrow(false);
    poolConfig.setTestOnReturn(false);

    /*
     * "Evictor?"??""
     */
    poolConfig.setTestWhileIdle(true);
    // ?5?????
    // poolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.SECONDS.toMillis(TestConfigUtils.getTimeBetweenEvictionRunsSeconds()));
    poolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.SECONDS.toMillis(2L)); // local test
    // ??Evictor
    // poolConfig.setTimeBetweenEvictionRunsMillis(-1L); // local test
    // ?
    // poolConfig.setNumTestsPerEvictionRun(TestConfigUtils.getNumTestsPerEvictionRun());
    poolConfig.setNumTestsPerEvictionRun(3); // local test
    // ?
    poolConfig.setSoftMinEvictableIdleTimeMillis(
            TimeUnit.MINUTES.toMillis(TestConfigUtils.getMinEvictableIdleTimeMinutes()));
    // ??(?)
    // ?
    poolConfig.setMinEvictableIdleTimeMillis(
            TimeUnit.MINUTES.toMillis(TestConfigUtils.getMaxEvictableIdleTimeMinutes()));

    this.shardedJedisPool = new CustomShardedJedisPool(poolConfig, shards, (int) TimeUnit.SECONDS.toMillis(1),
            2);
}

From source file:survey.data.RepositoryFactory.java

public RepositoryFactory(Properties properties) {
    assert properties != null;

    _properties = properties;/*from  ww  w.  j a va  2 s .c o  m*/
    _localCache = new LocalCache(properties);
    String redisHost = _properties == null ? null : _properties.getProperty("redis.host");
    if (redisHost == null) {
        redisHost = "localhost";
    }
    int redisPort;
    try {
        redisPort = Integer.parseInt(_properties.getProperty("redis.port"));
    } catch (Throwable e) {
        redisPort = 6379;
    }
    // Anedotally, the web service will eventually fail due to a broken
    // connection to Redis, unless a pool is used.
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setBlockWhenExhausted(false); // Fail if out of sockets.
    poolConfig.setLifo(true);
    poolConfig.setMaxIdle(50); // All 50 sockets can be briefly idle.
    poolConfig.setMaxTotal(50); // After 50 sockets used then fail.
    poolConfig.setMinEvictableIdleTimeMillis(600000); // 10 minutes even if few sockets are idle.
    poolConfig.setMinIdle(2); // Keep 2 sockets ready.
    poolConfig.setSoftMinEvictableIdleTimeMillis(120000); // 2 minutes unless insufficient sockets are idle.
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestOnReturn(false);
    poolConfig.setTestWhileIdle(false);
    poolConfig.setTimeBetweenEvictionRunsMillis(300000); // Check for idle sockets every 5 minutes.
    _jedisPool = new JedisPool(poolConfig, redisHost, redisPort);

    // Monitor changes in Redis.
    _subscribeService = Executors.newSingleThreadExecutor();

    final JedisPubSub jedisPubSub = new JedisPubSub() {
        @Override
        public void onMessage(String channelName, String notificationKey) {
            // _logger.info(String.format("onMessage(%s, %s)", channelName, notificationKey));
            // See the publish() method below.
            String[] splitKey = notificationKey.split(":", 4);
            assert splitKey.length >= 4;
            String magic = splitKey[0];
            assert "survey".equals(magic);
            String namespaceName = splitKey[1];
            String surveyPath = splitKey[2];
            long turnout;
            try {
                turnout = Long.parseLong(splitKey[3]);
            } catch (NumberFormatException e) {
                turnout = 0;
            }
            onVoteAdded(namespaceName, surveyPath, turnout);
        }
    };

    _subscribeService.submit(new Runnable() {
        @Override
        public void run() {
            _logger.info("subscribe thread running");
            int sleepMillis = 0;
            for (;;) {
                Jedis jedis = null;
                try {
                    if (sleepMillis != 0) {
                        Thread.sleep(sleepMillis);
                    }
                    jedis = _jedisPool.getResource();
                    jedis.subscribe(jedisPubSub, _CHANNEL_NAME);
                } catch (InterruptedException e) {
                    _logger.info("subscribe thread interrupted");
                    break;
                } catch (Throwable e) {
                    // e.g. if there is a Jedis exception, sleep 1 minute and try again.
                    sleepMillis = 60000;
                } finally {
                    if (jedis != null) {
                        _jedisPool.returnResourceObject(jedis);
                    }
                }
            }
        }
    });

    _logger = LoggerFactory.getLogger(RepositoryFactory.class);
    _logger.info("created repository factory (singleton)");
}

From source file:za.co.wilderness.WildernessPoolingDriver.java

private void setupDriver() throws Exception {

    String jdbcDriverName = "com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource";

    try {//from w w  w. ja  va2  s . c  o m
        java.lang.Class.forName(jdbcDriverName).newInstance();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        //System.out.println("Error when attempting to obtain DB Driver: " + jdbcDriverName + " on "+ new Date().toString() + e.getMessage());
        logger.log(Level.SEVERE,
                "Error when attempting to obtain DB Driver: " + jdbcDriverName + " on " + new Date().toString(),
                e);
        throw new Exception(e);
    }

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(this.connectURI, this.properties);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);

    GenericObjectPoolConfig genConfig = new GenericObjectPoolConfig();
    genConfig.setMaxIdle(this.config.getMaxIdle());
    genConfig.setMaxTotal(this.config.getMaxActive());
    genConfig.setMinIdle(this.config.getMinIdle());
    genConfig.setMaxWaitMillis(this.config.getMaxWaitMillis());
    genConfig.setTimeBetweenEvictionRunsMillis(5000);
    genConfig.setTestWhileIdle(true);

    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory,
            genConfig);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself...
    //
    Class.forName("org.apache.commons.dbcp2.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    //System.out.println("Driver : " + driver.toString());
    logger.log(Level.FINE, "Driver : " + driver.toString());

    //
    // ...and register our pool with it.
    //
    driver.registerPool(EXTERNAL_SERVICE.name(), connectionPool);

    //
    // Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
    // to access our pool of Connections.
    //
}