1010public abstract class RedisHandler {
1111 private final JedisPool publishPool ;
1212 private final JedisPool subscribePool ;
13-
13+
1414 private final Map <RedisListener , Thread > listenerThreads = new ConcurrentHashMap <>();
1515
16+ /**
17+ * Old constructor — defaults DB index to 0
18+ */
1619 public RedisHandler (String host , int port , String username , String password ) {
17- int timeout = 2000 ; // Set a reasonable timeout
20+ this (host , port , username , password , 0 );
21+ }
22+
23+ /**
24+ * New constructor with DB index
25+ */
26+ public RedisHandler (String host , int port , String username , String password , int dbIndex ) {
27+ int timeout = 2000 ;
28+
29+ JedisPoolConfig config = new JedisPoolConfig ();
30+
1831 if (username .isEmpty () && password .isEmpty ()) {
19- publishPool = new JedisPool (new JedisPoolConfig (), host , port , timeout );
20- subscribePool = new JedisPool (new JedisPoolConfig (), host , port , timeout );
32+ // No auth
33+ publishPool = new JedisPool (config , host , port , timeout , null , dbIndex );
34+ subscribePool = new JedisPool (config , host , port , timeout , null , dbIndex );
2135 } else if (username .isEmpty ()) {
22- publishPool = new JedisPool (new JedisPoolConfig (), host , port , timeout , password );
23- subscribePool = new JedisPool (new JedisPoolConfig (), host , port , timeout , password );
36+ // Legacy auth (password only)
37+ publishPool = new JedisPool (config , host , port , timeout , password , dbIndex );
38+ subscribePool = new JedisPool (config , host , port , timeout , password , dbIndex );
2439 } else {
25- publishPool = new JedisPool (new JedisPoolConfig (), host , port , timeout , username , password );
26- subscribePool = new JedisPool (new JedisPoolConfig (), host , port , timeout , username , password );
40+ // Username + password
41+ publishPool = new JedisPool (config , host , port , timeout , username , password , dbIndex );
42+ subscribePool = new JedisPool (config , host , port , timeout , username , password , dbIndex );
2743 }
2844 }
2945
3046 public void close () {
3147 debug ("Shutting down RedisHandler" );
3248
33- // Unsubscribe all listeners and stop threads
3449 for (Map .Entry <RedisListener , Thread > entry : listenerThreads .entrySet ()) {
3550 RedisListener listener = entry .getKey ();
3651 Thread thread = entry .getValue ();
3752
3853 try {
3954 debug ("Unsubscribing Redis listener on channel: " + listener .getChannel ());
40- listener .unsubscribe (); // Gracefully signal jedis.subscribe() to exit
55+ listener .unsubscribe ();
4156 } catch (Exception e ) {
4257 debug ("Failed to unsubscribe listener: " + e .getMessage ());
4358 }
4459
4560 try {
4661 if (thread != null && thread .isAlive ()) {
47- thread .join (2000 ); // Give up to 2 seconds for it to shut down
62+ thread .join (2000 );
4863 }
4964 } catch (InterruptedException e ) {
5065 debug ("Interrupted while waiting for Redis listener thread to finish: " + e .getMessage ());
@@ -53,7 +68,6 @@ public void close() {
5368
5469 listenerThreads .clear ();
5570
56- // Close Redis pools
5771 publishPool .close ();
5872 subscribePool .close ();
5973 }
0 commit comments