import org.apache.http.client.ClientProtocolException;
import redis.clients.jedis.*;
import redis.clients.jedis.exceptions.JedisConnectionException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.DriverManager;
import java.util.*;
public class JedisUtils {
private static JedisPool pool = null;
private static Properties pro = null;
private static void initializePool() {
//加载配置文件
InputStream in = JedisUtils.class.getClassLoader().getResourceAsStream("redis.properties");
pro = new Properties();
try {
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
}
//获得池子对象(redis数据库)
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));//最大闲置个数
poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));//最小闲置个数
poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));//最大连接数
//初始化连接池
pool = new JedisPool(poolConfig, pro.getProperty("redis.host"), Integer.parseInt(pro.get("redis.port").toString()));
}
//保证项目中有且仅有一个连接池
private static synchronized void poolInit() {
if (null == pool) {
initializePool();
}
}
public 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++;
System.out.println("getJedis timeoutCount="+ timeoutCount);
if (timeoutCount > 3) {
break;
}
} else {
System.out.println("GetJedis error,"+e);
break;
}
}
break;
}
return null;
}
public static String getKey() {
String keys = pro.getProperty("redis.keys");
return keys;
}
public static Object getObject(String key) {
try (Jedis jedis = pool.getResource()) {
//Jedis jedis = new Jedis("127.0.0.1")
byte[] bytes = jedis.get(key.getBytes());
if (bytes != null) {
return SerializeUtil.unserialize(bytes);
}
} catch (Exception e) {
System.out.println("获取Redis键值getObject方法异常:key=" + key + " cause=" + e.getMessage());
}
return null;
}
public static String setObject(String key, Object value) {
try (Jedis jedis = pool.getResource()) {
//Jedis jedis = new Jedis("127.0.0.1")
//ShardedJedis jedis = pool.getResource()
return jedis.set(key.getBytes(), SerializeUtil.serialize(value));
} catch (Exception e) {
e.printStackTrace();
System.out.println("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
}
return null;
}
public static String hsetObject(String keys, String key, Object value) {
try (Jedis jedis = pool.getResource()) {
//Jedis jedis = new Jedis("127.0.0.1")
//ShardedJedis jedis = pool.getResource()
return jedis.set(keys.getBytes(), key.getBytes(), SerializeUtil.serialize(value));
} catch (Exception e) {
e.printStackTrace();
System.out.println("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
}
return null;
}
public static boolean hset(String keys, String field, String value) {
if (keys.isEmpty() || field.isEmpty()) {
return false;
}
Jedis jedis = pool.getResource();
jedis.get("redis.keys");
Long statusCode = jedis.hset(keys, field, value);
jedis.close();
if (statusCode > -1) {
return true;
}
return false;
}
public static String hget(String key, String field){
if(key.isEmpty()|| field.isEmpty()){
return null;
}
Jedis jedis = pool.getResource();
String value = jedis.hget(key, field);
jedis.close();
return value;
}
public static void closePool() throws Exception {
try {
pool.close();
} catch (Exception e) {
throw new Exception("释放Jedis资源异常:" + e.getMessage());
}
}
}