Java Code Examples for redis.clients.jedis.JedisPoolConfig

The following examples show how to use redis.clients.jedis.JedisPoolConfig. These examples are extracted from open source projects. 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 Project: fastjgame   Source File: RedisEventLoopTest.java    License: Apache License 2.0 7 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 Project: netty-cookbook   Source File: RedisConnectionPoolConfig.java    License: Apache License 2.0 6 votes vote down vote up
public static JedisPoolConfig getJedisPoolConfigInstance(){
	if(_instance == null){
		try {
			String json = FileUtils.readFileAsString(getRedisPoolConnectionConfigFile());
			_instance = new Gson().fromJson(json, RedisConnectionPoolConfig.class);
		}
		catch (Exception e) {
			if (e instanceof JsonSyntaxException) {
				e.printStackTrace();
				System.err.println("Wrong JSON syntax in file "+getRedisPoolConnectionConfigFile());
			}
			else {
				e.printStackTrace();
			}
		}
	}
	return _instance.getJedisPoolConfig();
}
 
Example 3
Source Project: Mars-Java   Source File: JedisConfig.java    License: MIT License 6 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 4
Source Project: gameserver   Source File: BillingJedis.java    License: Apache License 2.0 6 votes vote down vote up
private BillingJedis() {
	JedisPoolConfig poolConfig = new JedisPoolConfig();
	poolConfig.setMaxActive(5);
	poolConfig.setMinIdle(5);
	
	int i=1;
	while ( true ) {
		String key = StringUtil.concat(prefix, i++);
		String redisId = GlobalConfig.getInstance().getStringProperty(key);
		if ( redisId == null ) {
			break;
		} else {
			String[] redisHostPort = StringUtil.splitMachineId(redisId);
			String jedisHost = redisHostPort[0];
			int jedisPort = StringUtil.toInt(redisHostPort[1], 6379);
			Jedis jedis = new JedisAdapter(jedisHost, jedisPort, poolConfig);
			logger.info("Connect to {} {}", new Object[]{key, redisId});
			jedises.add(jedis);
		}
	}
}
 
Example 5
Source Project: MineCloud   Source File: RedisDatabase.java    License: ISC License 6 votes vote down vote up
@Override
public void setup() {
    JedisPoolConfig config = new JedisPoolConfig();

    config.setMaxTotal(20);
    config.setMinIdle(5);
    config.setMaxIdle(10);
    config.setMaxWaitMillis(200L);
    config.setBlockWhenExhausted(false);

    String host = credentials.hosts()[0];
    int port = 6379;

    if (host.split(":").length == 2) {
        try {
            port = Integer.parseInt(host.split(":")[1]);
        } catch (NumberFormatException ignored) {
            MineCloud.logger().warning("Host " + host + " has an invalid port!");
        }
    }

    pool = credentials.password() != null && credentials.password().length > 0 ? new JedisPool(config, host, port, 1000, new String(credentials.password())) :
            new JedisPool(config, host, port, 1000);
}
 
Example 6
Source Project: tutorials   Source File: JedisIntegrationTest.java    License: MIT License 6 votes vote down vote up
@Test
public void givenAPoolConfiguration_thenCreateAJedisPool() {
    final JedisPoolConfig poolConfig = buildPoolConfig();

    try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost", port); Jedis jedis = jedisPool.getResource()) {

        // do simple operation to verify that the Jedis resource is working
        // properly
        String key = "key";
        String value = "value";

        jedis.set(key, value);
        String value2 = jedis.get(key);

        Assert.assertEquals(value, value2);

        // flush Redis
        jedis.flushAll();
    }
}
 
Example 7
Source Project: word_web   Source File: JedisListener.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
   public void contextInitialized(ServletContextEvent sce) {
       ServletContext sc = sce.getServletContext();
       String redisHost = sc.getInitParameter("redis.host");
       String redisPort = sc.getInitParameter("redis.port");
       LOGGER.info("redis.host: "+redisHost);
       LOGGER.info("redis.port: "+redisPort);
LOGGER.info("开始初始化JedisPool");
       try{
           JedisPoolConfig jedispool_config = new JedisPoolConfig();
           jedisPool = new JedisPool(jedispool_config, redisHost, Integer.parseInt(redisPort));
           ResourceChangeNotifier.setJedisPool(jedisPool);
           LOGGER.info("初始化JedisPool成功");
}catch(Exception e){
    LOGGER.error("初始化JedisPool失败", e);
}     
   }
 
Example 8
Source Project: netty-cookbook   Source File: RedisConnectionPoolConfig.java    License: Apache License 2.0 6 votes vote down vote up
public static JedisPoolConfig getJedisPoolConfigInstance(){
	if(_instance == null){
		try {
			String json = FileUtils.readFileAsString(getRedisPoolConnectionConfigFile());
			_instance = new Gson().fromJson(json, RedisConnectionPoolConfig.class);
		}
		catch (Exception e) {
			if (e instanceof JsonSyntaxException) {
				e.printStackTrace();
				System.err.println("Wrong JSON syntax in file "+getRedisPoolConnectionConfigFile());
			}
			else {
				e.printStackTrace();
			}
		}
	}
	return _instance.getJedisPoolConfig();
}
 
Example 9
Source Project: J2Cache   Source File: RedisUtils.java    License: 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 10
Source Project: ShiroJwt   Source File: JedisConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public JedisPool redisPoolFactory() {
    try {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWait);
        jedisPoolConfig.setMaxTotal(maxActive);
        jedisPoolConfig.setMinIdle(minIdle);
        // 密码为空设置为null
        if (StringUtil.isBlank(password)) {
            password = null;
        }
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
        logger.info("初始化Redis连接池JedisPool成功!地址: {}:{}", host, port);
        return jedisPool;
    } catch (Exception e) {
        logger.error("初始化Redis连接池JedisPool异常:{}", e.getMessage());
    }
    return null;
}
 
Example 11
Source Project: xian   Source File: Cache.java    License: Apache License 2.0 6 votes vote down vote up
public Cache(String name, String host, int port, int timeOut, String password, int dbIndex, JedisPoolConfig jedisPoolConfig) {
    this.name = name;

    if (port < 0 || port > 65536)
        throw new IllegalArgumentException(String.format("端口: %s, 为非法参数, 取值范围为 0-65536", port));
    if (dbIndex < 0 || dbIndex > 15)
        throw new IllegalArgumentException(String.format("DB Index: %s, 为非法参数, 取值范围为 0-15", dbIndex));

    LOG.info(String.format(this.name + ", Redis 初始化入参: host: %s, port: %s, dbIndex: %s", host, port, dbIndex));

    if (password != null && !"".equals(password))
        this.jedisPool = new JedisPool(jedisPoolConfig, host, port, timeOut, password);
    else
        this.jedisPool = new JedisPool(jedisPoolConfig, host, port, timeOut);

    LOG.info(String.format(this.name + ", Redis 初始化成功, TIME_OUT: %s", timeOut));

    this.dbIndex = dbIndex;

    /**
     * 健康检查
     */
    highAvailable();
}
 
Example 12
Source Project: xian   Source File: Redis.java    License: Apache License 2.0 6 votes vote down vote up
private synchronized static void initRedis(String host, int port, String password, int dbIndex, JedisPoolConfig jedisPoolConfig) {
    String name = dataSourceName(host);

    if (CACHE.containsKey(name))
        return;

    Cache cache = new Cache(name, host, port, TIME_OUT, password, dbIndex, jedisPoolConfig);

    Cache _cache = CACHE.putIfAbsent(cache.getName(), cache);

    if (_cache != null && _cache != cache) {
        if (!_cache.highAvailable()) {
            _cache.destroy();

            if (!isHostOrIP(host))
                removeMapping(host, _cache.getName());

            CACHE.put(cache.getName(), cache);
        } else {
            cache.destroy();

            if (!isHostOrIP(host))
                removeMapping(host, cache.getName());
        }
    }
}
 
Example 13
Source Project: quartz-redis-jobstore   Source File: BaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUpRedis() throws IOException, SchedulerConfigException {
    port = getPort();
    logger.debug("Attempting to start embedded Redis server on port " + port);
    redisServer = RedisServer.builder()
            .port(port)
            .build();
    redisServer.start();
    final short database = 1;
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setTestOnBorrow(true);
    jedisPool = new JedisPool(jedisPoolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, database);

    jobStore = new RedisJobStore();
    jobStore.setHost(host);
    jobStore.setLockTimeout(2000);
    jobStore.setPort(port);
    jobStore.setInstanceId("testJobStore1");
    jobStore.setDatabase(database);
    mockScheduleSignaler = mock(SchedulerSignaler.class);
    jobStore.initialize(null, mockScheduleSignaler);
    schema = new RedisJobStoreSchema();

    jedis = jedisPool.getResource();
    jedis.flushDB();
}
 
Example 14
Source Project: moon-api-gateway   Source File: JedisFactory.java    License: 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 15
Source Project: netty-cookbook   Source File: RedisConnectionPoolConfig.java    License: Apache License 2.0 6 votes vote down vote up
public static JedisPoolConfig getJedisPoolConfigInstance(){
	if(_instance == null){
		try {
			String json = FileUtils.readFileAsString(getRedisPoolConnectionConfigFile());
			_instance = new Gson().fromJson(json, RedisConnectionPoolConfig.class);
		}
		catch (Exception e) {
			if (e instanceof JsonSyntaxException) {
				e.printStackTrace();
				System.err.println("Wrong JSON syntax in file "+getRedisPoolConnectionConfigFile());
			}
			else {
				e.printStackTrace();
			}
		}
	}
	return _instance.getJedisPoolConfig();
}
 
Example 16
Source Project: MaxKey   Source File: RedisAutoConfiguration.java    License: 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 17
Source Project: pepper-metrics   Source File: JedisSampleMain.java    License: 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 18
Source Project: fw-spring-cloud   Source File: RedisAutoConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public JedisPool redisPoolFactory()  throws Exception{
    log.info("JedisPool注入成功!!");
    log.info("redis地址:" + host + ":" + port);
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxIdle(maxIdle);
    jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
    // 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
    jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
    // 是否启用pool的jmx管理功能, 默认true
    jedisPoolConfig.setJmxEnabled(true);
    JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
    return jedisPool;
}
 
Example 19
Source Project: netty-cookbook   Source File: RedisConnectionPoolConfig.java    License: 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 20
Source Project: jseckill   Source File: RedisCacheConfig.java    License: 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 21
Source Project: grain   Source File: RedisManager.java    License: MIT License 5 votes vote down vote up
/**
 * 初始化redis
 * 
 * @param ip
 *            ip地址
 * @param port
 *            端口
 * @param log
 *            日志可以为null
 */
public static void init(String ip, int port, ILog log) {
	RedisManager.log = log;
	JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
	jedisPoolConfig.setMaxTotal(2000);
	jedisPoolConfig.setMaxIdle(200);
	jedisPoolConfig.setMinIdle(0);
	jedisPoolConfig.setTestOnBorrow(true);
	jedisPool = new JedisPool(jedisPoolConfig, ip, port);
	serializer = new SerializingConverter(log);
	deserializer = new DeserializingConverter(log);
}
 
Example 22
Source Project: tutorials   Source File: JedisIntegrationTest.java    License: 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 23
Source Project: jframe   Source File: JedisServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
JedisPoolConfig createPoolConfig(PropsConf conf, String id) {
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(conf.getConfInt(id, "redis.conn.maxTotal", "200"));
    config.setMaxIdle(conf.getConfInt(id, "redis.conn.maxIdle", "100"));
    config.setMinIdle(conf.getConfInt(id, "redis.conn.minIdle", "1"));
    config.setMaxWaitMillis(conf.getConfLong(id, "redis.conn.maxWaitMillis", "3000"));
    // config.setTestOnReturn(true);
    return config;
}
 
Example 24
Source Project: cachecloud   Source File: JedisPoolTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void customClientName() {
  JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000,
      "foobared", 0, "my_shiny_client_name");

  Jedis jedis = pool0.getResource();

  assertEquals("my_shiny_client_name", jedis.clientGetname());

  jedis.close();
  pool0.destroy();
  assertTrue(pool0.isClosed());
}
 
Example 25
Source Project: charging_pile_cloud   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
JedisPoolConfig jedisPoolConfig(){
    JedisPoolConfig poolConfig = new  JedisPoolConfig();
    poolConfig.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());
    poolConfig.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());
    poolConfig.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());
    poolConfig.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMaxWait().toMillis());
    return poolConfig;

}
 
Example 26
Source Project: Voovan   Source File: RedisList.java    License: 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 27
Source Project: FATE-Serving   Source File: DefaultCacheManager.java    License: 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 28
Source Project: spring-boot-vue-admin   Source File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public RedisConnectionFactory redisConnectionFactory(
    @Qualifier(value = "jedisPoolConfig") final JedisPoolConfig jedisPoolConfig) {
  // 方法上的 @ConfigurationProperties 不生效
  // 未知 bug,暂时这样手动设置
  // fixme
  // 单机版 jedis
  final RedisStandaloneConfiguration redisStandaloneConfiguration =
      new RedisStandaloneConfiguration(
          this.redisProperties.getHost(), this.redisProperties.getPort());
  redisStandaloneConfiguration.setDatabase(this.redisProperties.getDatabase());
  redisStandaloneConfiguration.setPassword(this.redisProperties.getPassword());

  // 获得默认的连接池构造器
  final JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb =
      (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)
          JedisClientConfiguration.builder();
  // 指定 jedisPoolConifig 来修改默认的连接池构造器
  jpcb.poolConfig(jedisPoolConfig);
  // 连接超时
  jpcb.and().connectTimeout(Duration.ofSeconds(10));
  // 通过构造器来构造 jedis 客户端配置
  final JedisClientConfiguration jedisClientConfiguration = jpcb.build();
  // 单机配置 + 客户端配置 = jedis 连接工厂
  return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
 
Example 29
@Test
public void testClusterRedisFacade() {
  JedisSessionRepositoryFactory factory = spy(new JedisSessionRepositoryFactory());
  SessionConfiguration sessionConfig = spy(new SessionConfiguration());
  RedisConfiguration config = spy(new RedisConfiguration(sessionConfig));
  JedisClusterFacade clusterFacade = mock(JedisClusterFacade.class);
  doReturn(clusterFacade).when(factory).clusterFacade(any(JedisPoolConfig.class), any(RedisConfiguration.class));
  config.server = "1.2.3.4:1;2.3.4.5:2";
  config.clusterMode = "CLUSTER";
  RedisFacade facade = factory.getRedisFacade(config);
  verify(factory).clusterFacade(any(JedisPoolConfig.class), eq(config));
  assertSame(clusterFacade, facade);
}
 
Example 30
Source Project: SpringBoot-Home   Source File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public JedisPool redisPoolFactory() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxIdle(maxIdle);
    jedisPoolConfig.setMaxWaitMillis(maxWait);
    jedisPoolConfig.setMinIdle(minIdle);

    JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);

    return jedisPool;
}