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

The following examples show how to use redis.clients.jedis.JedisPoolConfig#setTestOnReturn() . 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: RedisEventLoopTest.java    From fastjgame with Apache License 2.0 8 votes vote down vote up
private static RedisEventLoop newRedisEventLoop() {
    final JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxIdle(1);
    config.setMaxTotal(5);

    config.setTestOnBorrow(true);
    config.setTestOnReturn(true);

    config.setBlockWhenExhausted(true);
    config.setMaxWaitMillis(10);

    // 测试时不使用哨兵
    final JedisPool jedisPool = new JedisPool(config, "localhost", 6379);
    final RedisEventLoop redisEventLoop = new RedisEventLoop(null, new DefaultThreadFactory("RedisEventLoop"), RejectedExecutionHandlers.abort(), jedisPool);
    redisEventLoop.terminationFuture().addListener(future -> jedisPool.close());
    return redisEventLoop;
}
 
Example 2
Source File: JedisFactory.java    From moon-api-gateway with MIT License 6 votes vote down vote up
@Override
public void afterPropertiesSet() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(jedisConfig.getMaxTotal());
    jedisPoolConfig.setMaxWaitMillis(jedisConfig.getMaxWaitMillis());
    jedisPoolConfig.setMaxIdle(jedisConfig.getMaxIdle());
    jedisPoolConfig.setMinIdle(jedisConfig.getMinIdle());
    jedisPoolConfig.setNumTestsPerEvictionRun(jedisConfig.getNumTestsPerEvictionRun());
    jedisPoolConfig.setTimeBetweenEvictionRunsMillis(jedisConfig.getTimeBetweenEvictionRunsMillis());
    jedisPoolConfig.setBlockWhenExhausted(jedisConfig.isBlockWhenExhausted());
    jedisPoolConfig.setTestOnBorrow(jedisConfig.isTestOnBorrow());
    jedisPoolConfig.setTestOnReturn(jedisConfig.isTestOnReturn());
    jedisPoolConfig.setTestWhileIdle(jedisConfig.isTestWhileIdle());

    String jedisHost = jedisConfig.getHost();
    int jedisPort = jedisConfig.getPort();
    int jedisTimeout = jedisConfig.getTimeout();
    int jedisDatabase = jedisConfig.getDatabase();

    jedisPool = new JedisPool(jedisPoolConfig, jedisHost, jedisPort, jedisTimeout, null, jedisDatabase);
}
 
Example 3
Source File: RedisUtils.java    From J2Cache with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化 Redis 连接池
 * @param props j2cache.properties
 * @param prefix configuration prefix
 * @return redis connection pool configuration object
 */
public final static JedisPoolConfig newPoolConfig(Properties props, String prefix) {
    JedisPoolConfig cfg = new JedisPoolConfig();
    cfg.setMaxTotal(Integer.valueOf(props.getProperty(key(prefix,"maxTotal"), "-1")));
    cfg.setMaxIdle(Integer.valueOf(props.getProperty(key(prefix,"maxIdle"), "100")));
    cfg.setMaxWaitMillis(Integer.valueOf(props.getProperty(key(prefix,"maxWaitMillis"), "100")));
    cfg.setMinEvictableIdleTimeMillis(Integer.valueOf(props.getProperty(key(prefix,"minEvictableIdleTimeMillis"), "864000000")));
    cfg.setMinIdle(Integer.valueOf(props.getProperty(key(prefix,"minIdle"), "10")));
    cfg.setNumTestsPerEvictionRun(Integer.valueOf(props.getProperty(key(prefix,"numTestsPerEvictionRun"), "10")));
    cfg.setLifo(Boolean.valueOf(props.getProperty(key(prefix,"lifo"), "false")));
    cfg.setSoftMinEvictableIdleTimeMillis(Integer.valueOf((String)props.getOrDefault(key(prefix,"softMinEvictableIdleTimeMillis"), "10")));
    cfg.setTestOnBorrow(Boolean.valueOf(props.getProperty(key(prefix,"testOnBorrow"), "true")));
    cfg.setTestOnReturn(Boolean.valueOf(props.getProperty(key(prefix,"testOnReturn"), "false")));
    cfg.setTestWhileIdle(Boolean.valueOf(props.getProperty(key(prefix,"testWhileIdle"), "true")));
    cfg.setTimeBetweenEvictionRunsMillis(Integer.valueOf(props.getProperty(key(prefix,"timeBetweenEvictionRunsMillis"), "300000")));
    cfg.setBlockWhenExhausted(Boolean.valueOf(props.getProperty(key(prefix,"blockWhenExhausted"), "false")));
    return cfg;
}
 
Example 4
Source File: DefaultRedis.java    From craft-atom with MIT License 6 votes vote down vote up
private JedisPoolConfig convert(RedisPoolConfig cfg) {
	JedisPoolConfig jpc = new JedisPoolConfig();
	jpc.setBlockWhenExhausted(cfg.isBlockWhenExhausted());
	jpc.setLifo(cfg.isLifo());
	jpc.setMaxIdle(cfg.getMaxIdle());
	jpc.setMaxTotal(cfg.getMaxTotal());
	jpc.setMaxWaitMillis(cfg.getMaxWaitMillis());
	jpc.setMinEvictableIdleTimeMillis(cfg.getMinEvictableIdleTimeMillis());
	jpc.setMinIdle(cfg.getMinIdle());
	jpc.setNumTestsPerEvictionRun(cfg.getNumTestsPerEvictionRun());
	jpc.setTestOnBorrow(cfg.isTestOnBorrow());
	jpc.setTestOnReturn(cfg.isTestOnReturn());
	jpc.setTestWhileIdle(cfg.isTestWhileIdle());
	jpc.setTimeBetweenEvictionRunsMillis(cfg.getTimeBetweenEvictionRunsMillis());
	return jpc;
}
 
Example 5
Source File: RedisPoolConfiguration.java    From seed with Apache License 2.0 6 votes vote down vote up
@Bean
public JedisPool getPool(){
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(this.maxTotal);
    config.setMaxIdle(this.maxIdle);
    config.setMinIdle(this.minIdle);
    config.setBlockWhenExhausted(true);
    config.setMaxWaitMillis(this.maxWaitMillis);
    config.setTestOnBorrow(false);
    config.setTestOnReturn(false);
    String[] parts = StringUtils.split(this.nodes.get(0), ":");
    JedisPool pool = new JedisPool(config, parts[0], Integer.parseInt(parts[1]),  this.connectionTimeout, this.password);
    for(int i=0; i<this.minIdle; i++){
        Jedis jedis = pool.getResource();
        jedis.ping();
        jedis.close();
    }
    return pool;
}
 
Example 6
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);
    }
}
 
Example 7
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 8
Source File: RedisCacheConfig.java    From jseckill with Apache License 2.0 5 votes vote down vote up
@Bean(name = "poolConfig")
public JedisPoolConfig initJedisPoolConfig() {
    log.info("JedisPoolConfig注入开始:");
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(maxActive);
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMaxWaitMillis(maxWaitMillis);
    poolConfig.setMinIdle(minIdle);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestOnReturn(true);
    poolConfig.setBlockWhenExhausted(true);
    return poolConfig;
}
 
Example 9
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 10
Source File: Client.java    From JRedisBloom with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Create a new client to ReBloom
 * @param host the redis host
 * @param port the redis port
 * @param timeout connection timeout
 * @param poolSize the poolSize of JedisPool
 */
public Client(String host, int port, int timeout, int poolSize) {
  JedisPoolConfig conf = new JedisPoolConfig();
  conf.setMaxTotal(poolSize);
  conf.setTestOnBorrow(false);
  conf.setTestOnReturn(false);
  conf.setTestOnCreate(false);
  conf.setTestWhileIdle(false);
  conf.setMinEvictableIdleTimeMillis(60000);
  conf.setTimeBetweenEvictionRunsMillis(30000);
  conf.setNumTestsPerEvictionRun(-1);
  conf.setFairness(true);

  pool = new JedisPool(conf, host, port, timeout);
}
 
Example 11
Source File: RedisPool.java    From mmall-kay-Java with Apache License 2.0 5 votes vote down vote up
private static void initPool() {
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(maxTotal);
    config.setMaxIdle(maxIdle);
    config.setMinIdle(minIdle);
    config.setBlockWhenExhausted(true); //资源耗尽时是否阻塞
    config.setTestOnBorrow(testOnborrow);
    config.setTestOnReturn(testOnReturn);

    pool = new JedisPool(config, host, port, 1000 * 2);
}
 
Example 12
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 13
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 14
Source File: DefaultRedisConfiguration.java    From spring-redis-plugin with Apache License 2.0 5 votes vote down vote up
@Bean
public JedisPoolConfig defaultJedisPoolConfig() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(env.getProperty("redis.maxTotal", Integer.class, 200));
    jedisPoolConfig.setMaxIdle(env.getProperty("redis.maxIdle", Integer.class, 20));
    jedisPoolConfig.setMinIdle(env.getProperty("redis.minIdle", Integer.class, 0));
    jedisPoolConfig.setMaxWaitMillis(env.getProperty("redis.maxWait", Integer.class, 3000));
    jedisPoolConfig.setMinEvictableIdleTimeMillis(env.getProperty("redis.minEvictableIdleTimeMillis", Long.class, 60000L));
    jedisPoolConfig.setTimeBetweenEvictionRunsMillis(env.getProperty("redis.timeBetweenEvictionRunsMillis", Long.class, 120000L));
    jedisPoolConfig.setNumTestsPerEvictionRun(env.getProperty("redis.numTestsPerEvictionRun", Integer.class, 1));
    jedisPoolConfig.setTestOnBorrow(env.getProperty("redis.testOnBorrow", Boolean.class, false));
    jedisPoolConfig.setTestOnReturn(env.getProperty("redis.testOnReturn", Boolean.class, false));
    jedisPoolConfig.setTestWhileIdle(env.getProperty("redis.testWhileIdle", Boolean.class, true));
    return jedisPoolConfig;
}
 
Example 15
Source File: DefaultShardedRedisConfiguration.java    From spring-redis-plugin with Apache License 2.0 5 votes vote down vote up
@Bean
public JedisPoolConfig defaultJedisPoolConfig() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(env.getProperty("redis.maxTotal", Integer.class, 200));
    jedisPoolConfig.setMaxIdle(env.getProperty("redis.maxIdle", Integer.class, 20));
    jedisPoolConfig.setMinIdle(env.getProperty("redis.minIdle", Integer.class, 0));
    jedisPoolConfig.setMaxWaitMillis(env.getProperty("redis.maxWait", Integer.class, 3000));
    jedisPoolConfig.setMinEvictableIdleTimeMillis(env.getProperty("redis.minEvictableIdleTimeMillis", Long.class, 60000L));
    jedisPoolConfig.setTimeBetweenEvictionRunsMillis(env.getProperty("redis.timeBetweenEvictionRunsMillis", Long.class, 120000L));
    jedisPoolConfig.setNumTestsPerEvictionRun(env.getProperty("redis.numTestsPerEvictionRun", Integer.class, 1));
    jedisPoolConfig.setTestOnBorrow(env.getProperty("redis.testOnBorrow", Boolean.class, false));
    jedisPoolConfig.setTestOnReturn(env.getProperty("redis.testOnReturn", Boolean.class, false));
    jedisPoolConfig.setTestWhileIdle(env.getProperty("redis.testWhileIdle", Boolean.class, true));
    return jedisPoolConfig;
}
 
Example 16
Source File: JedisConfig.java    From springboot-learn with MIT License 5 votes vote down vote up
@Bean(name = "jedis.pool.config")
public JedisPoolConfig jedisPoolConfig(@Value("${spring.redis.jedis.pool.maxTotal}") int maxTotal,
                                       @Value("${spring.redis.jedis.pool.maxIdle}") int maxIdle,
                                       @Value("${spring.redis.jedis.pool.maxWaitMillis}") int maxWaitMillis,
                                       @Value("${spring.redis.jedis.pool.testOnBorrow}") boolean testOnBorrow,
                                       @Value("${spring.redis.jedis.pool.testOnReturn}") boolean testOnReturn) {

    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(maxTotal);
    config.setMaxIdle(maxIdle);
    config.setMaxWaitMillis(maxWaitMillis);
    config.setTestOnBorrow(testOnBorrow);
    config.setTestOnReturn(testOnBorrow);
    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: RedisSource.java    From ingestion with Apache License 2.0 4 votes vote down vote up
private void init() {
    try {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        String prop;

        if((prop = poolProps.get(CONF_TESTONBORROW)) == null){
            poolConfig.setTestOnBorrow(DEFAULT_TESTONBORROW);
        } else {
            log.info("Setting testOnBorrow property to " + prop);
            poolConfig.setTestOnBorrow(Boolean.valueOf(prop));
        }
        if((prop = poolProps.get(CONF_MAXTOTAL)) == null){
            poolConfig.setMaxTotal(DEFAULT_MAXTOTAL);
        } else {
            log.info("Setting maxTotal property to " + prop);
            poolConfig.setMaxTotal(Integer.valueOf(prop));
        }
        if((prop = poolProps.get(CONF_MAXIDLE)) == null){
            poolConfig.setMaxTotal(DEFAULT_MAXIDLE);
        } else {
            log.info("Setting maxIdle property to " + prop);
            poolConfig.setMaxIdle(Integer.valueOf(prop));
        }
        if((prop = poolProps.get(CONF_MINIDLE)) == null){
            poolConfig.setMinIdle(DEFAULT_MINIDLE);
        } else {
            log.info("Setting minIdle property to " + prop);
            poolConfig.setMinIdle(Integer.valueOf(prop));
        }
        if((prop = poolProps.get(CONF_MAXWAITINMILLIS)) == null){
            poolConfig.setMaxWaitMillis(DEFAULT_MAXWAITINMILLIS);
        } else {
            log.info("Setting maxWaitInMillis property to " + prop);
            poolConfig.setMaxWaitMillis(Integer.valueOf(prop));
        }
        if((prop = poolProps.get(CONF_TESTWHILEIDLE)) == null){
            poolConfig.setTestWhileIdle(DEFAULT_TESTWHILEIDLE);
        } else {
            log.info("Setting testWhileIdle property to " + prop);
            poolConfig.setTestWhileIdle(Boolean.valueOf(prop));
        }
        if((prop = poolProps.get(CONF_TESTONRETURN)) == null){
            poolConfig.setTestOnReturn(DEFAULT_TESTONRETURN);
        } else {
            log.info("Setting testOnReturn property to " + prop);
            poolConfig.setTestOnReturn(Boolean.valueOf(prop));
        }
        if((prop = poolProps.get(CONF_MINEVICTABLEIDLETIMEINMILLIS)) == null){
            poolConfig.setMinEvictableIdleTimeMillis(DEFAULT_MINEVICTABLEIDLETIMEINMILLIS);
        } else {
            log.info("Setting minEvictableIdleTimeInMillis property to " + prop);
            poolConfig.setMinEvictableIdleTimeMillis(Integer.valueOf(prop));
        }
        if((prop = poolProps.get(CONF_TIMEBETWEETNEVICTIONRUNSMILLIS)) == null){
            poolConfig.setTimeBetweenEvictionRunsMillis(DEFAULT_TIMEBETWEETNEVICTIONRUNSMILLIS);
        } else {
            log.info("Setting timeBetweenEvictionRunMillis property to " + prop);
            poolConfig.setTimeBetweenEvictionRunsMillis(Integer.valueOf(prop));
        }
        if((prop = poolProps.get(CONF_NUMTESTSPEREVICTIONRUN)) == null){
            poolConfig.setNumTestsPerEvictionRun(DEFAULT_NUMTESTSPEREVICTIONRUN);
        } else {
            log.info("Setting numTestsPerEvictionRun property to " + prop);
            poolConfig.setNumTestsPerEvictionRun(Integer.valueOf(prop));
        }

        // create JEDIS pool
        this.jedisPool = new JedisPool(poolConfig, host, port);

    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
}
 
Example 19
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public RedisRegistry(URL url) {
    super(url);
    if (url.isAnyHost()) {
        throw new IllegalStateException("registry address == null");
    }
  //GenericObjectPool.Config config = new GenericObjectPool.Config();
    //config.testOnBorrow = url.getParameter("test.on.borrow", true);
    //config.testOnReturn = url.getParameter("test.on.return", false);
    //config.testWhileIdle = url.getParameter("test.while.idle", false);
	JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
	jedisPoolConfig.setTestOnBorrow(url.getParameter("test.on.borrow", true));
	jedisPoolConfig.setTestOnReturn(url.getParameter("test.on.return", false));
	jedisPoolConfig.setTestWhileIdle(url.getParameter("test.while.idle", false));
    if (url.getParameter("max.idle", 0) > 0)
        //config.maxIdle = url.getParameter("max.idle", 0);
    	jedisPoolConfig.setMaxIdle(url.getParameter("max.idle", 0));
    if (url.getParameter("min.idle", 0) > 0)
        //config.minIdle = url.getParameter("min.idle", 0);
		jedisPoolConfig.setMinIdle(url.getParameter("min.idle", 0));
    if (url.getParameter("max.active", 0) > 0)
        //config.maxActive = url.getParameter("max.active", 0);
    	jedisPoolConfig.setMaxTotal(url.getParameter("max.active", 0));
    if (url.getParameter("max.wait", 0) > 0)
        //config.maxWait = url.getParameter("max.wait", 0);
    	jedisPoolConfig.setMaxWaitMillis(url.getParameter("max.wait", 0));
    if (url.getParameter("num.tests.per.eviction.run", 0) > 0)
        //config.numTestsPerEvictionRun = url.getParameter("num.tests.per.eviction.run", 0);
    	jedisPoolConfig.setNumTestsPerEvictionRun(url.getParameter("num.tests.per.eviction.run", 0));
    if (url.getParameter("time.between.eviction.runs.millis", 0) > 0)
        //config.timeBetweenEvictionRunsMillis = url.getParameter("time.between.eviction.runs.millis", 0);
    	jedisPoolConfig.setTimeBetweenEvictionRunsMillis(url.getParameter("time.between.eviction.runs.millis", 0));
    if (url.getParameter("min.evictable.idle.time.millis", 0) > 0)
        //config.minEvictableIdleTimeMillis = url.getParameter("min.evictable.idle.time.millis", 0);
    	jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(url.getParameter("min.evictable.idle.time.millis", 0));

    String cluster = url.getParameter("cluster", "failover");
    if (! "failover".equals(cluster) && ! "replicate".equals(cluster)) {
        throw new IllegalArgumentException("Unsupported redis cluster: " + cluster + ". The redis cluster only supported failover or replicate.");
    }
    replicate = "replicate".equals(cluster);

    List<String> addresses = new ArrayList<String>();
    addresses.add(url.getAddress());
    String[] backups = url.getParameter(Constants.BACKUP_KEY, new String[0]);
    if (backups != null && backups.length > 0) {
        addresses.addAll(Arrays.asList(backups));
    }

    // 增加Redis密码支持
    String password = url.getPassword();
    for (String address : addresses) {
        int i = address.indexOf(':');
        String host;
        int port;
        if (i > 0) {
            host = address.substring(0, i);
            port = Integer.parseInt(address.substring(i + 1));
        } else {
            host = address;
            port = DEFAULT_REDIS_PORT;
        }
        if (StringUtils.isEmpty(password)) {
            this.jedisPools.put(address, new JedisPool(jedisPoolConfig, host, port,
                    url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)));
        } else {
            // 使用密码连接。  此处要求备用redis与主要redis使用相同的密码
            this.jedisPools.put(address, new JedisPool(jedisPoolConfig, host, port,
                    url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT), password));
        }
    }

    this.reconnectPeriod = url.getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD);
    String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
    if (! group.startsWith(Constants.PATH_SEPARATOR)) {
        group = Constants.PATH_SEPARATOR + group;
    }
    if (! group.endsWith(Constants.PATH_SEPARATOR)) {
        group = group + Constants.PATH_SEPARATOR;
    }
    this.root = group;

    this.expirePeriod = url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT);
    this.expireFuture = expireExecutor.scheduleWithFixedDelay(new Runnable() {
        public void run() {
            try {
                deferExpired(); // 延长过期时间
            } catch (Throwable t) { // 防御性容错
                logger.error("Unexpected exception occur at defer expire time, cause: " + t.getMessage(), t);
            }
        }
    }, expirePeriod / 2, expirePeriod / 2, TimeUnit.MILLISECONDS);
}
 
Example 20
Source File: RedisSentinelJobStoreTest.java    From quartz-redis-jobstore with Apache License 2.0 3 votes vote down vote up
@Before
public void setUpRedis() throws IOException, SchedulerConfigException {
    final List<Integer> sentinels = Arrays.asList(getPort(), getPort());
    final List<Integer> group1 = Arrays.asList(getPort(), getPort());
    final List<Integer> group2 = Arrays.asList(getPort(), getPort());
    //creates a cluster with 3 sentinels, quorum size of 2 and 3 replication groups, each with one master and one slave
    redisCluster = RedisCluster.builder().sentinelPorts(sentinels).quorumSize(2)
        .serverPorts(group1).replicationGroup("master1", 1)
        .serverPorts(group2).replicationGroup("master2", 1)
        .ephemeralServers().replicationGroup("master3", 1)
        .build();
    redisCluster.start();


    Set<String> jedisSentinelHosts = JedisUtil.sentinelHosts(redisCluster);

    joinedHosts = Joiner.on(",").join(jedisSentinelHosts);

    final short database = 1;
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setTestOnBorrow(true);
    jedisPoolConfig.setTestOnCreate(true);
    jedisPoolConfig.setTestOnReturn(true);
    jedisPoolConfig.setMaxWaitMillis(2000);
    jedisPoolConfig.setMaxTotal(20);
    jedisPool = new JedisSentinelPool("master1", jedisSentinelHosts, jedisPoolConfig);
    jobStore = new RedisJobStore();
    jobStore.setHost(joinedHosts);
    jobStore.setJedisPool(jedisSentinelPool);
    jobStore.setLockTimeout(2000);
    jobStore.setMasterGroupName("master1");
    jobStore.setRedisSentinel(true);
    jobStore.setInstanceId("testJobStore1");
    jobStore.setDatabase(database);
    mockScheduleSignaler = mock(SchedulerSignaler.class);
    jobStore.initialize(null, mockScheduleSignaler);
    schema = new RedisJobStoreSchema();

    jedis = jedisPool.getResource();
    jedis.flushDB();

}