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

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

Introduction

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

Prototype

public void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis) 

Source Link

Document

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

Usage

From source file:survey.data.RepositoryFactory.java

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

    _properties = properties;/*from   w  w w  . j  a  v a  2 s.c om*/
    _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)");
}