@@ -193,9 +193,12 @@ protected Jedis fetchJedisConnector() {
193193if (usePool && pool != null ) {
194194return pool .getResource ();
195195}
196+
196197Jedis jedis = new Jedis (getShardInfo ());
197198// force initialization (see Jedis issue #82)
198199jedis .connect ();
200+
201+ potentiallySetClientName (jedis );
199202return jedis ;
200203} catch (Exception ex ) {
201204throw new RedisConnectionFailureException ("Cannot get Jedis connection" , ex );
@@ -343,8 +346,8 @@ public RedisConnection getConnection() {
343346}
344347
345348Jedis jedis = fetchJedisConnector ();
346- JedisConnection connection = (usePool ? new JedisConnection (jedis , pool , dbIndex )
347- : new JedisConnection (jedis , null , dbIndex ));
349+ JedisConnection connection = (usePool ? new JedisConnection (jedis , pool , dbIndex , clientName )
350+ : new JedisConnection (jedis , null , dbIndex , clientName ));
348351connection .setConvertPipelineAndTxResults (convertPipelineAndTxResults );
349352return postProcessConnection (connection );
350353}
@@ -373,7 +376,7 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
373376/**
374377 * Returns the Redis hostName.
375378 *
376- * @return Returns the hostName
379+ * @return the hostName.
377380 */
378381public String getHostName () {
379382return hostName ;
@@ -382,7 +385,7 @@ public String getHostName() {
382385/**
383386 * Sets the Redis hostName.
384387 *
385- * @param hostName The hostName to set.
388+ * @param hostName the hostName to set.
386389 */
387390public void setHostName (String hostName ) {
388391this .hostName = hostName ;
@@ -401,7 +404,7 @@ public void setUseSsl(boolean useSsl) {
401404/**
402405 * Returns whether to use SSL.
403406 *
404- * @return use of SSL
407+ * @return use of SSL.
405408 * @since 1.8
406409 */
407410public boolean isUseSsl () {
@@ -411,7 +414,7 @@ public boolean isUseSsl() {
411414/**
412415 * Returns the password used for authenticating with the Redis server.
413416 *
414- * @return password for authentication
417+ * @return password for authentication.
415418 */
416419public String getPassword () {
417420return password ;
@@ -420,7 +423,7 @@ public String getPassword() {
420423/**
421424 * Sets the password used for authenticating with the Redis server.
422425 *
423- * @param password the password to set
426+ * @param password the password to set.
424427 */
425428public void setPassword (String password ) {
426429this .password = password ;
@@ -429,7 +432,7 @@ public void setPassword(String password) {
429432/**
430433 * Returns the port used to connect to the Redis instance.
431434 *
432- * @return Redis port.
435+ * @return the Redis port.
433436 */
434437public int getPort () {
435438return port ;
@@ -438,7 +441,7 @@ public int getPort() {
438441/**
439442 * Sets the port used to connect to the Redis instance.
440443 *
441- * @param port Redis port
444+ * @param port the Redis port.
442445 */
443446public void setPort (int port ) {
444447this .port = port ;
@@ -447,7 +450,7 @@ public void setPort(int port) {
447450/**
448451 * Returns the shardInfo.
449452 *
450- * @return Returns the shardInfo
453+ * @return the shardInfo.
451454 */
452455public JedisShardInfo getShardInfo () {
453456return shardInfo ;
@@ -456,7 +459,7 @@ public JedisShardInfo getShardInfo() {
456459/**
457460 * Sets the shard info for this factory.
458461 *
459- * @param shardInfo The shardInfo to set.
462+ * @param shardInfo the shardInfo to set.
460463 */
461464public void setShardInfo (JedisShardInfo shardInfo ) {
462465this .shardInfo = shardInfo ;
@@ -465,14 +468,16 @@ public void setShardInfo(JedisShardInfo shardInfo) {
465468/**
466469 * Returns the timeout.
467470 *
468- * @return Returns the timeout
471+ * @return the timeout.
469472 */
470473public int getTimeout () {
471474return timeout ;
472475}
473476
474477/**
475- * @param timeout The timeout to set.
478+ * Sets the timeout.
479+ *
480+ * @param timeout the timeout to set.
476481 */
477482public void setTimeout (int timeout ) {
478483this .timeout = timeout ;
@@ -481,7 +486,7 @@ public void setTimeout(int timeout) {
481486/**
482487 * Indicates the use of a connection pool.
483488 *
484- * @return Returns the use of connection pooling.
489+ * @return the use of connection pooling.
485490 */
486491public boolean getUsePool () {
487492return usePool ;
@@ -490,7 +495,7 @@ public boolean getUsePool() {
490495/**
491496 * Turns on or off the use of connection pooling.
492497 *
493- * @param usePool The usePool to set.
498+ * @param usePool the usePool to set.
494499 */
495500public void setUsePool (boolean usePool ) {
496501this .usePool = usePool ;
@@ -499,7 +504,7 @@ public void setUsePool(boolean usePool) {
499504/**
500505 * Returns the poolConfig.
501506 *
502- * @return Returns the poolConfig
507+ * @return the poolConfig
503508 */
504509public JedisPoolConfig getPoolConfig () {
505510return poolConfig ;
@@ -508,7 +513,7 @@ public JedisPoolConfig getPoolConfig() {
508513/**
509514 * Sets the pool configuration for this factory.
510515 *
511- * @param poolConfig The poolConfig to set.
516+ * @param poolConfig the poolConfig to set.
512517 */
513518public void setPoolConfig (JedisPoolConfig poolConfig ) {
514519this .poolConfig = poolConfig ;
@@ -517,7 +522,7 @@ public void setPoolConfig(JedisPoolConfig poolConfig) {
517522/**
518523 * Returns the index of the database.
519524 *
520- * @return Returns the database index
525+ * @return the database index.
521526 */
522527public int getDatabase () {
523528return dbIndex ;
@@ -526,26 +531,28 @@ public int getDatabase() {
526531/**
527532 * Sets the index of the database used by this connection factory. Default is 0.
528533 *
529- * @param index database index
534+ * @param index database index.
530535 */
531536public void setDatabase (int index ) {
532537Assert .isTrue (index >= 0 , "invalid DB index (a positive index required)" );
533538this .dbIndex = index ;
534539}
535-
540+
536541/**
537542 * Returns the client name.
538- *
539- * @return Returns the client name
543+ *
544+ * @return the client name.
545+ * @since 1.8
540546 */
541547public String getClientName () {
542548return clientName ;
543549}
544550
545551/**
546- * Sets the client name used by this connection factory. Default is empty.
547- *
548- * @param clientName client name
552+ * Sets the client name used by this connection factory. Defaults to none which does not set a client name.
553+ *
554+ * @param clientName the client name.
555+ * @since 1.8
549556 */
550557public void setClientName (String clientName ) {
551558this .clientName = clientName ;
@@ -554,9 +561,9 @@ public void setClientName(String clientName) {
554561/**
555562 * Specifies if pipelined results should be converted to the expected data type. If false, results of
556563 * {@link JedisConnection#closePipeline()} and {@link JedisConnection#exec()} will be of the type returned by the
557- * Jedis driver
564+ * Jedis driver.
558565 *
559- * @return Whether or not to convert pipeline and tx results
566+ * @return Whether or not to convert pipeline and tx results.
560567 */
561568public boolean getConvertPipelineAndTxResults () {
562569return convertPipelineAndTxResults ;
@@ -565,9 +572,9 @@ public boolean getConvertPipelineAndTxResults() {
565572/**
566573 * Specifies if pipelined results should be converted to the expected data type. If false, results of
567574 * {@link JedisConnection#closePipeline()} and {@link JedisConnection#exec()} will be of the type returned by the
568- * Jedis driver
575+ * Jedis driver.
569576 *
570- * @param convertPipelineAndTxResults Whether or not to convert pipeline and tx results
577+ * @param convertPipelineAndTxResults Whether or not to convert pipeline and tx results.
571578 */
572579public void setConvertPipelineAndTxResults (boolean convertPipelineAndTxResults ) {
573580this .convertPipelineAndTxResults = convertPipelineAndTxResults ;
@@ -597,14 +604,19 @@ public RedisSentinelConnection getSentinelConnection() {
597604private Jedis getActiveSentinel () {
598605
599606Assert .notNull (this .sentinelConfig );
607+
600608for (RedisNode node : this .sentinelConfig .getSentinels ()) {
609+
601610Jedis jedis = new Jedis (node .getHost (), node .getPort ());
611+
602612if (jedis .ping ().equalsIgnoreCase ("pong" )) {
613+
614+ potentiallySetClientName (jedis );
603615return jedis ;
604616}
605617}
606618
607- throw new InvalidDataAccessResourceUsageException ("no sentinel found" );
619+ throw new InvalidDataAccessResourceUsageException ("No Sentinel found" );
608620}
609621
610622private Set <String > convertToJedisSentinelSet (Collection <RedisNode > nodes ) {
@@ -622,6 +634,13 @@ private Set<String> convertToJedisSentinelSet(Collection<RedisNode> nodes) {
622634return convertedNodes ;
623635}
624636
637+ private void potentiallySetClientName (Jedis jedis ) {
638+
639+ if (StringUtils .hasText (clientName )) {
640+ jedis .clientSetname (clientName );
641+ }
642+ }
643+
625644private void setTimeoutOn (JedisShardInfo shardInfo , int timeout ) {
626645ReflectionUtils .invokeMethod (SET_TIMEOUT_METHOD , shardInfo , timeout );
627646}
0 commit comments