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

The following examples show how to use redis.clients.jedis.JedisPoolConfig#setMaxTotal() . 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: 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 2
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 3
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 4
Source File: RedisConnectionUtil.java    From redis_util with Apache License 2.0 5 votes vote down vote up
public static RedisConnection create() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(50);
    jedisPoolConfig.setMaxIdle(10);
    jedisPoolConfig.setMinIdle(1);
    RedisConnection redisConnection = new RedisConnection();
    redisConnection.setIp("10.110.2.56");
    redisConnection.setPort(52981);
    redisConnection.setPwd("hhSbcpotThgWdnxJNhrzwstSP20DvYOldkjf");
    redisConnection.setClientName(Thread.currentThread().getName());
    redisConnection.setTimeOut(600);
    redisConnection.setJedisPoolConfig(jedisPoolConfig);
    return redisConnection;
}
 
Example 5
Source File: JedisDemo.java    From JavaTutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 多机分布式+连接池。
 */
private static void shardPool() {
    // 生成多机连接信息列表
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add( new JedisShardInfo("127.0.0.1", 6379) );
    shards.add( new JedisShardInfo("192.168.56.102", 6379) );
    
    // 生成连接池配置信息
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxIdle(10);
    config.setMaxTotal(30);
    config.setMaxWaitMillis(3*1000);
    
    // 在应用初始化的时候生成连接池
    ShardedJedisPool pool = new ShardedJedisPool(config, shards);
    
    // 在业务操作时,从连接池获取连接
    ShardedJedis client = pool.getResource();
    try {
        // 执行指令
        String result = client.set("key-string", "Hello, Redis!");
        System.out.println( String.format("set指令执行结果:%s", result) );
        String value = client.get("key-string");
        System.out.println( String.format("get指令执行结果:%s", value) );
    } catch (Exception e) {
        // TODO: handle exception
    } finally {
        // 业务操作完成,将连接返回给连接池
        if (null != client) {
            pool.returnResource(client);
        }
    } // end of try block
    
    // 应用关闭时,释放连接池资源
    pool.destroy();
}
 
Example 6
Source File: RedisDataCaseBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp() {
  try {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(10);
    pool = new JedisPool(jedisPoolConfig, HOST, PORT);
  } catch (Exception e) {
    throw e;
  }
}
 
Example 7
Source File: RedisPool.java    From mmall20180107 with Apache License 2.0 5 votes vote down vote up
private static void initPool(){

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

        jedisPool = new JedisPool(jedisPoolConfig,redisIp,redisPort,2000);

    }
 
Example 8
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 9
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 10
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 11
Source File: RedisPool.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * 禁止被外部实例化
 */
private RedisPool(){
    super();

    try {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(MAX_TOTAL);
        jedisPoolConfig.setMaxIdle(MAX_IDLE);
        jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS);
        jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW);
        jedisPool = new JedisPool(jedisPoolConfig, ADDR, PORT, TIMEOUT);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 12
Source File: RedisClusterConnPool.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
private JedisPoolConfig buildConf() {
    // org.apache.commons.pool2.impl.BaseObjectPoolConfig
    JedisPoolConfig conf = new JedisPoolConfig();
    conf.setMaxTotal(1000);
    conf.setMinIdle(50);
    conf.setMaxIdle(100);
    // conf.setMaxWaitMillis(6 * 1000);
    conf.setTestOnCreate(true);
    conf.setTestOnBorrow(true);
    conf.setTestOnReturn(true);
    conf.setTestWhileIdle(true);
    // conf.setTimeBetweenEvictionRunsMillis(1);
    conf.setNumTestsPerEvictionRun(30);
    return conf;
}
 
Example 13
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 14
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 15
Source File: MyJedisPoolConfig.java    From flycache with Apache License 2.0 5 votes vote down vote up
@Bean
public JedisPoolConfig newJedisPoolConfig() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxIdle(maxIdle);
    jedisPoolConfig.setMaxTotal(maxTotal);
    jedisPoolConfig.setTestOnBorrow(testOnBorrow);
    return jedisPoolConfig;
}
 
Example 16
Source File: JedisFactory.java    From droptools with Apache License 2.0 5 votes vote down vote up
public JedisPool build(Environment environment) {
    final JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMinIdle(getMinIdle());
    poolConfig.setMaxIdle(getMaxIdle());
    poolConfig.setMaxTotal(getMaxTotal());
    poolConfig.setMaxWaitMillis(getTimeout()); // Use configured timeout value as the time to wait for a connection

    final JedisPool pool = new JedisPool(poolConfig, getHost(), getPort(), getTimeout(), getPassword(), ssl);

    environment.lifecycle().manage(new JedisPoolManager(pool));

    return pool;
}
 
Example 17
Source File: RedisMiniServer.java    From calcite with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp() {
  try {
    RedisServer redisServer = new RedisServer(PORT);
    redisServer.start();
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(10);
    pool = new JedisPool(jedisPoolConfig, HOST, PORT);
    makeData();
    System.out.println("The redis server is started at host: " + HOST + " port: " + PORT);
  } catch (Exception e) {
    assertNotNull(e.getMessage());
  }
}
 
Example 18
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 19
Source File: RedisPoolFactory.java    From commafeed with Apache License 2.0 4 votes vote down vote up
public JedisPool build() {
	JedisPoolConfig config = new JedisPoolConfig();
	config.setMaxTotal(maxTotal);

	return new JedisPool(config, host, port, timeout, StringUtils.trimToNull(password), database);
}
 
Example 20
Source File: JedisClusterFactory.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
private static JedisPoolConfig createJedisPoolConfig(RedisShardBackplaneConfig config) {
  JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  jedisPoolConfig.setMaxTotal(config.getJedisPoolMaxTotal());
  return jedisPoolConfig;
}