redis.clients.jedis.JedisPoolConfig Java Examples
The following examples show how to use
redis.clients.jedis.JedisPoolConfig.
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 |
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: JedisConfig.java From Mars-Java with MIT License | 7 votes |
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 #3
Source File: Cache.java From xian with Apache License 2.0 | 6 votes |
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 #4
Source File: RedisConnectionPoolConfig.java From netty-cookbook with Apache License 2.0 | 6 votes |
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 #5
Source File: RedisConnectionPoolConfig.java From netty-cookbook with Apache License 2.0 | 6 votes |
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 #6
Source File: JedisFactory.java From moon-api-gateway with MIT License | 6 votes |
@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 #7
Source File: BillingJedis.java From gameserver with Apache License 2.0 | 6 votes |
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 #8
Source File: RedisDatabase.java From MineCloud with ISC License | 6 votes |
@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 #9
Source File: BaseTest.java From quartz-redis-jobstore with Apache License 2.0 | 6 votes |
@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 #10
Source File: JedisIntegrationTest.java From tutorials with MIT License | 6 votes |
@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 #11
Source File: Redis.java From xian with Apache License 2.0 | 6 votes |
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 #12
Source File: JedisListener.java From word_web with GNU General Public License v3.0 | 6 votes |
@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 #13
Source File: RedisConnectionPoolConfig.java From netty-cookbook with Apache License 2.0 | 6 votes |
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 #14
Source File: RedisUtils.java From J2Cache with Apache License 2.0 | 6 votes |
/** * 初始化 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 #15
Source File: JedisConfig.java From ShiroJwt with MIT License | 6 votes |
@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 #16
Source File: RedisLock.java From Voovan with Apache License 2.0 | 5 votes |
/** * 构造函数 * @param host redis 服务地址 * @param port redis 服务端口 * @param timeout redis 连接超时时间 * @param poolsize redis 连接池的大小 * @param lockName 锁的键名称 */ public RedisLock(String host, int port, int timeout, int poolsize, String lockName){ super(); //如果没有指定JedisPool的配置文件,则使用默认的 JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxIdle(poolsize); poolConfig.setMaxTotal(poolsize); redisPool = new JedisPool(poolConfig, host, port, timeout); this.lockName = lockName; }
Example #17
Source File: RedisConfiguration.java From heimdall with Apache License 2.0 | 5 votes |
/** * Configures and returns a {@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 #18
Source File: RedisPool.java From blog_demos with Apache License 2.0 | 5 votes |
/** * 禁止被外部实例化 */ 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 #19
Source File: DefaultShardedRedisConfiguration.java From spring-redis-plugin with Apache License 2.0 | 5 votes |
@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 #20
Source File: JedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void checkConnections() { JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); Jedis jedis = pool.getResource(); jedis.auth("foobared"); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.close(); pool.destroy(); assertTrue(pool.isClosed()); }
Example #21
Source File: RedisPool.java From blog_demos with Apache License 2.0 | 5 votes |
/** * 禁止被外部实例化 */ 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 #22
Source File: RedisFactory.java From Mykit with Apache License 2.0 | 5 votes |
public static JedisPoolConfig getPoolConfig(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(Integer.parseInt(properties.getProperty(MAXIDLE, "100"))); config.setMinIdle(Integer.parseInt(properties.getProperty(MINIDLE, "1"))); config.setMaxTotal(Integer.parseInt(properties.getProperty(MAXTOTAL, "1000"))); return config; }
Example #23
Source File: RedisConfigurer.java From mySpringBoot with Apache License 2.0 | 5 votes |
@Bean @ConfigurationProperties(prefix="spring.redis") public JedisConnectionFactory getConnectionFactory(){ JedisConnectionFactory factory = new JedisConnectionFactory(); JedisPoolConfig config = getRedisConfig(); factory.setPoolConfig(config); return factory; }
Example #24
Source File: DFRedisUtil.java From dfactor with MIT License | 5 votes |
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 #25
Source File: AbstractTestMode.java From x-pipe with Apache License 2.0 | 5 votes |
protected JedisPool getJedisPool(String ip, int port, int maxTotal, int maxIdle, int timeout) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(maxTotal); config.setMaxIdle(maxIdle); config.setTestOnBorrow(false); config.setTestOnReturn(false); return new JedisPool(config, ip, port, timeout); }
Example #26
Source File: RGTConfigService.java From redis-game-transaction with Apache License 2.0 | 5 votes |
public JedisPoolConfig initRediPoolConfig() throws DataConversionException { Element element = JdomUtils.getRootElemet(FileUtil.getConfigURL(GlobalConstants.RedisConfigFile.REDIS_POOL_CONIFG).getFile()); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); int maxIdle = element.getAttribute("maxIdle").getIntValue(); boolean testWhileIdle = element.getAttribute("testWhileIdle").getBooleanValue(); int timeBetweenEvictionRunsMillis = element.getAttribute("timeBetweenEvictionRunsMillis").getIntValue(); int numTestsPerEvictionRun = element.getAttribute("numTestsPerEvictionRun").getIntValue(); int minEvictableIdleTimeMillis = element.getAttribute("minEvictableIdleTimeMillis").getIntValue(); jedisPoolConfig.setTestWhileIdle(testWhileIdle); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun); jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); return jedisPoolConfig; }
Example #27
Source File: StateConfiguration.java From chaos-lemur with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnProperty("vcap.services.chaos-lemur-persistence.credentials.hostname") JedisPool jedisPool(@Value("${vcap.services.chaos-lemur-persistence.credentials.hostname}") String hostname, @Value("${vcap.services.chaos-lemur-persistence.credentials.port}") Integer port, @Value("${vcap.services.chaos-lemur-persistence.credentials.password}") String password) { return new JedisPool(new JedisPoolConfig(), hostname, port, Protocol.DEFAULT_TIMEOUT, password); }
Example #28
Source File: RedisPool.java From mmall-kay-Java with Apache License 2.0 | 5 votes |
private static void initPool() { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(maxTotal); config.setMaxIdle(maxIdle); config.setMinIdle(minIdle); config.setBlockWhenExhausted(true); //资源耗尽时是否阻塞 config.setTestOnBorrow(testOnborrow); config.setTestOnReturn(testOnReturn); pool = new JedisPool(config, host, port, 1000 * 2); }
Example #29
Source File: RedisConnectionPoolConfig.java From netty-cookbook with Apache License 2.0 | 5 votes |
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 #30
Source File: RedisRegistry.java From light-task-scheduler with Apache License 2.0 | 5 votes |
public RedisRegistry(AppContext appContext) { super(appContext); Config config = appContext.getConfig(); this.clusterName = config.getClusterName(); this.lock = new RedisLock("LTS_CLEAN_LOCK_KEY", config.getIdentity(), 2 * 60); // 锁两分钟过期 JedisPoolConfig redisConfig = new JedisPoolConfig(); // TODO 可以设置n多参数 String address = NodeRegistryUtils.getRealRegistryAddress(config.getRegistryAddress()); String cluster = config.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); this.reconnectPeriod = config.getParameter(ExtConfig.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD); String[] addrs = address.split(","); for (String addr : addrs) { int i = addr.indexOf(':'); String host = addr.substring(0, i); int port = Integer.parseInt(addr.substring(i + 1)); this.jedisPools.put(addr, new JedisPool(redisConfig, host, port, Constants.DEFAULT_TIMEOUT)); } this.expirePeriod = config.getParameter(ExtConfig.REDIS_SESSION_TIMEOUT, 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); }