Java Code Examples for org.apache.commons.pool2.impl.GenericObjectPoolConfig#setMaxIdle()

The following examples show how to use org.apache.commons.pool2.impl.GenericObjectPoolConfig#setMaxIdle() . 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: RedisCacheTest.java    From jetcache with Apache License 2.0 6 votes vote down vote up
@Test
public void readFromSlaveTest() throws Exception {
    GenericObjectPoolConfig pc = new GenericObjectPoolConfig();
    pc.setMinIdle(2);
    pc.setMaxIdle(10);
    pc.setMaxTotal(10);
    JedisPool pool1 = new JedisPool(pc, "localhost", 6379);
    JedisPool pool2 = new JedisPool(pc, "localhost", 6380);
    JedisPool pool3 = new JedisPool(pc, "localhost", 6381);

    RedisCacheBuilder builder = RedisCacheBuilder.createRedisCacheBuilder();
    builder.setJedisPool(pool1);
    builder.setReadFromSlave(true);
    builder.setJedisSlavePools(pool2, pool3);
    builder.setSlaveReadWeights(1, 1);
    builder.setKeyConvertor(FastjsonKeyConvertor.INSTANCE);
    builder.setValueEncoder(JavaValueEncoder.INSTANCE);
    builder.setValueDecoder(JavaValueDecoder.INSTANCE);
    builder.setKeyPrefix(new Random().nextInt() + "");
    builder.setExpireAfterWriteInMillis(500);

    readFromSlaveTestAsserts(pool1, builder);

    builder.setSlaveReadWeights(null);
    readFromSlaveTestAsserts(pool1, builder);
}
 
Example 2
Source File: RedisCommandsContainerBuilder.java    From bahir-flink with Apache License 2.0 6 votes vote down vote up
/**
 * Builds container for Redis Cluster environment.
 *
 * @param jedisClusterConfig configuration for JedisCluster
 * @return container for Redis Cluster environment
 * @throws NullPointerException if jedisClusterConfig is null
 */
public static RedisCommandsContainer build(FlinkJedisClusterConfig jedisClusterConfig) {
    Objects.requireNonNull(jedisClusterConfig, "Redis cluster config should not be Null");

    GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    genericObjectPoolConfig.setMaxIdle(jedisClusterConfig.getMaxIdle());
    genericObjectPoolConfig.setMaxTotal(jedisClusterConfig.getMaxTotal());
    genericObjectPoolConfig.setMinIdle(jedisClusterConfig.getMinIdle());

    JedisCluster jedisCluster = new JedisCluster(jedisClusterConfig.getNodes(),
            jedisClusterConfig.getConnectionTimeout(),
            jedisClusterConfig.getConnectionTimeout(),
            jedisClusterConfig.getMaxRedirections(),
            jedisClusterConfig.getPassword(),
            genericObjectPoolConfig);
    return new RedisClusterContainer(jedisCluster);
}
 
Example 3
Source File: ObjectPoolFactory.java    From Thunder with Apache License 2.0 6 votes vote down vote up
public static GenericObjectPoolConfig createFSTObjectPoolConfig() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    try {
        config.setMaxTotal(ThunderConstant.CPUS * properties.getInteger(ThunderConstant.FST_OBJECT_POOL_MAX_TOTAL_ATTRIBUTE_NAME));
        config.setMaxIdle(ThunderConstant.CPUS * properties.getInteger(ThunderConstant.FST_OBJECT_POOL_MAX_IDLE_ATTRIBUTE_NAME));
        config.setMinIdle(ThunderConstant.CPUS * properties.getInteger(ThunderConstant.FST_OBJECT_POOL_MIN_IDLE_ATTRIBUTE_NAME));
        config.setMaxWaitMillis(properties.getLong(ThunderConstant.FST_OBJECT_POOL_MAX_WAIT_MILLIS_ATTRIBUTE_NAME));
        config.setTimeBetweenEvictionRunsMillis(properties.getLong(ThunderConstant.FST_OBJECT_POOL_TIME_BETWEEN_EVICTION_RUN_MILLIS_ATTRIBUTE_NAME));
        config.setMinEvictableIdleTimeMillis(properties.getLong(ThunderConstant.FST_OBJECT_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS_ATTRIBUTE_NAME));
        config.setSoftMinEvictableIdleTimeMillis(properties.getLong(ThunderConstant.FST_OBJECT_POOL_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS_ATTRIBUTE_NAME));
        config.setBlockWhenExhausted(properties.getBoolean(ThunderConstant.FST_OBJECT_POOL_BLOCK_WHEN_EXHAUSTED_ATTRIBUTE_NAME));
        config.setLifo(properties.getBoolean(ThunderConstant.FST_OBJECT_POOL_LIFO_ATTRIBUTE_NAME));
        config.setFairness(properties.getBoolean(ThunderConstant.FST_OBJECT_POOL_FAIRNESS_ATTRIBUTE_NAME));
        config.setTestOnBorrow(false);
        config.setTestOnReturn(false);
        config.setTestOnCreate(false);
        config.setTestWhileIdle(false);
        config.setNumTestsPerEvictionRun(-1);
    } catch (Exception e) {
        throw new IllegalArgumentException("Properties maybe isn't initialized");
    }

    return config;
}
 
Example 4
Source File: JedisHolder.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private JedisPool initialize(String host, int port) {

		GenericObjectPoolConfig jedisPoolConfig = new GenericObjectPoolConfig();
		jedisPoolConfig.setMaxIdle(maxIdle);
		jedisPoolConfig.setMinIdle(minIdle);
		jedisPoolConfig.setTestOnBorrow(testOnBorrow);
		jedisPoolConfig.setTestOnReturn(testOnReturn);
		jedisPoolConfig.setTestWhileIdle(testWhileIdle);

		jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
		jedisPoolConfig.setMinEvictableIdleTimeMillis(-1);
		jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
		jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);

		return new JedisPool(jedisPoolConfig, host, port, timeBetweenEvictionRunsMillis, null);
		
	}
 
Example 5
Source File: UpdateEventCacheService.java    From game-executor with Apache License 2.0 5 votes vote down vote up
public static void start(){
//        int size = 1024;
        int size = 1024;
        int max = 32;
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxTotal(size * max);
        genericObjectPoolConfig.setMaxIdle(size * max);
        genericObjectPoolConfig.setMinIdle(size);
        long time = 1000 * 30;
        genericObjectPoolConfig.setMaxWaitMillis(time);
        genericObjectPoolConfig.setSoftMinEvictableIdleTimeMillis(time);

        updateEventCacheFactory = new UpdateEventCacheFactory(new UpdateEventPoolFactory(), genericObjectPoolConfig);
    }
 
Example 6
Source File: TestLEEFParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private GenericObjectPool<StringBuilder> getStringBuilderPool() {
  GenericObjectPoolConfig stringBuilderPoolConfig = new GenericObjectPoolConfig();
  stringBuilderPoolConfig.setMaxTotal(1);
  stringBuilderPoolConfig.setMinIdle(1);
  stringBuilderPoolConfig.setMaxIdle(1);
  stringBuilderPoolConfig.setBlockWhenExhausted(false);
  return new GenericObjectPool<>(new StringBuilderPoolFactory(1024), stringBuilderPoolConfig);
}
 
Example 7
Source File: RedisCommandsContainerBuilder.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
/**
 * Builds container for Redis Sentinel environment.
 *
 * @param jedisSentinelConfig configuration for JedisSentinel
 * @return container for Redis sentinel environment
 * @throws NullPointerException if jedisSentinelConfig is null
 */
public static RedisCommandsContainer build(FlinkJedisSentinelConfig jedisSentinelConfig) {
    Objects.requireNonNull(jedisSentinelConfig, "Redis sentinel config should not be Null");

    GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    genericObjectPoolConfig.setMaxIdle(jedisSentinelConfig.getMaxIdle());
    genericObjectPoolConfig.setMaxTotal(jedisSentinelConfig.getMaxTotal());
    genericObjectPoolConfig.setMinIdle(jedisSentinelConfig.getMinIdle());

    JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(jedisSentinelConfig.getMasterName(),
        jedisSentinelConfig.getSentinels(), genericObjectPoolConfig,
        jedisSentinelConfig.getConnectionTimeout(), jedisSentinelConfig.getSoTimeout(),
        jedisSentinelConfig.getPassword(), jedisSentinelConfig.getDatabase());
    return new RedisContainer(jedisSentinelPool);
}
 
Example 8
Source File: BaseTest.java    From lite-pool with Apache License 2.0 5 votes vote down vote up
public GenericObjectPool<TestObject> createCommonsPool2(int minimum, int maximum, long timeout) {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(maximum);
    config.setMinIdle(minimum);
    config.setMaxIdle(minimum);
    config.setFairness(false);
    config.setJmxEnabled(false);
    config.setBlockWhenExhausted(true);
    config.setTestOnBorrow(false);
    config.setMaxWaitMillis(timeout);
    config.setTestOnCreate(false);
    config.setTestOnReturn(false);
    config.setTestWhileIdle(false);
    return new GenericObjectPool<>( new CommonsPool2Factory(), config);
}
 
Example 9
Source File: RedisStorageService.java    From awesome-delay-queue with MIT License 5 votes vote down vote up
public RedisStorageService(AwesomeURL url){
    super(url);
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setTestOnBorrow(url.getParameter("test.on.borrow", true));
    config.setTestOnReturn(url.getParameter("test.on.return", false));
    config.setTestWhileIdle(url.getParameter("test.while.idle", false));
    if (url.getParameter("max.idle", 0) > 0) {
        config.setMaxIdle(url.getParameter("max.idle", 0));
    }
    if (url.getParameter("min.idle", 0) > 0) {
        config.setMinIdle(url.getParameter("min.idle", 0));
    }
    if (url.getParameter("max.active", 0) > 0) {
        config.setMaxTotal(url.getParameter("max.active", 0));
    }
    if (url.getParameter("max.total", 0) > 0) {
        config.setMaxTotal(url.getParameter("max.total", 0));
    }
    if (url.getParameter("max.wait", url.getParameter("timeout", 0)) > 0) {
        config.setMaxWaitMillis(url.getParameter("max.wait", url.getParameter("timeout", 0)));
    }
    if (url.getParameter("num.tests.per.eviction.run", 0) > 0) {
        config.setNumTestsPerEvictionRun(url.getParameter("num.tests.per.eviction.run", 0));
    }
    if (url.getParameter("time.between.eviction.runs.millis", 0) > 0) {
        config.setTimeBetweenEvictionRunsMillis(url.getParameter("time.between.eviction.runs.millis", 0));
    }
    if (url.getParameter("min.evictable.idle.time.millis", 0) > 0) {
        config.setMinEvictableIdleTimeMillis(url.getParameter("min.evictable.idle.time.millis", 0));
    }
    this.jedisPool = new JedisPool(config, url.getHost(), url.getPort(),
            url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT), StringUtils.isEmpty(url.getPassword()) ? null : url.getPassword(),
            url.getParameter("db.index", 0));
}
 
Example 10
Source File: RedisClusterTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
private GenericObjectPoolConfig getPoolConfig() {
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxTotal(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL * 20);
    poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 20);
    poolConfig.setMinIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 10);
    // JedisPool.borrowObject最大等待时间
    poolConfig.setMaxWaitMillis(1000L);
    poolConfig.setJmxNamePrefix("jedis-pool");
    poolConfig.setJmxEnabled(true);
    return poolConfig;
}
 
Example 11
Source File: RedisSetData.java    From shark with Apache License 2.0 5 votes vote down vote up
public @BeforeClass static void init() {
	GenericObjectPoolConfig cfg = new GenericObjectPoolConfig();
	cfg.setMaxIdle(10);
	cfg.setMinIdle(1);
	cfg.setMaxIdle(5);
	cfg.setMaxWaitMillis(5000);
	cfg.setTestOnBorrow(true);
	cfg.setTestOnReturn(true);
	Set<HostAndPort> hostAndPorts = new HashSet<HostAndPort>();
	HostAndPort hostAndPort = new HostAndPort("ip", 7000);
	hostAndPorts.add(hostAndPort);
	jedis = new JedisCluster(hostAndPorts, cfg);
}
 
Example 12
Source File: TestRegexParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private GenericObjectPool<StringBuilder> getStringBuilderPool() {
  GenericObjectPoolConfig stringBuilderPoolConfig = new GenericObjectPoolConfig();
  stringBuilderPoolConfig.setMaxTotal(1);
  stringBuilderPoolConfig.setMinIdle(1);
  stringBuilderPoolConfig.setMaxIdle(1);
  stringBuilderPoolConfig.setBlockWhenExhausted(false);
  return new GenericObjectPool<>(new StringBuilderPoolFactory(1024), stringBuilderPoolConfig);
}
 
Example 13
Source File: CommonsPool2TargetSource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Subclasses can override this if they want to return a specific Commons pool.
 * They should apply any configuration properties to the pool here.
 * <p>Default is a GenericObjectPool instance with the given pool size.
 * @return an empty Commons {@code ObjectPool}.
 * @see GenericObjectPool
 * @see #setMaxSize
 */
protected ObjectPool createObjectPool() {
	GenericObjectPoolConfig config = new GenericObjectPoolConfig();
	config.setMaxTotal(getMaxSize());
	config.setMaxIdle(getMaxIdle());
	config.setMinIdle(getMinIdle());
	config.setMaxWaitMillis(getMaxWait());
	config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
	config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
	config.setBlockWhenExhausted(isBlockWhenExhausted());
	return new GenericObjectPool(this, config);
}
 
Example 14
Source File: Dom4jTools.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected void initPool() {
    int poolSize = config.getMaxPoolSize();
    GenericObjectPoolConfig<SAXParser> poolConfig = new GenericObjectPoolConfig<>();
    poolConfig.setMaxIdle(poolSize);
    poolConfig.setMaxTotal(poolSize);
    poolConfig.setMaxWaitMillis(config.getMaxBorrowWaitMillis());

    String jmxName = "dom4JTools-" + globalConfig.getWebContextName();
    poolConfig.setJmxNamePrefix(jmxName);

    PooledObjectFactory<SAXParser> factory = new SAXParserObjectFactory();
    pool = new GenericObjectPool<>(factory, poolConfig);
}
 
Example 15
Source File: RedisConfiguration.java    From ad with Apache License 2.0 5 votes vote down vote up
@Bean
public GenericObjectPoolConfig genericObjectPoolConfig() {
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMaxTotal(maxActive);
    poolConfig.setMinIdle(minIdle);
    poolConfig.setMaxWaitMillis(maxWait);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestOnCreate(true);
    poolConfig.setTestWhileIdle(true);
    return poolConfig;
}
 
Example 16
Source File: ClientKafkaMonitor.java    From Kafdrop with Apache License 2.0 5 votes vote down vote up
public void applyPoolProperties(GenericObjectPoolConfig<?> poolConfig,
                                KafkaConfiguration.PoolProperties poolProperties)
{
   poolConfig.setMinIdle(poolProperties.getMinIdle());
   poolConfig.setMaxIdle(poolProperties.getMaxIdle());
   poolConfig.setMaxTotal(poolProperties.getMaxTotal());
   poolConfig.setMaxWaitMillis(poolProperties.getMaxWaitMillis());
}
 
Example 17
Source File: RedisStandaloneBuilder.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
/**
 * 构造函数package访问域,package外直接构造实例;
 *
 * @param appId
 */
RedisStandaloneBuilder(final long appId) {
    this.appId = appId;
    poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxTotal(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL * 3);
    poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 2);
    poolConfig.setMinIdle(GenericObjectPoolConfig.DEFAULT_MIN_IDLE);
    poolConfig.setJmxEnabled(true);
    poolConfig.setJmxNamePrefix("jedis-pool");
}
 
Example 18
Source File: DataSourceFactory.java    From athenz with Apache License 2.0 4 votes vote down vote up
public static GenericObjectPoolConfig setupPoolConfig() {
    
    // setup config vars for the object pool
    // ie. min and max idle instances, and max total instances of arbitrary objects
    
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();

    // The maximum number of active connections that can be allocated from
    // this pool at the same time, or negative for no limit. Default: 8
    config.setMaxTotal(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_TOTAL,
            GenericObjectPoolConfig.DEFAULT_MAX_TOTAL));
    if (config.getMaxTotal() == 0) {
        config.setMaxTotal(-1); // -1 means no limit
    }
    
    //  The maximum number of connections that can remain idle in the pool,
    // without extra ones being released, or negative for no limit. Default 8
    config.setMaxIdle(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_IDLE,
            GenericObjectPoolConfig.DEFAULT_MAX_IDLE));
    if (config.getMaxIdle() == 0) {
        config.setMaxIdle(-1); // -1 means no limit
    }
    
    // The minimum number of connections that can remain idle in the pool,
    // without extra ones being created, or zero to create none. Default 0
    config.setMinIdle(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MIN_IDLE,
            GenericObjectPoolConfig.DEFAULT_MIN_IDLE));
    
    // The maximum number of milliseconds that the pool will wait (when
    // there are no available connections) for a connection to be returned
    // before throwing an exception, or -1 to wait indefinitely. Default -1
    config.setMaxWaitMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_WAIT,
            GenericObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS));
    
    // setup the configuration to cleanup idle connections
    //
    // Minimum time an object can be idle in the pool before being eligible
    // for eviction by the idle object evictor.
    // The default value is 30 minutes (1000 * 60 * 30).
    config.setMinEvictableIdleTimeMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_EVICT_IDLE_TIMEOUT,
            BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS));
    
    // Number of milliseconds to sleep between runs of idle object evictor thread.
    // Not using DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS since it is -1
    // meaning it will not run the evictor thread and instead we're using
    // the default min value for evictable idle connections (Default 30 minutes)
    config.setTimeBetweenEvictionRunsMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_EVICT_IDLE_INTERVAL,
            BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS));
    
    if (LOG.isDebugEnabled()) {
        LOG.debug("Config settings for idle object eviction: " +
                "time interval between eviction thread runs (" +
                config.getTimeBetweenEvictionRunsMillis() +
                " millis): minimum timeout for idle objects (" +
                config.getMinEvictableIdleTimeMillis() + " millis)");
    }
    
    // Validate objects by the idle object evictor. If invalid, gets dropped
    // from the pool.
    config.setTestWhileIdle(true);
    
    // Validate object before borrowing from pool and returning to the pool.
    // If invalid, gets dropped from the pool and an attempt to borrow
    // another one will occur.
    config.setTestOnBorrow(true);
    config.setTestOnReturn(true);
    return config;
}
 
Example 19
Source File: ClusterRedisBuilder.java    From ns4_frame with Apache License 2.0 4 votes vote down vote up
public RedisMQTemplate build() {
       if (this.maxTotal == -1) {
           this.maxTotal = MQConfig.getConfig.getRedisClusterMaxTotal();
           if (this.maxTotal == -1) {
               throw new IllegalArgumentException("ClusterRedisBuilder was not set maxTotal value");
           }
       }
       if (this.miniIdle == -1) {
           this.miniIdle = MQConfig.getConfig.getRedisClusterMiniIdle();
       }
       if (this.maxIdle == -1) {
           this.maxIdle = MQConfig.getConfig.getRedisClusterMaxIdle();
       }
       if (this.connectionTimeout == -1) {
           this.connectionTimeout = MQConfig.getConfig.getRedisClusterConnectionTimeout();
       }
       if (this.maxRedirections == -1) {
           this.maxRedirections = MQConfig.getConfig.getRedisClusterMaxRedirections();
       }

       if (this.hostAndPorts == null) {
           this.hostAndPorts = MQConfig.getConfig.getRedisClusterHost();
           if (this.hostAndPorts == null) {
               throw new IllegalArgumentException("ClusterRedisBuilder was not set hostAndPorts value");
           }
       }

	if (this.password == null) {
		this.password = MQConfig.getConfig.getRedisClusterPassword();
		if ("".equals(this.password)) {
			password = null;
		}
	}

	GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
	poolConfig.setMaxIdle(maxTotal);
	poolConfig.setMinIdle(miniIdle);
	poolConfig.setMinIdle(maxIdle);
	String[] hostArray = hostAndPorts.split(";");

       Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
       for (String hostItem : hostArray) {
           String[] hostPort = hostItem.split(":");
           if (hostPort.length == 2) {
               jedisClusterNodes.add(new HostAndPort(hostPort[0], Integer.parseInt(hostPort[1])));
           }
       }

	JedisClusterExtend jc = new JedisClusterExtend(jedisClusterNodes, connectionTimeout, maxRedirections,
			poolConfig, password);
	ClusterRedis clusterRedis = new ClusterRedis(jc);
	RedisMQTemplate client = new RedisMQTemplate();
	client.setRedis(clusterRedis);
	try {
		boolean result = client.ping();
		if (!result) {
			throw new MQInitError("redis服务不可用 hostAndPorts:" + hostAndPorts);
		}
	} catch (MQException e) {
		e.printStackTrace();
		throw new MQInitError("redis服务不可用 hostAndPorts:" + hostAndPorts);
	}
	return client;
}
 
Example 20
Source File: JedisPoolTest.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
	// 连接池中最大空闲的连接数
	int maxIdle = 100;
	int minIdle = 20;

	// 当调用borrow Object方法时,是否进行有效性检查
	boolean testOnBorrow = false;

	// 当调用return Object方法时,是否进行有效性检查
	boolean testOnReturn = false;

	// 如果为true,表示有一个idle object evitor线程对idle
	// object进行扫描,如果validate失败,此object会被从pool中drop掉
	// TODO: 这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
	boolean testWhileIdle = true;

	// 对于“空闲链接”检测线程而言,每次检测的链接资源的个数.(jedis 默认设置成-1)
	int numTestsPerEvictionRun = -1;

	// 连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除
	int minEvictableIdleTimeMillis = 60 * 1000;

	// “空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1
	int timeBetweenEvictionRunsMillis = 30 * 1000;

	GenericObjectPoolConfig jedisPoolConfig = new GenericObjectPoolConfig();
	jedisPoolConfig.setMaxIdle(maxIdle);
	jedisPoolConfig.setMinIdle(minIdle);
	jedisPoolConfig.setTestOnBorrow(testOnBorrow);
	jedisPoolConfig.setTestOnReturn(testOnReturn);
	jedisPoolConfig.setTestWhileIdle(testWhileIdle);

	jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
	jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
	jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);

	JedisPool jedisPool = new JedisPool(jedisPoolConfig, "127.0.0.1", 8066, 30000, null);
	while (true) {
		JedisConnection jc = null;
		try {
			jc = jedisPool.getResource();
			jc.sendCommand(RedisCommand.AUTH, "pwd01");
			System.out.println(jc.getStatusCodeReply());
			jc.sendCommand(RedisCommand.GET, "tt");
			System.out.println(jc.getStatusCodeReply());
		} catch (Exception e) {
			
		} finally {
			if (jc != null)
				jc.close();
		}
		Thread.sleep(5000);
	}
	
}