Skip to content

Commit 4df1e1e

Browse files
committed
Add sentinel support. Fixes #24.
1 parent 344aa5a commit 4df1e1e

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

README.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ Add the following into your Tomcat context.xml (or the context block of the serv
5858
host="localhost" <!-- optional: defaults to "localhost" -->
5959
port="6379" <!-- optional: defaults to "6379" -->
6060
database="0" <!-- optional: defaults to "0" -->
61-
maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) --> />
61+
maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) -->
62+
sentinelMaster="SentinelMasterName" <!-- optional -->
63+
sentinels="sentinel-host-1:port,sentinel-host-2:port,.." <!-- optional --> />
6264

6365
The Valve must be declared before the Manager.
6466

src/main/java/com/radiadesign/catalina/session/RedisSessionManager.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import org.apache.catalina.Session;
1111
import org.apache.catalina.session.ManagerBase;
1212

13+
import redis.clients.util.Pool;
1314
import redis.clients.jedis.JedisPool;
15+
import redis.clients.jedis.JedisSentinelPool;
1416
import redis.clients.jedis.JedisPoolConfig;
1517
import redis.clients.jedis.Jedis;
1618
import redis.clients.jedis.Protocol;
@@ -20,6 +22,7 @@
2022
import java.util.Collections;
2123
import java.util.Enumeration;
2224
import java.util.Set;
25+
import java.util.HashSet;
2326

2427
import org.apache.juli.logging.Log;
2528
import org.apache.juli.logging.LogFactory;
@@ -36,7 +39,11 @@ public class RedisSessionManager extends ManagerBase implements Lifecycle {
3639
protected int database = 0;
3740
protected String password = null;
3841
protected int timeout = Protocol.DEFAULT_TIMEOUT;
39-
protected JedisPool connectionPool;
42+
protected String sentinelMaster = null;
43+
protected String sentinels = null;
44+
Set<String> sentinelSet = null;
45+
46+
protected Pool<Jedis> connectionPool;
4047

4148
protected RedisSessionHandlerValve handlerValve;
4249
protected ThreadLocal<RedisSession> currentSession = new ThreadLocal<>();
@@ -107,6 +114,33 @@ public void setSaveOnChange(boolean saveOnChange) {
107114
this.saveOnChange = saveOnChange;
108115
}
109116

117+
public String getSentinels() {
118+
return sentinels;
119+
}
120+
121+
public void setSentinels(String sentinels) {
122+
if (null == sentinels) {
123+
sentinels = "";
124+
}
125+
126+
String[] sentinelArray = getSentinels().split(",");
127+
this.sentinelSet = new HashSet<String>(Arrays.asList(sentinelArray));
128+
129+
this.sentinels = sentinels;
130+
}
131+
132+
public Set<String> getSentinelSet() {
133+
return this.sentinelSet;
134+
}
135+
136+
public String getSentinelMaster() {
137+
return this.sentinelMaster;
138+
}
139+
140+
public void setSentinelMaster(String master) {
141+
this.sentinelMaster = sentinelMaster;
142+
}
143+
110144
@Override
111145
public int getRejectedSessions() {
112146
// Essentially do nothing.
@@ -546,10 +580,19 @@ public void processExpires() {
546580
private void initializeDatabaseConnection() throws LifecycleException {
547581
try {
548582
// TODO: Allow configuration of pool (such as size...)
549-
connectionPool = new JedisPool(new JedisPoolConfig(), getHost(), getPort(), getTimeout(), getPassword());
583+
if (getSentinelMaster() != null) {
584+
Set<String> sentinelSet = getSentinelSet();
585+
if (sentinelSet != null && sentinelSet.size() > 0) {
586+
connectionPool = new JedisSentinelPool(getSentinelMaster(), sentinelSet, new JedisPoolConfig(), getTimeout(), getPassword());
587+
} else {
588+
throw new LifecycleException("Error configuring Redis Sentinel connection pool: expected both `sentinelMaster` and `sentiels` to be configured");
589+
}
590+
} else {
591+
connectionPool = new JedisPool(new JedisPoolConfig(), getHost(), getPort(), getTimeout(), getPassword());
592+
}
550593
} catch (Exception e) {
551594
e.printStackTrace();
552-
throw new LifecycleException("Error Connecting to Redis", e);
595+
throw new LifecycleException("Error connecting to Redis", e);
553596
}
554597
}
555598

0 commit comments

Comments
 (0)