JedisUtils.java
4.61 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*******************************************************************************
* Copyright (c) 2005, 2014 springside.github.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
*******************************************************************************/
package com.cjs.site.util.redis;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import com.cjs.site.util.file.ReadResourceUtils;
import com.cjs.site.util.redis.pool.ConnectionInfo;
import com.cjs.site.util.redis.pool.JedisPool;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;
import redis.clients.util.Pool;
/**
* Jedis工具类
*
* @author tongyufu
*
*/
public class JedisUtils {
public static final String DEFAULT_HOST = "localhost";
public static final int DEFAULT_PORT = Protocol.DEFAULT_PORT;
public static final int DEFAULT_TIMEOUT = Protocol.DEFAULT_TIMEOUT;
private static final String OK_CODE = "OK";
private static final String OK_MULTI_CODE = "+OK";
private static Pool<Jedis> jedisPool = null;
/**获取单例Pool*/
public synchronized static Pool<Jedis> getPool() {
if (jedisPool == null) {
Properties props = ReadResourceUtils.getPropertyFile("config.properties");
JedisPoolConfig config = JedisUtils.createPoolConfig(300, 1000, 300, 300);
String env = props.getProperty("env") + ".";
String host = props.getProperty(env + "redis.host");
int port = Integer.parseInt(props.getProperty(env + "redis.port", "6377"));
String password = props.getProperty(env + "redis.password");
ConnectionInfo conn = new ConnectionInfo(host, port);
if (StringUtils.isNotBlank(password)) {
conn.setPassword(password);
}
jedisPool = new JedisPool(conn, config);
}
return jedisPool;
}
/**获取单例Sentinel Pool*/
public synchronized static Pool<Jedis> getSentinelPool() {
if (jedisPool == null) {
Properties props = ReadResourceUtils.getPropertyFile("config.properties");
String env = props.getProperty("env") + ".";
JedisPoolConfig poolConfig = JedisUtils.createPoolConfig(300, 1000, 300, 300);
String masterName = props.getProperty(env + "redis.sentinel.masterName");
List<String> hosts = Arrays
.asList(props.getProperty(env + "redis.sentinel.host").split(","));
Set<String> sentinels = new HashSet<String>(hosts);
Integer timeout = Integer.valueOf(props.getProperty(env + "redis.sentinel.timeout"));
jedisPool = new redis.clients.jedis.JedisSentinelPool(masterName, sentinels, poolConfig,
timeout);
}
return jedisPool;
}
/**
* 快速设置JedisPoolConfig, 不执行idle checking。
*/
public static JedisPoolConfig createPoolConfig(int maxIdle, int maxTotal) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMaxTotal(maxTotal);
poolConfig.setTimeBetweenEvictionRunsMillis(-1);
return poolConfig;
}
/**
* 快速设置JedisPoolConfig, 设置执行idle checking的间隔和可被清除的idle时间.
* 默认的checkingIntervalSecs是30秒,可被清除时间是60秒。
*/
public static JedisPoolConfig createPoolConfig(int maxIdle, int maxTotal,
int checkingIntervalSecs,
int evictableIdleTimeSecs) {
JedisPoolConfig poolConfig = createPoolConfig(maxIdle, maxTotal);
poolConfig.setTimeBetweenEvictionRunsMillis(checkingIntervalSecs * 1000);
poolConfig.setMinEvictableIdleTimeMillis(evictableIdleTimeSecs * 1000);
return poolConfig;
}
/**
* 判断 是 OK 或 +OK.
*/
public static boolean isStatusOk(String status) {
return (status != null) && (OK_CODE.equals(status) || OK_MULTI_CODE.equals(status));
}
/**
* 退出然后关闭Jedis连接。如果Jedis为null则无动作。
*/
public static void closeJedis(Jedis jedis) {
if ((jedis != null) && jedis.isConnected()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {
}
}
}
}