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: #219.
This commit is contained in:
@@ -56,6 +56,7 @@ import redis.clients.util.Pool;
|
|||||||
* @author Thomas Darimont
|
* @author Thomas Darimont
|
||||||
* @author Christoph Strobl
|
* @author Christoph Strobl
|
||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
|
* @author Fu Jian
|
||||||
*/
|
*/
|
||||||
public class JedisConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory {
|
public class JedisConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory {
|
||||||
|
|
||||||
@@ -94,6 +95,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
|
|||||||
private Pool<Jedis> pool;
|
private Pool<Jedis> pool;
|
||||||
private JedisPoolConfig poolConfig = new JedisPoolConfig();
|
private JedisPoolConfig poolConfig = new JedisPoolConfig();
|
||||||
private int dbIndex = 0;
|
private int dbIndex = 0;
|
||||||
|
private String clientName;
|
||||||
private boolean convertPipelineAndTxResults = true;
|
private boolean convertPipelineAndTxResults = true;
|
||||||
private RedisSentinelConfiguration sentinelConfig;
|
private RedisSentinelConfiguration sentinelConfig;
|
||||||
private RedisClusterConfiguration clusterConfig;
|
private RedisClusterConfiguration clusterConfig;
|
||||||
@@ -248,7 +250,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
|
|||||||
protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config) {
|
protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config) {
|
||||||
return new JedisSentinelPool(config.getMaster().getName(), convertToJedisSentinelSet(config.getSentinels()),
|
return new JedisSentinelPool(config.getMaster().getName(), convertToJedisSentinelSet(config.getSentinels()),
|
||||||
getPoolConfig() != null ? getPoolConfig() : new JedisPoolConfig(), getTimeoutFrom(getShardInfo()),
|
getPoolConfig() != null ? getPoolConfig() : new JedisPoolConfig(), getTimeoutFrom(getShardInfo()),
|
||||||
getShardInfo().getPassword());
|
getShardInfo().getPassword(), Protocol.DEFAULT_DATABASE, clientName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -260,7 +262,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
|
|||||||
protected Pool<Jedis> createRedisPool() {
|
protected Pool<Jedis> createRedisPool() {
|
||||||
|
|
||||||
return new JedisPool(getPoolConfig(), getShardInfo().getHost(), getShardInfo().getPort(),
|
return new JedisPool(getPoolConfig(), getShardInfo().getHost(), getShardInfo().getPort(),
|
||||||
getTimeoutFrom(getShardInfo()), getShardInfo().getPassword(), useSsl);
|
getTimeoutFrom(getShardInfo()), getShardInfo().getPassword(), Protocol.DEFAULT_DATABASE, clientName, useSsl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private JedisCluster createCluster() {
|
private JedisCluster createCluster() {
|
||||||
@@ -537,6 +539,24 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
|
|||||||
Assert.isTrue(index >= 0, "invalid DB index (a positive index required)");
|
Assert.isTrue(index >= 0, "invalid DB index (a positive index required)");
|
||||||
this.dbIndex = index;
|
this.dbIndex = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the client name.
|
||||||
|
*
|
||||||
|
* @return Returns the client name
|
||||||
|
*/
|
||||||
|
public String getClientName() {
|
||||||
|
return clientName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the client name used by this connection factory. Default is empty.
|
||||||
|
*
|
||||||
|
* @param clientName client name
|
||||||
|
*/
|
||||||
|
public void setClientName(String clientName) {
|
||||||
|
this.clientName = clientName;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specifies if pipelined results should be converted to the expected data type. If false, results of
|
* Specifies if pipelined results should be converted to the expected data type. If false, results of
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ public class JedisConnectionFactoryTests {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
factory = new JedisConnectionFactory(SENTINEL_CONFIG);
|
factory = new JedisConnectionFactory(SENTINEL_CONFIG);
|
||||||
|
factory.setClientName("clientName");
|
||||||
factory.afterPropertiesSet();
|
factory.afterPropertiesSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,4 +55,9 @@ public class JedisConnectionFactoryTests {
|
|||||||
public void shouldSendCommandCorrectlyViaConnectionFactoryUsingSentinel() {
|
public void shouldSendCommandCorrectlyViaConnectionFactoryUsingSentinel() {
|
||||||
assertThat(factory.getConnection().ping(), equalTo("PONG"));
|
assertThat(factory.getConnection().ping(), equalTo("PONG"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getClientNameShouldEqualWithFactorySetting() {
|
||||||
|
assertThat(factory.getConnection().getClientName(), equalTo("clientName"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user