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

The following examples show how to use redis.clients.jedis.JedisPoolConfig#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: 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: 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 3
Source File: JedisCglibProxy.java    From limiter with MIT License 6 votes vote down vote up
private void init() {
    if (isLoad) {
        return;
    }
    synchronized (JedisCglibProxy.class) {
        if (!isLoad) {
            JedisConfig jedisConfig = load(redisConfigPath);

            JedisPoolConfig jdeJedisPoolConfig = new JedisPoolConfig();
            jdeJedisPoolConfig.setMaxIdle(jedisConfig.getMaxIdle());
            jdeJedisPoolConfig.setMaxTotal(jedisConfig.getMaxTotal());
            jedisPool = new JedisPool(jdeJedisPoolConfig, jedisConfig.getHost(), jedisConfig.getPort(),
                    jedisConfig.getTimeout(), jedisConfig.getPassword());
            isLoad = true;
        }
    }
}
 
Example 4
Source File: RedisConfig.java    From springboot-learn with MIT License 6 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,
                                       @Value("${spring.redis.jedis.pool.blockWhenExhausted}") boolean blockWhenExhausted,
                                       @Value("${spring.redis.jedis.pool.testWhileIdle}") boolean testWhileIdle,
                                       @Value("${spring.redis.jedis.pool.timeBetweenEvictionRunsMillis}") long timeBetweenEvictionRunsMillis,
                                       @Value("${spring.redis.jedis.pool.numTestsPerEvictionRun}") int numTestsPerEvictionRun,
                                       @Value("${spring.redis.jedis.pool.minEvictableIdleTimeMillis}") long minEvictableIdleTimeMillis) {
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(maxTotal);
    config.setMaxIdle(maxIdle);
    config.setMaxWaitMillis(maxWaitMillis);
    config.setTestOnBorrow(testOnBorrow);
    config.setTestOnReturn(testOnReturn);
    config.setBlockWhenExhausted(blockWhenExhausted);
    config.setTestWhileIdle(testWhileIdle);
    config.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    config.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

    return config;
}
 
Example 5
Source File: JedisManagerFactory.java    From es-service-parent with Apache License 2.0 6 votes vote down vote up
/**
 * 根据配置创建
 * 
 * @return
 */
private static JedisManager createJedisManager() {

    // 池基本配置
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxIdle(Conf.getInt("redis.maxIdle"));
    config.setMinIdle(Conf.getInt("redis.minIdle"));
    config.setMaxWaitMillis(Conf.getLong("redis.maxWaitMillis"));
    config.setTestOnBorrow(Conf.getBoolean("redis.testOnBorrow"));
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    // 链接
    shards.add(new JedisShardInfo(Conf.getString("redis.host"), Conf.getInt("redis.port"), Conf
            .getInt("redis.timeout")));

    // 构造池
    ShardedJedisPool shardedJedisPool = new ShardedJedisPool(config, shards);

    return new JedisManager(shardedJedisPool);
}
 
Example 6
Source File: RedisConfiguration.java    From heimdall with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a configured {@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 7
Source File: JedisSessionRepositoryFactory.java    From HttpSessionReplacer with MIT License 5 votes vote down vote up
/**
 * Configures Jedis pool of connection.
 * 
 * @param config
 *
 * @return configured Jedis pool of connection.
 */
static JedisPoolConfig configurePool(RedisConfiguration config) {
  JedisPoolConfig poolConfig = new JedisPoolConfig();
  poolConfig.setMaxTotal(Integer.parseInt(config.poolSize));
  poolConfig.setMaxIdle(Math.min(poolConfig.getMaxIdle(), poolConfig.getMaxTotal()));
  poolConfig.setMinIdle(Math.min(poolConfig.getMinIdle(), poolConfig.getMaxIdle()));
  return poolConfig;
}
 
Example 8
Source File: RedisJedisManager.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RedisJedisManager(String host, int port, int database, String password) {
  JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  jedisPoolConfig.setMaxTotal(maxTotal);
  jedisPoolConfig.setMaxIdle(maxIdle);
  jedisPoolConfig.setMinIdle(minIdle);
  this.host = host;
  this.port = port;
  this.database = database;
  this.password = password;
  this.jedisPoolConfig = jedisPoolConfig;
  this.jedisPoolCache = CacheBuilder.newBuilder()
      .removalListener(new JedisPoolRemovalListener())
      .build(CacheLoader.from(this::createConsumer));
}
 
Example 9
Source File: DFRedisUtil.java    From dfactor with MIT License 5 votes vote down vote up
public static JedisPool createJedisPool(String host, int port, String auth,
		int maxTotal, int maxIdle, int minIdle, int connTimeoutMilli, int borrowTimeoutMilli){
	JedisPoolConfig config = new JedisPoolConfig();  
       //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。  
       config.setMaxTotal(maxTotal); 
       //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。  
       config.setMaxIdle(maxIdle);  
       config.setMinIdle(minIdle);
       //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;  
       config.setMaxWaitMillis(borrowTimeoutMilli);
       //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;  
       config.setTestOnBorrow(true);  
       return new JedisPool(config, host, port, connTimeoutMilli, auth);
}
 
Example 10
Source File: RedisList.java    From Voovan with Apache License 2.0 5 votes vote down vote up
/**
 * 构造函数
 * @param host        redis 服务地址
 * @param port        redis 服务端口
 * @param timeout     redis 连接超时时间
 * @param poolsize    redis 连接池的大小
 * @param name        在 redis 中的 HashMap的名称
 */
public RedisList(String host, int port, int timeout, int poolsize, String name){
    super();

    //如果没有指定JedisPool的配置文件,则使用默认的
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxIdle(poolsize);
    poolConfig.setMaxTotal(poolsize);

    redisPool = new JedisPool(poolConfig, host, port, timeout);
    this.name = name;
}
 
Example 11
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 12
Source File: RedisCacheConfiguration.java    From NetworkDisk_Storage with GNU General Public License v2.0 5 votes vote down vote up
public JedisPool getRedisPoolFactory() {
    logger.info("JedisPool注入成功!!");
    logger.info("redis地址:" + host + ":" + port);
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxIdle(maxIdle);
    jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
    JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
    return jedisPool;
}
 
Example 13
Source File: DefaultCacheManager.java    From FATE-Serving with Apache License 2.0 5 votes vote down vote up
public   static  void  main(String[] args){

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        System.err.println("oooooooooooo");
        jedisPoolConfig.setMaxTotal(10);
        jedisPoolConfig.setMaxIdle(10);
        System.err.println("11111111");
        JedisPool jedisPool = new JedisPool(jedisPoolConfig,
                "localhost",
          6379,
        2000,
                null
        );

        jedisPool.getResource();

    }
 
Example 14
Source File: RedisClientConfig.java    From scaffold-cloud with MIT License 5 votes vote down vote up
@Bean(name = "jedisPool")
public JedisPool redisPoolFactory() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxIdle(maxIdle);
    jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);

    JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password, database, null, false);
    log.info("JedisPool注入成功!! host={},port={},database={}", host, port, database);
    return jedisPool;
}
 
Example 15
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 16
Source File: AbstractRedisCache.java    From framework with Apache License 2.0 5 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @return <br>
 */
protected JedisPoolConfig getConfig() {
    JedisPoolConfig config = new JedisPoolConfig();
    // 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
    config.setMaxIdle(PropertyHolder.getIntProperty("cache.redis.max.idle", MAX_IDLE));
    // 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
    config.setMaxWaitMillis(
        GlobalConstants.SECONDS * PropertyHolder.getIntProperty("cache.redis.max.wait", MAX_WAIT));
    // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
    config.setTestOnBorrow(PropertyHolder.getBooleanProperty("cache.redis.testonborrow", VALIDATE));
    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: 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 19
Source File: RedisAutoConfiguration.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
/**
 * RedisConnectionFactory. 
 * @param host String
 * @param port int
 * @param timeout int
 * @param password String
 * @param maxActive int
 * @param maxWait int
 * @param maxIdle int
 * @param minIdle int
 * @return RedisConnectionFactory
 */
@Bean
public RedisConnectionFactory redisConnectionFactory(
        @Value("${spring.redis.host}")
        String host,
        @Value("${spring.redis.port}")
        int port,
        @Value("${spring.redis.timeout}")
        int timeout,
        @Value("${spring.redis.password}")
        String password,
        @Value("${spring.redis.lettuce.pool.max-active}")
        int maxActive,
        @Value("${spring.redis.jedis.pool.max-wait}")
        int maxWait,
        @Value("${spring.redis.jedis.pool.max-idle}")
        int maxIdle,
        @Value("${spring.redis.lettuce.pool.min-idle}")
        int minIdle) {
    _logger.debug("RedisConnectionFactory init .");
    RedisConnectionFactory factory = new RedisConnectionFactory();
    factory.setHostName(host);
    factory.setPort(port);
    factory.setTimeOut(timeout); 
    factory.setPassword(password);
    
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMinIdle(minIdle);
    poolConfig.setMaxTotal(maxActive);
    poolConfig.setMaxWaitMillis(maxWait);
    
    factory.setPoolConfig(poolConfig);
    
    return factory;
}
 
Example 20
Source File: MyTest.java    From ehousechina with Apache License 2.0 4 votes vote down vote up
@Test
public void test1() {

	Snowflake s = SnowflakeUtil.getSnowflake();

	JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
	jedisPoolConfig.setMaxTotal(500);
	jedisPoolConfig.setMaxIdle(500);
	jedisPoolConfig.setMaxWaitMillis(-1);
	jedisPoolConfig.setTestOnBorrow(true);
	jedisPoolConfig.setTestOnReturn(true);
	JedisPool jedisPool = new JedisPool(jedisPoolConfig, "10.99.70.53", 32853);
	try {
		while (true) {
			i++;
			long id = s.next();
			System.out.println(i + "----" + id);
			// pool.execute(new Runnable() {
			// @Override
			// public void run() {
			// i++;
			// Jedis jedis = jedisPool.getResource();
			// long id = s.next();
			// System.out.println(i+"----"+id);
			// long value = jedis.setnx(String.valueOf(id), "");
			// if(value==0){
			// System.out.println("1111111111111111111111");
			// }
			// jedis.close();
		}
		// });
		// TimeUnit.NANOSECONDS.sleep(1);
		// }
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		jedisPool.close();
	}
	// long value = jedis.setnx("test", "test");
	// System.out.println(value);
	/*
	 * jedis.close(); jedisPool.close();
	 */
}