1010import org .apache .catalina .Session ;
1111import org .apache .catalina .session .ManagerBase ;
1212
13+ import redis .clients .util .Pool ;
1314import redis .clients .jedis .JedisPool ;
15+ import redis .clients .jedis .JedisSentinelPool ;
1416import redis .clients .jedis .JedisPoolConfig ;
1517import redis .clients .jedis .Jedis ;
1618import redis .clients .jedis .Protocol ;
2022import java .util .Collections ;
2123import java .util .Enumeration ;
2224import java .util .Set ;
25+ import java .util .HashSet ;
2326
2427import org .apache .juli .logging .Log ;
2528import 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