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

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

Introduction

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

Prototype

public void setTestOnBorrow(boolean testOnBorrow) 

Source Link

Document

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

Usage

From source file:org.power.commons.redis.RedisClient.java

private GenericObjectPoolConfig getPoolConfig() {
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    // maxIdle?pool size???????redis
    poolConfig.setMaxTotal(this.maxTotal);
    if (this.maxIdle > 0) {
        poolConfig.setMaxIdle(this.maxIdle);
    }/*from  w w w . ja va2s  .co m*/
    poolConfig.setMaxWaitMillis(this.maxWait);
    /* if (this.whenExhaustedAction >= 0 && this.whenExhaustedAction < 3) {
    poolConfig.whenExhaustedAction = this.whenExhaustedAction;
     }*/
    poolConfig.setBlockWhenExhausted(this.blockWhenExhausted);
    poolConfig.setTestOnBorrow(this.testOnBorrow);
    poolConfig.setMinIdle(this.minIdle);
    poolConfig.setTestOnReturn(testOnReturn);
    poolConfig.setTestWhileIdle(testWhileIdle);
    poolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    poolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    poolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    poolConfig.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
    poolConfig.setLifo(lifo);
    return poolConfig;
}

From source file:org.springframework.data.redis.connection.jredis.JredisPoolTests.java

@Test
public void testGetResourceValidate() {
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setTestOnBorrow(true);
    this.pool = new JredisPool(connectionSpec, poolConfig);
    JRedis client = pool.getResource();/*w ww  .  jav a 2  s .co  m*/
    assertNotNull(client);
}

From source file:org.springframework.data.redis.connection.lettuce.DefaultLettucePoolTests.java

@Test
public void testGetResourceValidate() {

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setTestOnBorrow(true);
    pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
    pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
    pool.afterPropertiesSet();/*from   w  w w  .  j  av  a  2  s . c o  m*/
    RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
    assertNotNull(client);
    client.close();
}

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  w  w  w .java 2 s  . c o  m*/
    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  www .ja va2  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)");
}