今天遇到一个问题 老提示redis线程未打开,
搜了一下 整理了一把redis配置如下:
public class RedisClient {
//Redis服务器IP private static String ADDR = "127.0.0.1"; //Redis的端口号 private static int PORT = 6379; //访问密码 private static String AUTH = "123456"; //可用连接实例的最大数目,默认值为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 pool; private List<JSONObject> resultList; /** * 初始化Redis连接池 */ private static void initializePool() { try { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(MAX_ACTIVE); config.setMaxIdle(MAX_IDLE); config.setMaxWaitMillis(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); //在将连接放回池中前,自动检验连接是否有效 config.setTestOnReturn(true); //自动测试池中的空闲连接是否都是可用连接 config.setTestWhileIdle(true); pool = new JedisPool(config, ADDR, PORT,TIMEOUT,AUTH); } catch (Exception e) { e.printStackTrace(); } } /** * 多线程环境同步初始化(保证项目中有且仅有一个连接池) */ private static synchronized void poolInit() { if (null == pool) { initializePool(); } } /** * 获取Jedis实例 * @return */ public synchronized static Jedis getJedis() { if (null == pool) { poolInit(); }int timeoutCount = 0;
while (true) { try { if (null != pool) { return pool.getResource(); } } catch (Exception e) { if (e instanceof JedisConnectionException) { timeoutCount++; if (timeoutCount > 3) { break; } } else { System.out.println("jedisInfo ... NumActive=" + pool.getNumActive() + ", NumIdle=" + pool.getNumIdle() + ", NumWaiters=" + pool.getNumWaiters() + ", isClosed=" + pool.isClosed()); break; } } break; } return null; } /** * 释放jedis资源 * @param jedis */ public static void returnResource(final Jedis jedis) { if (jedis != null) { pool.close(); } } /以下为资料 ,本人没用,只用了上面的getredis方法/// /**
* 绝对获取方法(保证一定能够使用可用的连接获取到 目标数据)
* Jedis连接使用后放回
* @param key
* @return
*/
private String safeGet(String key) {
Jedis jedis = getJedis();
while (true) {
if (null != jedis) {
break;
} else {
jedis = getJedis();
}
}
String value = jedis.get(key);
returnResource(jedis);
return value;
}
/**
* 绝对设置方法(保证一定能够使用可用的链接设置 数据)
* Jedis连接使用后返回连接池
* @param key
* @param time
* @param value
*/
private void safeSet(String key, int time, String value) {
Jedis jedis = getJedis();
while (true) {
if (null != jedis) {
break;
} else {
jedis = getJedis();
}
}
jedis.setex(key, time, value);
returnResource(jedis);
}
/**
* 绝对删除方法(保证删除绝对有效)
* Jedis连接使用后返回连接池</span>
* @param key
*/
private void safeDel(String key) {
Jedis jedis = getJedis();
while (true) {
if (null != jedis) {
break;
} else {
jedis = getJedis();
}
}
jedis.del(key);
returnResource(jedis);
}
/**自定义的一些 get set del 方法,方便使用**/
public JSONObject getByCache(String key) {
String result = safeGet(key);
if (result != null) {
return (JSONObject) JSONObject.parse(result);
}
return null;
}
public String getByCacheToString(String key) {
String result = safeGet(key);
if (result != null) {
return result;
}
return null;
}
public List<JSONObject> getArrayByCache(String key) {
String result = safeGet(key);
if (result != null) {
resultList = JSONArray.parseArray(result, JSONObject.class);
return resultList;
}
return null;
}
public JSONArray getJSONArrayByCache(String key) {
String result = safeGet(key);
if (result != null) {
return JSONArray.parseArray(result);
}
return null;
}
public void setByCache(String key, String s) {
safeSet(key, 86400, s);
}
public void setByCacheOneHour(String key, String s) {
safeSet(key, 3600, s);
}
public void setByCacheOneHour(String key, List<JSONObject> json) {
safeSet(key, 86400, JSONObject.toJSONString(json));
resultList = json;
}
public void setByCache(String key, JSONObject json) {
safeSet(key, 86400, JSONObject.toJSONString(json));
}
public void setByCache(String key, List<JSONObject> list) {
safeSet(key, 86400, JSONObject.toJSONString(list));
resultList = list;
}
public void setByCache(String key, JSONArray array) {
safeSet(key, 86400, JSONArray.toJSONString(array));
}
public void setByCacheCusTime(String key, String s, int time) {
safeSet(key, time, s);
}
public void delByCache(String key) {
if (null != safeGet(key)) {
safeDel(key);
}
}
public JSONObject toJSON(DBObject db) {
return (JSONObject) JSONObject.toJSON(db);
}
public List<JSONObject> toJSON(List<DBObject> list) {
List<JSONObject> json = new ArrayList<>();
for (DBObject aList : list) {
json.add((JSONObject) JSONObject.toJSON(aList));
}
return json;
}
public boolean notNull() {
return resultList != null && resultList.size() > 0;
}
public List<JSONObject> getResult() {
return resultList;
}
/测试main方法///
public static void main(String[] args) {
try { testJedisPool(); } catch (Exception e) { e.printStackTrace(); } } public static void testJedisPool() throws Exception { //创建一个连接池对象 //系统中应该是单例的。 JedisPool jedisPool = null; //从连接池中获得一个连接 Jedis jedis = null; try { jedisPool = new JedisPool("60.205.59.98", 6379); jedis = jedisPool.getResource(); jedis.auth("changdee8068"); jedis.set("name", "ceshi"); System.out.println(jedis.get("name")); } catch (Exception e) { e.printStackTrace(); }finally{ //jedis必须关闭 jedis.close(); //系统关闭时关闭连接池 jedisPool.close(); } }}