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

The following examples show how to use redis.clients.jedis.JedisPoolConfig#setSoftMinEvictableIdleTimeMillis() . 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: JedisConfig.java    From Mars-Java with MIT License 7 votes vote down vote up
public JedisPoolConfig getJedisPoolConfig() {
    jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(2048);
    jedisPoolConfig.setMaxIdle(200);
    jedisPoolConfig.setMinIdle(2);
    jedisPoolConfig.setNumTestsPerEvictionRun(2048);
    jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
    jedisPoolConfig.setMinEvictableIdleTimeMillis(-1);
    jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(10000);
    jedisPoolConfig.setMaxWaitMillis(10000);
    jedisPoolConfig.setTestOnBorrow(true);
    jedisPoolConfig.setTestWhileIdle(true);
    jedisPoolConfig.setTestOnReturn(true);
    jedisPoolConfig.setJmxEnabled(true);
    jedisPoolConfig.setBlockWhenExhausted(true);
    return jedisPoolConfig;
}
 
Example 2
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 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: 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 5
Source File: RedisDataSet.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public void testStarted(String distributedHost) {
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxActive(getMaxActive());
    config.setMaxIdle(getMaxIdle());
    config.setMinIdle(getMinIdle());
    config.setMaxWait(getMaxWait());
    config.setWhenExhaustedAction((byte)getWhenExhaustedAction());
    config.setTestOnBorrow(getTestOnBorrow());
    config.setTestOnReturn(getTestOnReturn());
    config.setTestWhileIdle(getTestWhileIdle());
    config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
    config.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
    config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
    config.setSoftMinEvictableIdleTimeMillis(getSoftMinEvictableIdleTimeMillis());

    int port = Protocol.DEFAULT_PORT;
    if(!JOrphanUtils.isBlank(this.port)) {
        port = Integer.parseInt(this.port);
    }
    int timeout = Protocol.DEFAULT_TIMEOUT;
    if(!JOrphanUtils.isBlank(this.timeout)) {
        timeout = Integer.parseInt(this.timeout);
    }
    int database = Protocol.DEFAULT_DATABASE;
    if(!JOrphanUtils.isBlank(this.database)) {
        database = Integer.parseInt(this.database);
    }
    String password = null;
    if(!JOrphanUtils.isBlank(this.password)) {
        password = this.password;
    }
    this.pool = new JedisPool(config, this.host, port, timeout, password, database);
}
 
Example 6
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);
}