Java Code Examples for redis.clients.jedis.JedisPoolConfig#setTestOnBorrow()

The following examples show how to use redis.clients.jedis.JedisPoolConfig#setTestOnBorrow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisUtils.java    From nifi with Apache License 2.0 7 votes vote down vote up
private static JedisPoolConfig createJedisPoolConfig(final PropertyContext context) {
    final JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(context.getProperty(RedisUtils.POOL_MAX_TOTAL).asInteger());
    poolConfig.setMaxIdle(context.getProperty(RedisUtils.POOL_MAX_IDLE).asInteger());
    poolConfig.setMinIdle(context.getProperty(RedisUtils.POOL_MIN_IDLE).asInteger());
    poolConfig.setBlockWhenExhausted(context.getProperty(RedisUtils.POOL_BLOCK_WHEN_EXHAUSTED).asBoolean());
    poolConfig.setMaxWaitMillis(context.getProperty(RedisUtils.POOL_MAX_WAIT_TIME).asTimePeriod(TimeUnit.MILLISECONDS));
    poolConfig.setMinEvictableIdleTimeMillis(context.getProperty(RedisUtils.POOL_MIN_EVICTABLE_IDLE_TIME).asTimePeriod(TimeUnit.MILLISECONDS));
    poolConfig.setTimeBetweenEvictionRunsMillis(context.getProperty(RedisUtils.POOL_TIME_BETWEEN_EVICTION_RUNS).asTimePeriod(TimeUnit.MILLISECONDS));
    poolConfig.setNumTestsPerEvictionRun(context.getProperty(RedisUtils.POOL_NUM_TESTS_PER_EVICTION_RUN).asInteger());
    poolConfig.setTestOnCreate(context.getProperty(RedisUtils.POOL_TEST_ON_CREATE).asBoolean());
    poolConfig.setTestOnBorrow(context.getProperty(RedisUtils.POOL_TEST_ON_BORROW).asBoolean());
    poolConfig.setTestOnReturn(context.getProperty(RedisUtils.POOL_TEST_ON_RETURN).asBoolean());
    poolConfig.setTestWhileIdle(context.getProperty(RedisUtils.POOL_TEST_WHILE_IDLE).asBoolean());
    return poolConfig;
}
 
Example 2
Source File: RedisClient.java    From Redis_Learning with Apache License 2.0 6 votes vote down vote up
/**
 * ��ʼ����Ƭ��
 */
private void initialShardedPool() {
	// �ػ�������
	JedisPoolConfig config = new JedisPoolConfig();
	config.setMaxActive(20);
	config.setMaxIdle(5);
	config.setMaxWait(1000l);
	config.setTestOnBorrow(false);

	List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
	JedisShardInfo infoA = new JedisShardInfo(ADDR, PORT);
	infoA.setPassword("redis");
	shards.add(infoA);
	// ���Dz��� ��ʱ���������ķ������������ӷ���������
	// JedisShardInfo infoB = new JedisShardInfo(SUB_ADDR, PORT2);
	// infoB.setPassword("redis");
	// shards.add(infoB);
	// shards = Arrays.asList(infoA,infoB);
	shardedJedisPool = new ShardedJedisPool(config, shards,
			Hashing.MURMUR_HASH, ShardedJedis.DEFAULT_KEY_TAG_PATTERN);
}
 
Example 3
Source File: AbstractRedisCacheClient.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
/**
 * 获取poolconfig
 *
 * @return
 */
public JedisPoolConfig getPoolConfig() {
    JedisPoolConfig config = new JedisPoolConfig();
    if (getMaxIdle() != 0) {
        config.setMaxIdle(getMaxIdle());
    }
    if (getMinIdle() != 0) {
        config.setMinIdle(getMinIdle());
    }
    if (getMaxWaitMillis() != 0) {
        config.setMaxWaitMillis(getMaxWaitMillis());
    }
    if (getMaxTotal() != 0) {
        config.setMaxTotal(getMaxTotal());
    }
    config.setTestOnBorrow(true);
    config.setTestWhileIdle(true);
    return config;
}
 
Example 4
Source File: RedisClientFactory.java    From framework with Apache License 2.0 6 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @return <br>
 */
private static JedisPoolConfig getConfig() {
    JedisPoolConfig config = new JedisPoolConfig();

    // 最大连接数, 默认30个
    config.setMaxTotal(PropertyHolder.getIntProperty("message.redis.max.total", MAX_TOTLE));

    // 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
    config.setMaxIdle(PropertyHolder.getIntProperty("message.redis.max.idle", MAX_IDLE));

    // 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
    config.setMaxWaitMillis(
        GlobalConstants.SECONDS * PropertyHolder.getIntProperty("message.redis.max.wait", MAX_WAIT));

    // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
    config.setTestOnBorrow(PropertyHolder.getBooleanProperty("message.redis.testonborrow", VALIDATE));

    return config;
}
 
Example 5
Source File: RedisUtil.java    From zhcc-server with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化Redis连接池
 */
private static void initialPool() {
	try {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(MAX_ACTIVE);
		config.setMaxIdle(MAX_IDLE);
		config.setMaxWaitMillis(MAX_WAIT);
		config.setTestOnBorrow(TEST_ON_BORROW);
		jedisPool = new JedisPool(config, IP, PORT, TIMEOUT);
	} catch (Exception e) {
		LOGGER.error("First create JedisPool error : " + e);
	}
}
 
Example 6
Source File: RedisClient.java    From Redis_Learning with Apache License 2.0 5 votes vote down vote up
/**
 * ��ʼ������Ƭ��
 */
private void initialPool() {
	// �ػ�������
	JedisPoolConfig config = new JedisPoolConfig();
	config.setMaxActive(MAX_ACTIVE);
	config.setMaxIdle(MAX_IDLE);
	config.setMaxWait(MAX_WAIT);
	config.setTestOnBorrow(TEST_ON_BORROW);

	jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, PASSWORD);
}
 
Example 7
Source File: JedisIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
private JedisPoolConfig buildPoolConfig() {
    final JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(128);
    poolConfig.setMaxIdle(128);
    poolConfig.setMinIdle(16);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestOnReturn(true);
    poolConfig.setTestWhileIdle(true);
    poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
    poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
    poolConfig.setNumTestsPerEvictionRun(3);
    poolConfig.setBlockWhenExhausted(true);
    return poolConfig;
}
 
Example 8
Source File: BootStrap.java    From MyBlog with Apache License 2.0 5 votes vote down vote up
/*********************************************************************************************************/
//redisCluster设置
@Bean(destroyMethod = "close")
public JedisCluster getJedisCluster() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    // 最大空闲数
    jedisPoolConfig.setMaxIdle(10);
    // 连接池的最大数据库连接数
    jedisPoolConfig.setMaxTotal(30);
    // 最大建立连接等待时间
    jedisPoolConfig.setMaxWaitMillis(1500);
    // 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
    jedisPoolConfig.setMinEvictableIdleTimeMillis(1800000);
    // 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
    jedisPoolConfig.setNumTestsPerEvictionRun(3);
    // 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
    jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
    // 连接空闲多久后释放,当空闲时间大于该值且空闲连接大于最大空闲连接数时释放
    jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(10000);
    // 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
    jedisPoolConfig.setTestOnBorrow(true);
    // 在空闲时检查有效性, 默认false
    jedisPoolConfig.setTestWhileIdle(true);
    // 连接耗尽时是否阻塞,false报异常,true阻塞直到超时,默认true
    jedisPoolConfig.setBlockWhenExhausted(false);
    Set<HostAndPort> nodeSet = Sets.newHashSet();
    nodeSet.add(new HostAndPort("127.0.0.1", 6381));
    nodeSet.add(new HostAndPort("127.0.0.1", 6382));
    nodeSet.add(new HostAndPort("127.0.0.1", 6383));
    nodeSet.add(new HostAndPort("127.0.0.1", 6384));
    nodeSet.add(new HostAndPort("127.0.0.1", 6385));
    nodeSet.add(new HostAndPort("127.0.0.1", 6386));
    return new JedisCluster(nodeSet, 2000, 100, jedisPoolConfig);
}
 
Example 9
Source File: JedisPoolFactoryBean.java    From juice with Apache License 2.0 5 votes vote down vote up
@Override
public JedisPool getObject() throws Exception {
    logger.info("jedis init host:{} port:{} timeout:{} password:{}", host, port, timeout, password);
    if (port==null) {
        port = DEFAULT_PORT;
    }
    if (timeout==null) {
        timeout = DEFAULT_TIMEOUT;
    }
    logger.info("jedis pool init maxTotal:{} maxIdle:{} minIdle:{}", maxTotal, maxIdle, minIdle);
    //pool config
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    if (maxTotal!=null) {
        poolConfig.setMaxTotal(maxTotal);
    }
    if (maxIdle!=null) {
        poolConfig.setMaxIdle(maxIdle);
    }
    if (minIdle!=null) {
        poolConfig.setMinIdle(minIdle);
    }

    poolConfig.setTestOnBorrow(testOnBorrow == null ? Boolean.FALSE : testOnBorrow);
    poolConfig.setTestOnReturn(testOnReturn == null ? Boolean.FALSE : testOnReturn);
    poolConfig.setTestWhileIdle(testWhileIdle == null ? Boolean.TRUE : testWhileIdle);

    this.pool = new JedisPool(poolConfig, host, port, timeout, password);
    return pool;
}
 
Example 10
Source File: RedisConfiguration.java    From heimdall with Apache License 2.0 5 votes vote down vote up
/**
 * Configures and returns a {@link JedisPoolConfig}.
 * 
 * @return {@link JedisPoolConfig}
 */
public JedisPoolConfig jediPoolConfig() {
     final JedisPoolConfig poolConfig = new JedisPoolConfig();
     poolConfig.setMaxTotal(property.getRedis().getMaxTotal());
     poolConfig.setMaxIdle(property.getRedis().getMaxIdle());
     poolConfig.setMinIdle(property.getRedis().getMinIdle());
     poolConfig.setTestOnBorrow(property.getRedis().isTestOnBorrow());
     poolConfig.setTestOnReturn(property.getRedis().isTestOnReturn());
     poolConfig.setTestWhileIdle(property.getRedis().isTestWhileIdle());
     poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(property.getRedis().getMinEvictableIdleTimeSeconds()).toMillis());
     poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(property.getRedis().getTimeBetweenEvictionRunsSeconds()).toMillis());
     poolConfig.setNumTestsPerEvictionRun(property.getRedis().getNumTestsPerEvictionRun());
     poolConfig.setBlockWhenExhausted(property.getRedis().isBlockWhenExhausted());
     return poolConfig;
}
 
Example 11
Source File: SpringDataRedis.java    From howsun-javaee-framework with Apache License 2.0 5 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
	JedisShardInfo jedisShardInfo1 = new JedisShardInfo(ip1);
	jedisShardInfo1.setPassword(JedisConstant.password);
	JedisShardInfo jedisShardInfo2 = new JedisShardInfo(ip2);
	jedisShardInfo2.setPassword(JedisConstant.password);

	List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();
	jedisShardInfos.add(jedisShardInfo1);
	jedisShardInfos.add(jedisShardInfo2);

	JedisPoolConfig poolConfig = new JedisPoolConfig();
	poolConfig.setMaxActive(JedisConstant.maxActive);
	poolConfig.setMaxIdle(JedisConstant.maxIdle);
	poolConfig.setMaxWait(JedisConstant.maxWait);
	poolConfig.setTestOnBorrow(JedisConstant.testOnBorrow);
	poolConfig.setTestOnReturn(JedisConstant.testOnReturn);


	ShardedJedisPool shardedJedisPool = new ShardedJedisPool(poolConfig, jedisShardInfos);

	JedisConnectionFactory factory = new JedisConnectionFactory(jedisShardInfo1);
	StringRedisTemplate template = new StringRedisTemplate(factory);
	for (int i = 0; i < 2000; i++) {
		String key = "howsun_" + i;
		BoundValueOperations<String, String> v = template.boundValueOps(key);
		//jedis.set(key, UUID.randomUUID().toString());
		System.out.println(key + "\t" + v.get() + "\t" + factory.getHostName());
	}

}
 
Example 12
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloseConnectionOnMakeObject() {
  JedisPoolConfig config = new JedisPoolConfig();
  config.setTestOnBorrow(true);
  JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, "wrong pass");
  Jedis jedis = new Jedis("redis://:foobared@localhost:6379/");
  int currentClientCount = getClientCount(jedis.clientList());
  try {
    pool.getResource();
    fail("Should throw exception as password is incorrect.");
  } catch (Exception e) {
    assertEquals(currentClientCount, getClientCount(jedis.clientList()));
  }

}
 
Example 13
Source File: RedisPools.java    From pinlater with Apache License 2.0 5 votes vote down vote up
private static JedisPool createRedisPool(
    String host, int port, int poolSize, int maxWaitMillis, int socketTimeoutMillis) {
  JedisPoolConfig config = new JedisPoolConfig();
  config.setMaxWait(maxWaitMillis);
  config.setMaxActive(poolSize);
  config.setMaxIdle(poolSize);
  // Deal with idle connection eviction.
  config.setTestOnBorrow(false);
  config.setTestOnReturn(false);
  config.setTestWhileIdle(true);
  config.setMinEvictableIdleTimeMillis(5 * 60 * 1000);
  config.setTimeBetweenEvictionRunsMillis(3 * 60 * 1000);
  config.setNumTestsPerEvictionRun(poolSize);

  JedisPool pool = new JedisPool(config, host, port, socketTimeoutMillis);
  // Force connection pool initialization.
  Jedis jedis = null;
  try {
    jedis = pool.getResource();
  } catch (JedisConnectionException e) {
    LOG.error(
        String.format("Failed to get a redis connection when creating redis pool, "
            + "host: %s, port: %d", host, port),
        e);
  } finally {
    pool.returnResource(jedis);
  }
  return pool;
}
 
Example 14
Source File: RedisPool.java    From redis-distributed-lock with Apache License 2.0 5 votes vote down vote up
private static void init() {
    maxTotal = Integer.parseInt(PropertiesUtil.getProperty("redis.max.total", "20"));
    maxIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle","20"));
    minIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle","10"));
    testOnBorrow = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borrow","true"));
    testOnReturn = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.return","true"));
    redisTimeout = Integer.valueOf(PropertiesUtil.getProperty("redis.server.timeout", "3000"));

    redisIp = PropertiesUtil.getProperty("redis.ip");
    if (redisIp == null) {
        throw new RuntimeException("请检查redis服务端ip配置项redis.ip是否配置");
    }
    redisPort = Integer.parseInt(PropertiesUtil.getProperty("redis.port"));
    if (redisPort == null) {
        throw new RuntimeException("请检查redis服务端port配置项redis.port是否配置");
    }

    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(maxTotal);
    config.setMaxIdle(maxIdle);
    config.setMinIdle(minIdle);
    config.setTestOnBorrow(testOnBorrow);
    config.setTestOnReturn(testOnReturn);
    /**连接耗尽的时候,是否阻塞,false会抛出异常,true阻塞直到超时。默认为true*/
    config.setBlockWhenExhausted(true);

    pool = new JedisPool(config, redisIp, redisPort, redisTimeout);
}
 
Example 15
Source File: RedisClientPool.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
private JedisPoolConfig getJedisPoolConfig(final RedisConfig conf) {
    Assert.notNull(conf);

    Integer maxTotal = conf.getMaxTotal();
    if(maxTotal == null) {
        maxTotal = DEFAULT_MAX_TOTAL;
    }
    
    Integer maxIdle = conf.getMaxIdle();
    if(maxIdle == null) {
        maxIdle = DEFAULT_MAX_IDLE;
    }
    
    Integer minIdle = conf.getMinIdle();
    if(minIdle == null) {
        minIdle = DEFAULT_MIN_IDLE;
    }
    
    Boolean testOnBorrow = conf.getTestOnBorrow();
    if(testOnBorrow == null) {
        testOnBorrow = DEFAULT_TEST_ON_BORROW;
    }
    
    final JedisPoolConfig poolConf = new JedisPoolConfig();
    poolConf.setMaxTotal(maxTotal);
    poolConf.setMaxIdle(maxIdle);
    poolConf.setTestOnBorrow(testOnBorrow);
    poolConf.setMinIdle(minIdle);
    return poolConf;
}
 
Example 16
Source File: RedisConnectionPoolConfig.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public JedisPoolConfig getJedisPoolConfig() {
	JedisPoolConfig config = new JedisPoolConfig();
	config.setMaxTotal(this.maxTotal);
	config.setMaxIdle(this.maxIdle);
	config.setMinIdle(this.minIdle);
	config.setMaxWaitMillis(this.maxWaitMillis);
	config.setNumTestsPerEvictionRun(this.numTestsPerEvictionRun);
	config.setTestOnBorrow(this.testOnBorrow);
	config.setTestOnReturn(this.testOnReturn);
	config.setTestWhileIdle(this.testWhileIdle);
	config.setTimeBetweenEvictionRunsMillis(this.timeBetweenEvictionRunsMillis);
	return config;
}
 
Example 17
Source File: RedisConnectionPoolConfig.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public JedisPoolConfig getJedisPoolConfig() {
	JedisPoolConfig config = new JedisPoolConfig();
	config.setMaxTotal(this.maxTotal);
	config.setMaxIdle(this.maxIdle);
	config.setMinIdle(this.minIdle);
	config.setMaxWaitMillis(this.maxWaitMillis);
	config.setNumTestsPerEvictionRun(this.numTestsPerEvictionRun);
	config.setTestOnBorrow(this.testOnBorrow);
	config.setTestOnReturn(this.testOnReturn);
	config.setTestWhileIdle(this.testWhileIdle);
	config.setTimeBetweenEvictionRunsMillis(this.timeBetweenEvictionRunsMillis);
	return config;
}
 
Example 18
Source File: RedisPool.java    From seconds-kill with MIT License 5 votes vote down vote up
private static void initPool() {
    JedisPoolConfig config = new JedisPoolConfig();

    config.setMaxTotal(maxTotal);
    config.setMaxIdle(maxIdle);
    config.setTestOnBorrow(testOnBorrow);
    config.setBlockWhenExhausted(true);
    config.setMaxWaitMillis(maxWait);

    pool = new JedisPool(config, redisIP, redisPort, 1000 * 2);
}
 
Example 19
Source File: RedisConnectionPoolConfig.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public JedisPoolConfig getJedisPoolConfig() {
	JedisPoolConfig config = new JedisPoolConfig();
	config.setMaxTotal(this.maxTotal);
	config.setMaxIdle(this.maxIdle);
	config.setMinIdle(this.minIdle);
	config.setMaxWaitMillis(this.maxWaitMillis);
	config.setNumTestsPerEvictionRun(this.numTestsPerEvictionRun);
	config.setTestOnBorrow(this.testOnBorrow);
	config.setTestOnReturn(this.testOnReturn);
	config.setTestWhileIdle(this.testWhileIdle);
	config.setTimeBetweenEvictionRunsMillis(this.timeBetweenEvictionRunsMillis);
	return config;
}
 
Example 20
Source File: JedisSampleMain.java    From pepper-metrics with Apache License 2.0 5 votes vote down vote up
public static void testJedis() throws InterruptedException {
    log.info("testJedis()");
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(300);
    config.setMaxIdle(10);
    config.setMinIdle(5);
    config.setMaxWaitMillis(6000);
    config.setTestOnBorrow(false);
    config.setTestOnReturn(false);
    config.setTestWhileIdle(true);
    config.setTestOnCreate(false);

    log.info("init JedisPoolConfig: {}", config.toString());
    final String namespace = "myns";
    JedisPropsHolder.NAMESPACE.set(namespace);
    PjedisPool jedisPool = new PjedisPool(config, "192.168.100.221", 6379);
    JedisHealthTracker.addJedisPool(namespace, jedisPool);
    for (int j = 0; j < 100; j++) {
        for (int i = 0; i < 10; i++) {
            try (Jedis jedis = jedisPool.getResource()) {
                jedis.set("hello", "robin");
            }
        }
        log.info(String.format("%s NumActive:%s NumIdle:%s", j, jedisPool.getNumActive(), jedisPool.getNumIdle()));
        TimeUnit.SECONDS.sleep(1);
    }
}