彻底解决 Jedis 连接池 获取不到连接,连接放回连接池错误的问题

上一篇 / 下一篇  2018-02-11 16:17:58 / 个人分类:其它

  1. public class CacheKit {  
  2.     private static Logger logger = LoggerFactory.getLogger(CacheKit.class);  
  3.     private List<JSONObject> resultList;  
  4.     private static JedisPool pool;  
  5.   
  6.     /** 
  7.      * 初始化Redis连接池 
  8.      */  
  9.     private static void initializePool() {  
  10.         //redisURL 与 redisPort 的配置文件  
  11.         String configFile = "production.properties";  
  12.         if (PropKit.getBoolean("devMode")) {  
  13.             configFile = "dev.properties";  
  14.         }  
  15.   
  16.         JedisPoolConfig config = new JedisPoolConfig();  
  17.         //设置最大连接数(100个足够用了,没必要设置太大)  
  18.         config.setMaxTotal(100);  
  19.         //最大空闲连接数  
  20.         config.setMaxIdle(10);  
  21.         //获取Jedis连接的最大等待时间(50秒)   
  22.         config.setMaxWaitMillis(50 * 1000);  
  23.         //在获取Jedis连接时,自动检验连接是否可用  
  24.         config.setTestOnBorrow(true);  
  25.         //在将连接放回池中前,自动检验连接是否有效  
  26.         config.setTestOnReturn(true);  
  27.         //自动测试池中的空闲连接是否都是可用连接  
  28.         config.setTestWhileIdle(true);  
  29.         //创建连接池  
  30.         pool = new JedisPool(config, PropKit.use(configFile).get("redisURL"),  
  31.                     PropKit.use(configFile).getInt("redisPort"));  
  32.     }  
  33.   
  34.     /** 
  35.      * 多线程环境同步初始化(保证项目中有且仅有一个连接池) 
  36.      */  
  37.     private static synchronized void poolInit() {  
  38.         if (null == pool) {  
  39.             initializePool();  
  40.         }  
  41.     }  
  42.   
  43.     /** 
  44.      * 获取Jedis实例 
  45.      */  
  46.     private static Jedis getJedis() {  
  47.         if (null == pool) {  
  48.             poolInit();  
  49.         }  
  50.   
  51.         int timeoutCount = 0;  
  52.         while (true) {  
  53.             try {  
  54.                 if (null != pool) {  
  55.                     return pool.getResource();  
  56.                 }  
  57.             } catch (Exception e) {  
  58.                 if (e instanceof JedisConnectionException) {  
  59.                     timeoutCount++;  
  60.                     logger.warn("getJedis timeoutCount={}", timeoutCount);  
  61.                     if (timeoutCount > 3) {  
  62.                         break;  
  63.                     }  
  64.                 } else {  
  65.                     logger.warn("jedisInfo ... NumActive=" + pool.getNumActive()  
  66.                             + ", NumIdle=" + pool.getNumIdle()  
  67.                             + ", NumWaiters=" + pool.getNumWaiters()  
  68.                             + ", isClosed=" + pool.isClosed());  
  69.                     logger.error("GetJedis error,", e);  
  70.                     break;  
  71.                 }  
  72.             }  
  73.             break;  
  74.         }  
  75.         return null;  
  76.     }  
  77.   
  78.     /** 
  79.      * 释放Jedis资源 
  80.      * 
  81.      * @param jedis 
  82.      */  
  83.     public static void returnResource(Jedis jedis) {  
  84.         if (null != jedis) {  
  85.             pool.returnResourceObject(jedis);  
  86.         }  
  87.     }  
  88.   
  89.     /** 
  90.      * 绝对获取方法(保证一定能够使用可用的连接获取到 目标数据) 
  91.      * Jedis连接使用后放回  
  92.      * @param key 
  93.      * @return 
  94.      */  
  95.     private String safeGet(String key) {  
  96.         Jedis jedis = getJedis();  
  97.         while (true) {  
  98.             if (null != jedis) {  
  99.                 break;  
  100.             } else {  
  101.                 jedis = getJedis();  
  102.             }  
  103.         }  
  104.         String value = jedis.get(key);  
  105.         returnResource(jedis);  
  106.         return value;  
  107.     }  
  108.   
  109.     /** 
  110.      * 绝对设置方法(保证一定能够使用可用的链接设置 数据) 
  111.      * Jedis连接使用后返回连接池 
  112.      * @param key 
  113.      * @param time 
  114.      * @param value 
  115.      */  
  116.     private void safeSet(String key, int time, String value) {  
  117.         Jedis jedis = getJedis();  
  118.         while (true) {  
  119.             if (null != jedis) {  
  120.                 break;  
  121.             } else {  
  122.                 jedis = getJedis();  
  123.             }  
  124.         }  
  125.         jedis.setex(key, time, value);  
  126.         returnResource(jedis);  
  127.     }  
  128.   
  129.     /** 
  130.      * 绝对删除方法(保证删除绝对有效) 
  131.      * Jedis连接使用后返回连接池</span> 
  132.      * @param key 
  133.      */  
  134.     private void safeDel(String key) {  
  135.         Jedis jedis = getJedis();  
  136.         while (true) {  
  137.             if (null != jedis) {  
  138.                 break;  
  139.             } else {  
  140.                 jedis = getJedis();  
  141.             }  
  142.         }  
  143.         jedis.del(key);  
  144.         returnResource(jedis);  
  145.     }  
  146.   
  147.     /**自定义的一些 get set del 方法,方便使用**/  
  148.     public JSONObject getByCache(String key) {  
  149.         String result = safeGet(key);  
  150.         if (result != null) {  
  151.             return (JSONObject) JSONObject.parse(result);  
  152.         }  
  153.         return null;  
  154.   
  155.     }  
  156.   
  157.     public String getByCacheToString(String key) {  
  158.         String result = safeGet(key);  
  159.         if (result != null) {  
  160.             return result;  
  161.         }  
  162.         return null;  
  163.   
  164.     }  
  165.   
  166.     public List<JSONObject> getArrayByCache(String key) {  
  167.         String result = safeGet(key);  
  168.         if (result != null) {  
  169.             resultList = JSONArray.parseArray(result, JSONObject.class);  
  170.             return resultList;  
  171.         }  
  172.         return null;  
  173.     }  
  174.   
  175.     public JSONArray getJSONArrayByCache(String key) {  
  176.         String result = safeGet(key);  
  177.         if (result != null) {  
  178.             return JSONArray.parseArray(result);  
  179.         }  
  180.         return null;  
  181.     }  
  182.   
  183.     public void setByCache(String key, String s) {  
  184.         safeSet(key, 86400, s);  
  185.     }  
  186.   
  187.     public void setByCacheOneHour(String key, String s) {  
  188.         safeSet(key, 3600, s);  
  189.     }  
  190.   
  191.     public void setByCacheOneHour(String key, List<JSONObject> json) {  
  192.         safeSet(key, 86400, JSONObject.toJSONString(json));  
  193.         resultList = json;  
  194.     }  
  195.   
  196.     public void setByCache(String key, JSONObject json) {  
  197.         safeSet(key, 86400, JSONObject.toJSONString(json));  
  198.     }  
  199.   
  200.     public void setByCache(String key, List<JSONObject> list) {  
  201.         safeSet(key, 86400, JSONObject.toJSONString(list));  
  202.         resultList = list;  
  203.     }  
  204.   
  205.     public void setByCache(String key, JSONArray array) {  
  206.         safeSet(key, 86400, JSONArray.toJSONString(array));  
  207.     }  
  208.   
  209.     public void setByCacheCusTime(String key, String s, int time) {  
  210.         safeSet(key, time, s);  
  211.     }  
  212.   
  213.   
  214.     public void delByCache(String key) {  
  215.         if (null != safeGet(key)) {  
  216.             safeDel(key);  
  217.         }  
  218.     }  
  219.   
  220.     public JSONObject toJSON(DBObject db) {  
  221.         return (JSONObject) JSONObject.toJSON(db);  
  222.     }  
  223.   
  224.     public List<JSONObject> toJSON(List<DBObject> list) {  
  225.         List<JSONObject> json = new ArrayList<>();  
  226.         for (DBObject aList : list) {  
  227.             json.add((JSONObject) JSONObject.toJSON(aList));  
  228.         }  
  229.         return json;  
  230.     }  
  231.   
  232.     public boolean notNull() {  
  233.         return resultList != null && resultList.size() > 0;  
  234.     }  
  235.   
  236.     public List<JSONObject> getResult() {  
  237.         return resultList;  
  238.     }  
  239.   
  240. }

TAG:

 

评分:0

我来说两句

Open Toolbar