forked from fengshao0907/JavaCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJRedisPoolUtils.java
More file actions
73 lines (61 loc) · 2.02 KB
/
Copy pathJRedisPoolUtils.java
File metadata and controls
73 lines (61 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Redis的连接池
*
* @author:chenming
* @date : 2016/10/31 8:56
*/
public class JRedisPoolUtils {
private final static String REDIS_IP = "localhost";
//可用连接实例的最大数目,默认值为8;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
private static int MAX_ACTIVE = 1024;
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 200;
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
/**
* 初始化连接池
*/
static {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWait(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config,REDIS_IP);
}
/**
* 获取Redis实例
*
* @author chenming
* @date 2016-11-06
* @since v1.0.0
*/
public synchronized static Jedis getJedis(){
if(jedisPool != null){
return jedisPool.getResource();
}else{
return null;
}
}
/**
* 释放jedis资源
*
* @author chenming
* @date 2016-11-06
* @since v1.0.0
*/
public static void returnJedis(final Jedis jedis){
if(jedisPool != null){
jedisPool.returnResource(jedis);
}
}
}