Skip to content

Commit 9117352

Browse files
jiafu1115mp911de
authored andcommitted
DATAREDIS-552 - Support client name configuration for JedisConnectionFactory.
Motivation: JedisConnectionFactory should support setting clientName for Jedis connections so to improve troubleshooting. Result: Easier to troubleshooting with connection name. Original pull request: spring-projects#219.
1 parent 4bab94b commit 9117352

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnectionFactory.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
* @author Thomas Darimont
6464
* @author Christoph Strobl
6565
* @author Mark Paluch
66+
* @author Fu Jian
6667
*/
6768
public class JedisConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory {
6869

@@ -101,6 +102,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
101102
private Pool<Jedis> pool;
102103
private JedisPoolConfig poolConfig = new JedisPoolConfig();
103104
private int dbIndex = 0;
105+
private String clientName;
104106
private boolean convertPipelineAndTxResults = true;
105107
private RedisSentinelConfiguration sentinelConfig;
106108
private RedisClusterConfiguration clusterConfig;
@@ -255,7 +257,7 @@ private Pool<Jedis> createPool() {
255257
protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config) {
256258
return new JedisSentinelPool(config.getMaster().getName(), convertToJedisSentinelSet(config.getSentinels()),
257259
getPoolConfig() != null ? getPoolConfig() : new JedisPoolConfig(), getTimeoutFrom(getShardInfo()),
258-
getShardInfo().getPassword());
260+
getShardInfo().getPassword(), Protocol.DEFAULT_DATABASE, clientName);
259261
}
260262

261263
/**
@@ -267,7 +269,7 @@ protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config)
267269
protected Pool<Jedis> createRedisPool() {
268270

269271
return new JedisPool(getPoolConfig(), getShardInfo().getHost(), getShardInfo().getPort(),
270-
getTimeoutFrom(getShardInfo()), getShardInfo().getPassword(), useSsl);
272+
getTimeoutFrom(getShardInfo()), getShardInfo().getPassword(), Protocol.DEFAULT_DATABASE, clientName, useSsl);
271273
}
272274

273275
private JedisCluster createCluster() {
@@ -530,6 +532,24 @@ public void setDatabase(int index) {
530532
Assert.isTrue(index >= 0, "invalid DB index (a positive index required)");
531533
this.dbIndex = index;
532534
}
535+
536+
/**
537+
* Returns the client name.
538+
*
539+
* @return Returns the client name
540+
*/
541+
public String getClientName() {
542+
return clientName;
543+
}
544+
545+
/**
546+
* Sets the client name used by this connection factory. Default is empty.
547+
*
548+
* @param clientName client name
549+
*/
550+
public void setClientName(String clientName) {
551+
this.clientName = clientName;
552+
}
533553

534554
/**
535555
* Specifies if pipelined results should be converted to the expected data type. If false, results of

src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionFactoryTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class JedisConnectionFactoryTests {
3939
@Before
4040
public void setUp() {
4141
factory = new JedisConnectionFactory(SENTINEL_CONFIG);
42+
factory.setClientName("clientName");
4243
factory.afterPropertiesSet();
4344
}
4445

@@ -54,4 +55,9 @@ public void tearDown() {
5455
public void shouldSendCommandCorrectlyViaConnectionFactoryUsingSentinel() {
5556
assertThat(factory.getConnection().ping(), equalTo("PONG"));
5657
}
58+
59+
@Test
60+
public void getClientNameShouldEqualWithFactorySetting() {
61+
assertThat(factory.getConnection().getClientName(), equalTo("clientName"));
62+
}
5763
}

0 commit comments

Comments
 (0)