11package redis .clients .jedis ;
22
3+ import java .util .ArrayList ;
34import java .util .Arrays ;
4- import java .util .HashSet ;
5+ import java .util .Collection ;
56import java .util .List ;
67import java .util .Set ;
78import java .util .concurrent .atomic .AtomicBoolean ;
1617
1718public class JedisSentinelPool extends JedisPoolAbstract {
1819
19- /**
20- * @deprecated This will be private in future.
21- */
22- @ Deprecated
23- protected static Logger log = LoggerFactory .getLogger (JedisSentinelPool .class );
20+ private static final Logger LOG = LoggerFactory .getLogger (JedisSentinelPool .class );
2421
25- @ Deprecated protected final GenericObjectPoolConfig <Jedis > poolConfig ;
2622 private final JedisFactory factory ;
2723
28- @ Deprecated protected int connectionTimeout ;
29- @ Deprecated protected int soTimeout ;
30- @ Deprecated protected int infiniteSoTimeout ;
31-
32- @ Deprecated protected String user ;
33- @ Deprecated protected String password ;
34- @ Deprecated protected int database ;
35- @ Deprecated protected String clientName ;
36-
37- @ Deprecated protected int sentinelConnectionTimeout ;
38- @ Deprecated protected int sentinelSoTimeout ;
39- @ Deprecated protected String sentinelUser ;
40- @ Deprecated protected String sentinelPassword ;
41- @ Deprecated protected String sentinelClientName ;
42-
4324 private final JedisClientConfig sentinelClientConfig ;
4425
45- protected final Set <MasterListener > masterListeners = new HashSet <>();
26+ protected final Collection <MasterListener > masterListeners = new ArrayList <>();
4627
4728 private volatile HostAndPort currentHostMaster ;
4829
@@ -163,19 +144,14 @@ public JedisSentinelPool(String masterName, Set<String> sentinels,
163144 final String user , final String password , final int database , final String clientName ,
164145 final int sentinelConnectionTimeout , final int sentinelSoTimeout , final String sentinelUser ,
165146 final String sentinelPassword , final String sentinelClientName ) {
166- this (masterName , sentinels , poolConfig , new JedisFactory (connectionTimeout , soTimeout , infiniteSoTimeout , user , password , database , clientName ));
167- this .connectionTimeout = connectionTimeout ;
168- this .soTimeout = soTimeout ;
169- this .infiniteSoTimeout = infiniteSoTimeout ;
170- this .user = user ;
171- this .password = password ;
172- this .database = database ;
173- this .clientName = clientName ;
174- this .sentinelConnectionTimeout = sentinelConnectionTimeout ;
175- this .sentinelSoTimeout = sentinelSoTimeout ;
176- this .sentinelUser = sentinelUser ;
177- this .sentinelPassword = sentinelPassword ;
178- this .sentinelClientName = sentinelClientName ;
147+ this (masterName , parseHostAndPorts (sentinels ), poolConfig ,
148+ DefaultJedisClientConfig .builder ().connectionTimeoutMillis (connectionTimeout )
149+ .socketTimeoutMillis (soTimeout ).blockingSocketTimeoutMillis (infiniteSoTimeout )
150+ .user (user ).password (password ).database (database ).clientName (clientName ).build (),
151+ DefaultJedisClientConfig .builder ().connectionTimeoutMillis (sentinelConnectionTimeout )
152+ .socketTimeoutMillis (sentinelSoTimeout ).user (sentinelUser ).password (sentinelPassword )
153+ .clientName (sentinelClientName ).build ()
154+ );
179155 }
180156
181157 public JedisSentinelPool (String masterName , Set <String > sentinels ,
@@ -195,7 +171,6 @@ public JedisSentinelPool(String masterName, Set<HostAndPort> sentinels,
195171 final JedisClientConfig sentinelClientConfig ) {
196172 super (poolConfig , factory );
197173
198- this .poolConfig = poolConfig ;
199174 this .factory = factory ;
200175 this .sentinelClientConfig = sentinelClientConfig ;
201176
@@ -229,7 +204,7 @@ private void initMaster(HostAndPort master) {
229204 // this call only clears idle instances, not borrowed instances
230205 clearInternalPool ();
231206
232- log .info ("Created JedisSentinelPool to master at {}" , master );
207+ LOG .info ("Created JedisSentinelPool to master at {}" , master );
233208 }
234209 }
235210 }
@@ -239,11 +214,11 @@ private HostAndPort initSentinels(Set<HostAndPort> sentinels, final String maste
239214 HostAndPort master = null ;
240215 boolean sentinelAvailable = false ;
241216
242- log .info ("Trying to find master from available Sentinels..." );
217+ LOG .info ("Trying to find master from available Sentinels..." );
243218
244219 for (HostAndPort sentinel : sentinels ) {
245220
246- log .debug ("Connecting to Sentinel {}" , sentinel );
221+ LOG .debug ("Connecting to Sentinel {}" , sentinel );
247222
248223 try (Jedis jedis = new Jedis (sentinel , sentinelClientConfig )) {
249224
@@ -253,19 +228,18 @@ private HostAndPort initSentinels(Set<HostAndPort> sentinels, final String maste
253228 sentinelAvailable = true ;
254229
255230 if (masterAddr == null || masterAddr .size () != 2 ) {
256- log .warn ("Can not get master addr, master name: {}. Sentinel: {}" , masterName , sentinel );
231+ LOG .warn ("Can not get master addr, master name: {}. Sentinel: {}" , masterName , sentinel );
257232 continue ;
258233 }
259234
260235 master = toHostAndPort (masterAddr );
261- log .debug ("Found Redis master at {}" , master );
236+ LOG .debug ("Found Redis master at {}" , master );
262237 break ;
263238 } catch (JedisException e ) {
264239 // resolves #1036, it should handle JedisException there's another chance
265240 // of raising JedisDataException
266- log .warn (
267- "Cannot get master address from sentinel running @ {}. Reason: {}. Trying next one." ,
268- sentinel , e );
241+ LOG .warn (
242+ "Cannot get master address from sentinel running @ {}. Reason: {}. Trying next one." , sentinel , e );
269243 }
270244 }
271245
@@ -280,7 +254,7 @@ private HostAndPort initSentinels(Set<HostAndPort> sentinels, final String maste
280254 }
281255 }
282256
283- log .info ("Redis master running at {}, starting Sentinel listeners..." , master );
257+ LOG .info ("Redis master running at {}, starting Sentinel listeners..." , master );
284258
285259 for (HostAndPort sentinel : sentinels ) {
286260
@@ -329,7 +303,7 @@ public void returnResource(final Jedis resource) {
329303 returnResourceObject (resource );
330304 } catch (Exception e ) {
331305 returnBrokenResource (resource );
332- log .debug ("Resource is returned to the pool as broken" , e );
306+ LOG .debug ("Resource is returned to the pool as broken" , e );
333307 }
334308 }
335309 }
@@ -378,7 +352,7 @@ public void run() {
378352 // code for active refresh
379353 List <String > masterAddr = j .sentinelGetMasterAddrByName (masterName );
380354 if (masterAddr == null || masterAddr .size () != 2 ) {
381- log .warn ("Can not get master addr, master name: {}. Sentinel: {}." , masterName ,
355+ LOG .warn ("Can not get master addr, master name: {}. Sentinel: {}." , masterName ,
382356 hostPort );
383357 } else {
384358 initMaster (toHostAndPort (masterAddr ));
@@ -387,7 +361,7 @@ public void run() {
387361 j .subscribe (new JedisPubSub () {
388362 @ Override
389363 public void onMessage (String channel , String message ) {
390- log .debug ("Sentinel {} published: {}." , hostPort , message );
364+ LOG .debug ("Sentinel {} published: {}." , hostPort , message );
391365
392366 String [] switchMasterMsg = message .split (" " );
393367
@@ -396,13 +370,13 @@ public void onMessage(String channel, String message) {
396370 if (masterName .equals (switchMasterMsg [0 ])) {
397371 initMaster (toHostAndPort (Arrays .asList (switchMasterMsg [3 ], switchMasterMsg [4 ])));
398372 } else {
399- log .debug (
373+ LOG .debug (
400374 "Ignoring message on +switch-master for master name {}, our master name is {}" ,
401375 switchMasterMsg [0 ], masterName );
402376 }
403377
404378 } else {
405- log .error ("Invalid message received on Sentinel {} on channel +switch-master: {}" ,
379+ LOG .error ("Invalid message received on Sentinel {} on channel +switch-master: {}" ,
406380 hostPort , message );
407381 }
408382 }
@@ -411,15 +385,15 @@ public void onMessage(String channel, String message) {
411385 } catch (JedisException e ) {
412386
413387 if (running .get ()) {
414- log .error ("Lost connection to Sentinel at {}:{}. Sleeping 5000ms and retrying." , host ,
388+ LOG .error ("Lost connection to Sentinel at {}:{}. Sleeping 5000ms and retrying." , host ,
415389 port , e );
416390 try {
417391 Thread .sleep (subscribeRetryWaitTimeMillis );
418392 } catch (InterruptedException e1 ) {
419- log .error ("Sleep interrupted: " , e1 );
393+ LOG .error ("Sleep interrupted: " , e1 );
420394 }
421395 } else {
422- log .debug ("Unsubscribing from Sentinel at {}:{}" , host , port );
396+ LOG .debug ("Unsubscribing from Sentinel at {}:{}" , host , port );
423397 }
424398 } finally {
425399 if (j != null ) {
@@ -431,14 +405,14 @@ public void onMessage(String channel, String message) {
431405
432406 public void shutdown () {
433407 try {
434- log .debug ("Shutting down listener on {}:{}" , host , port );
408+ LOG .debug ("Shutting down listener on {}:{}" , host , port );
435409 running .set (false );
436410 // This isn't good, the Jedis object is not thread safe
437411 if (j != null ) {
438412 j .close ();
439413 }
440414 } catch (Exception e ) {
441- log .error ("Caught exception while shutting down: " , e );
415+ LOG .error ("Caught exception while shutting down: " , e );
442416 }
443417 }
444418 }
0 commit comments