JedisPool

 1 package redis;
 2 
 3 import redis.clients.jedis.Jedis;
 4 import redis.clients.jedis.JedisPool;
 5 import redis.clients.jedis.JedisPoolConfig;
 6 
 7 public class JedisPoolUtil {
 8     private static volatile JedisPool jedisPool = null;
 9     private JedisPoolUtil() {}
10     public static JedisPool getJedisPoolIntence() {
11         if(null == jedisPool) {
12             synchronized (JedisPoolUtil.class) {
13                 if(null == jedisPool) {
14                     JedisPoolConfig poolConfig = new JedisPoolConfig();
15                     poolConfig.setMaxActive(1000);
16                     poolConfig.setMaxIdle(32);
17                     poolConfig.setMaxWait(100*1000);
18                     poolConfig.setTestOnBorrow(true);
19                     
20                     jedisPool = new JedisPool(poolConfig, "192.168.88.128", 6379);
21                     return jedisPool;
22                 }
23             }
24         }
25         return jedisPool;
26     }
27     
28     public static void relace(JedisPool jedisPool, Jedis jedis) {
29         if(null != jedis) {
30             jedisPool.returnResourceObject(jedis);
31         }
32     }
33 }    
 1 package redis;
 2 
 3 import redis.clients.jedis.Jedis;
 4 import redis.clients.jedis.JedisPool;
 5 
 6 public class TestPool {
 7     public static void main(String[] args) {
 8         JedisPool jedisPool = JedisPoolUtil.getJedisPoolIntence();
 9         Jedis jedis = null;
10         try {
11             jedis = jedisPool.getResource();
12             jedis.set("aa", "bb");
13             System.out.println("success");
14         } catch (Exception e) {
15             e.printStackTrace();
16         }finally {
17             JedisPoolUtil.relace(jedisPool, jedis);
18         }
19     }
20 }

双锁单例

posted @ 2018-02-07 16:47  林**  阅读(209)  评论(0编辑  收藏  举报