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 String password;
|
||||
private final int database;
|
||||
private final String clientName;
|
||||
|
||||
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();
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
this.password = password;
|
||||
this.database = database;
|
||||
this.clientName = clientName;
|
||||
}
|
||||
|
||||
public Object makeObject() throws Exception {
|
||||
@@ -32,10 +38,13 @@ class JedisFactory extends BasePoolableObjectFactory {
|
||||
if( database != 0 ) {
|
||||
jedis.select(database);
|
||||
}
|
||||
|
||||
if ( clientName != null ) {
|
||||
jedis.clientSetname(clientName);
|
||||
}
|
||||
|
||||
return jedis;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void activateObject(Object obj) throws Exception {
|
||||
if (obj instanceof Jedis) {
|
||||
|
||||
@@ -10,11 +10,11 @@ import redis.clients.util.Pool;
|
||||
public class JedisPool extends Pool<Jedis> {
|
||||
|
||||
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) {
|
||||
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) {
|
||||
@@ -25,11 +25,11 @@ public class JedisPool extends Pool<Jedis> {
|
||||
String password = uri.getUserInfo().split(":", 2)[1];
|
||||
int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
|
||||
this.internalPool = new GenericObjectPool(new JedisFactory(h, port,
|
||||
Protocol.DEFAULT_TIMEOUT, password, database), new Config());
|
||||
Protocol.DEFAULT_TIMEOUT, password, database, null), new Config());
|
||||
} else {
|
||||
this.internalPool = new GenericObjectPool(new JedisFactory(host,
|
||||
Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null,
|
||||
Protocol.DEFAULT_DATABASE), new Config());
|
||||
Protocol.DEFAULT_DATABASE, null), new Config());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,33 +39,38 @@ public class JedisPool extends Pool<Jedis> {
|
||||
String password = uri.getUserInfo().split(":", 2)[1];
|
||||
int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
|
||||
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,
|
||||
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) {
|
||||
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) {
|
||||
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,
|
||||
final int database) {
|
||||
super(poolConfig, new JedisFactory(host, port, timeout, password, database));
|
||||
final int 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));
|
||||
}
|
||||
|
||||
|
||||
public void returnBrokenResource(final BinaryJedis resource) {
|
||||
returnBrokenResourceObject(resource);
|
||||
}
|
||||
|
||||
|
||||
public void returnResource(final BinaryJedis resource) {
|
||||
returnResourceObject(resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user