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

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

Introduction

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

Prototype

public void setBlockWhenExhausted(boolean blockWhenExhausted) 

Source Link

Document

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

Usage

From source file:stroom.statistics.server.sql.SQLStatisticEventStore.java

private GenericObjectPoolConfig getObjectPoolConfig() {
    final GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    // Max number of idle items .... same as our pool size
    config.setMaxIdle(poolSize);/*from  w ww.j a v a  2 s  .  co  m*/
    // Pool size
    config.setMaxTotal(poolSize);
    // Returns the minimum amount of time an object may sit idle in the pool
    // before it is eligible for eviction by the idle object evictor
    // Here if it is idle for 10 min's it will simply return It will also
    // return by validateObject if it is simple more than 10min old
    config.setMinEvictableIdleTimeMillis(poolAgeMsThreshold);
    // Check for idle objects never .... we will do this with task sytstem
    config.setTimeBetweenEvictionRunsMillis(0);
    // Must cause other threads to block to wait for a object
    config.setBlockWhenExhausted(true);
    config.setJmxEnabled(false);
    // Check item on just before returning to pool
    config.setTestOnReturn(true);

    return config;
}

From source file:survey.data.RepositoryFactory.java

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

    _properties = properties;// www . j  a v  a  2  s.co 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)");
}