Allow JedisPool to set a custom client name
This commit is contained in:
committed by
Hisham Mardam-Bey
parent
bc4d0ed0b6
commit
796f80d9c0
@@ -11,15 +11,21 @@ class JedisFactory extends BasePoolableObjectFactory {
|
|||||||
private final int timeout;
|
private final int timeout;
|
||||||
private final String password;
|
private final String password;
|
||||||
private final int database;
|
private final int database;
|
||||||
|
private final String clientName;
|
||||||
|
|
||||||
public JedisFactory(final String host, final int port,
|
public JedisFactory(final String host, final int port,
|
||||||
final int timeout, final String password, final int database) {
|
final int timeout, final String password, final int database) {
|
||||||
|
this(host, port, timeout, password, database, null);
|
||||||
|
}
|
||||||
|
public JedisFactory(final String host, final int port,
|
||||||
|
final int timeout, final String password, final int database, final String clientName) {
|
||||||
super();
|
super();
|
||||||
this.host = host;
|
this.host = host;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.database = database;
|
this.database = database;
|
||||||
|
this.clientName = clientName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object makeObject() throws Exception {
|
public Object makeObject() throws Exception {
|
||||||
@@ -32,6 +38,9 @@ class JedisFactory extends BasePoolableObjectFactory {
|
|||||||
if( database != 0 ) {
|
if( database != 0 ) {
|
||||||
jedis.select(database);
|
jedis.select(database);
|
||||||
}
|
}
|
||||||
|
if ( clientName != null ) {
|
||||||
|
jedis.clientSetname(clientName);
|
||||||
|
}
|
||||||
|
|
||||||
return jedis;
|
return jedis;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ import redis.clients.util.Pool;
|
|||||||
public class JedisPool extends Pool<Jedis> {
|
public class JedisPool extends Pool<Jedis> {
|
||||||
|
|
||||||
public JedisPool(final Config poolConfig, final String host) {
|
public JedisPool(final Config poolConfig, final String host) {
|
||||||
this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
|
this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JedisPool(String host, int port) {
|
public JedisPool(String host, int port) {
|
||||||
this(new Config(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
|
this(new Config(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JedisPool(final String host) {
|
public JedisPool(final String host) {
|
||||||
@@ -25,11 +25,11 @@ public class JedisPool extends Pool<Jedis> {
|
|||||||
String password = uri.getUserInfo().split(":", 2)[1];
|
String password = uri.getUserInfo().split(":", 2)[1];
|
||||||
int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
|
int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
|
||||||
this.internalPool = new GenericObjectPool(new JedisFactory(h, port,
|
this.internalPool = new GenericObjectPool(new JedisFactory(h, port,
|
||||||
Protocol.DEFAULT_TIMEOUT, password, database), new Config());
|
Protocol.DEFAULT_TIMEOUT, password, database, null), new Config());
|
||||||
} else {
|
} else {
|
||||||
this.internalPool = new GenericObjectPool(new JedisFactory(host,
|
this.internalPool = new GenericObjectPool(new JedisFactory(host,
|
||||||
Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null,
|
Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null,
|
||||||
Protocol.DEFAULT_DATABASE), new Config());
|
Protocol.DEFAULT_DATABASE, null), new Config());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,25 +39,30 @@ public class JedisPool extends Pool<Jedis> {
|
|||||||
String password = uri.getUserInfo().split(":", 2)[1];
|
String password = uri.getUserInfo().split(":", 2)[1];
|
||||||
int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
|
int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
|
||||||
this.internalPool = new GenericObjectPool(new JedisFactory(h, port,
|
this.internalPool = new GenericObjectPool(new JedisFactory(h, port,
|
||||||
Protocol.DEFAULT_TIMEOUT, password, database), new Config());
|
Protocol.DEFAULT_TIMEOUT, password, database, null), new Config());
|
||||||
}
|
}
|
||||||
|
|
||||||
public JedisPool(final Config poolConfig, final String host, int port,
|
public JedisPool(final Config poolConfig, final String host, int port,
|
||||||
int timeout, final String password) {
|
int timeout, final String password) {
|
||||||
this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE);
|
this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JedisPool(final Config poolConfig, final String host, final int port) {
|
public JedisPool(final Config poolConfig, final String host, final int port) {
|
||||||
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
|
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JedisPool(final Config poolConfig, final String host, final int port, final int timeout) {
|
public JedisPool(final Config poolConfig, final String host, final int port, final int timeout) {
|
||||||
this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE);
|
this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password,
|
public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password,
|
||||||
final int database) {
|
final int database) {
|
||||||
super(poolConfig, new JedisFactory(host, port, timeout, password, database));
|
this(poolConfig, host, port, timeout, password, database, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password,
|
||||||
|
final int database, final String clientName) {
|
||||||
|
super(poolConfig, new JedisFactory(host, port, timeout, password, database, clientName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -170,4 +170,18 @@ public class JedisPoolTest extends Assert {
|
|||||||
pool.returnResource(jedis1);
|
pool.returnResource(jedis1);
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void customClientName() {
|
||||||
|
JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.host,
|
||||||
|
hnp.port, 2000, "foobared", 0, "my_shiny_client_name");
|
||||||
|
|
||||||
|
Jedis jedis = pool0.getResource();
|
||||||
|
|
||||||
|
assertEquals("my_shiny_client_name", jedis.clientGetname());
|
||||||
|
|
||||||
|
pool0.returnResource(jedis);
|
||||||
|
pool0.destroy();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user