upgrade to commons-pool 2

This commit is contained in:
Jonathan Leibiusky
2013-11-28 08:13:57 -05:00
parent 597366343d
commit bbb867781d
19 changed files with 901 additions and 995 deletions

View File

@@ -1,8 +1,6 @@
package redis.clients.jedis;
import java.util.List;
/**
* Pipelined responses for all of the low level, non key related commands
*/

View File

@@ -5,8 +5,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
/**
* Common interface for sharded and non-sharded BinaryJedis
*/
@@ -116,7 +114,7 @@ public interface BinaryJedisCommands {
Long strlen(byte[] key);
Long zadd(byte[] key, double score, byte[] member);
Long zadd(byte[] key, Map<Double, byte[]> scoreMembers);
Set<byte[]> zrange(byte[] key, long start, long end);
@@ -159,45 +157,45 @@ public interface BinaryJedisCommands {
Set<byte[]> zrevrangeByScore(byte[] key, byte[] max, byte[] min);
Set<byte[]> zrangeByScore(byte[] key, byte[] min, byte[] max, int offset,
int count);
int count);
Set<byte[]> zrevrangeByScore(byte[] key, double max, double min,
int offset, int count);
int offset, int count);
Set<Tuple> zrangeByScoreWithScores(byte[] key, double min, double max);
Set<Tuple> zrevrangeByScoreWithScores(byte[] key, double max, double min);
Set<Tuple> zrangeByScoreWithScores(byte[] key, double min, double max,
int offset, int count);
int offset, int count);
Set<byte[]> zrevrangeByScore(byte[] key, byte[] max, byte[] min,
int offset, int count);
int offset, int count);
Set<Tuple> zrangeByScoreWithScores(byte[] key, byte[] min, byte[] max);
Set<Tuple> zrevrangeByScoreWithScores(byte[] key, byte[] max, byte[] min);
Set<Tuple> zrangeByScoreWithScores(byte[] key, byte[] min, byte[] max,
int offset, int count);
int offset, int count);
Set<Tuple> zrevrangeByScoreWithScores(byte[] key, double max, double min,
int offset, int count);
int offset, int count);
Set<Tuple> zrevrangeByScoreWithScores(byte[] key, byte[] max, byte[] min,
int offset, int count);
int offset, int count);
Long zremrangeByRank(byte[] key, long start, long end);
Long zremrangeByScore(byte[] key, double start, double end);
Long zremrangeByScore(byte[] key, byte[] start, byte[] end);
Long linsert(byte[] key, Client.LIST_POSITION where, byte[] pivot,
byte[] value);
byte[] value);
Long lpushx(byte[] key, byte[]... arg);
Long rpushx(byte[] key, byte[]... arg);
List<byte[]> blpop(byte[] arg);

View File

@@ -1,16 +1,15 @@
package redis.clients.jedis;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.util.Hashing;
import redis.clients.util.Sharded;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.util.Hashing;
import redis.clients.util.Sharded;
public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
implements BinaryJedisCommands {
public BinaryShardedJedis(List<JedisShardInfo> shards) {
@@ -31,10 +30,10 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
}
public void disconnect() {
for (Jedis jedis : getAllShards()) {
jedis.quit();
jedis.disconnect();
}
for (Jedis jedis : getAllShards()) {
jedis.quit();
jedis.disconnect();
}
}
protected Jedis create(JedisShardInfo shard) {
@@ -102,10 +101,10 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
}
public Long del(byte[] key) {
Jedis j = getShard(key);
return j.del(key);
Jedis j = getShard(key);
return j.del(key);
}
public Long incrBy(byte[] key, long integer) {
Jedis j = getShard(key);
return j.incrBy(key, integer);
@@ -197,23 +196,23 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
}
public Long strlen(final byte[] key) {
Jedis j = getShard(key);
return j.strlen(key);
Jedis j = getShard(key);
return j.strlen(key);
}
public Long lpushx(byte[] key, byte[]... string) {
Jedis j = getShard(key);
return j.lpushx(key, string);
Jedis j = getShard(key);
return j.lpushx(key, string);
}
public Long persist(final byte[] key) {
Jedis j = getShard(key);
return j.persist(key);
Jedis j = getShard(key);
return j.persist(key);
}
public Long rpushx(byte[] key, byte[]... string) {
Jedis j = getShard(key);
return j.rpushx(key, string);
Jedis j = getShard(key);
return j.rpushx(key, string);
}
public Long llen(byte[] key) {
@@ -365,7 +364,7 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
Jedis j = getShard(key);
return j.zcount(key, min, max);
}
public Long zcount(byte[] key, byte[] min, byte[] max) {
Jedis j = getShard(key);
return j.zcount(key, min, max);
@@ -394,8 +393,8 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
}
public Set<byte[]> zrangeByScore(byte[] key, byte[] min, byte[] max) {
Jedis j = getShard(key);
return j.zrangeByScore(key, min, max);
Jedis j = getShard(key);
return j.zrangeByScore(key, min, max);
}
public Set<Tuple> zrangeByScoreWithScores(byte[] key, byte[] min, byte[] max) {
@@ -404,14 +403,15 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
}
public Set<Tuple> zrangeByScoreWithScores(byte[] key, byte[] min,
byte[] max, int offset, int count) {
byte[] max, int offset, int count) {
Jedis j = getShard(key);
return j.zrangeByScoreWithScores(key, min, max, offset, count);
}
public Set<byte[]> zrangeByScore(byte[] key, byte[] min, byte[] max, int offset, int count) {
Jedis j = getShard(key);
return j.zrangeByScore(key, min, max, offset, count);
public Set<byte[]> zrangeByScore(byte[] key, byte[] min, byte[] max,
int offset, int count) {
Jedis j = getShard(key);
return j.zrangeByScore(key, min, max, offset, count);
}
public Set<byte[]> zrevrangeByScore(byte[] key, double max, double min) {
@@ -436,7 +436,7 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
Jedis j = getShard(key);
return j.zrevrangeByScoreWithScores(key, max, min, offset, count);
}
public Set<byte[]> zrevrangeByScore(byte[] key, byte[] max, byte[] min) {
Jedis j = getShard(key);
return j.zrevrangeByScore(key, max, min);
@@ -449,13 +449,13 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
}
public Set<Tuple> zrevrangeByScoreWithScores(byte[] key, byte[] max,
byte[] min) {
byte[] min) {
Jedis j = getShard(key);
return j.zrevrangeByScoreWithScores(key, max, min);
}
public Set<Tuple> zrevrangeByScoreWithScores(byte[] key, byte[] max,
byte[] min, int offset, int count) {
byte[] min, int offset, int count) {
Jedis j = getShard(key);
return j.zrevrangeByScoreWithScores(key, max, min, offset, count);
}
@@ -510,57 +510,57 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
}
public Boolean setbit(byte[] key, long offset, boolean value) {
Jedis j = getShard(key);
return j.setbit(key, offset, value);
Jedis j = getShard(key);
return j.setbit(key, offset, value);
}
public Boolean setbit(byte[] key, long offset, byte[] value) {
Jedis j = getShard(key);
return j.setbit(key, offset, value);
Jedis j = getShard(key);
return j.setbit(key, offset, value);
}
public Boolean getbit(byte[] key, long offset) {
Jedis j = getShard(key);
return j.getbit(key, offset);
Jedis j = getShard(key);
return j.getbit(key, offset);
}
public Long setrange(byte[] key, long offset, byte[] value) {
Jedis j = getShard(key);
return j.setrange(key, offset, value);
Jedis j = getShard(key);
return j.setrange(key, offset, value);
}
public byte[] getrange(byte[] key, long startOffset, long endOffset) {
Jedis j = getShard(key);
return j.getrange(key, startOffset, endOffset);
Jedis j = getShard(key);
return j.getrange(key, startOffset, endOffset);
}
public Long move(byte[] key, int dbIndex) {
Jedis j = getShard(key);
return j.move(key, dbIndex);
Jedis j = getShard(key);
return j.move(key, dbIndex);
}
public byte[] echo(byte[] arg) {
Jedis j = getShard(arg);
return j.echo(arg);
Jedis j = getShard(arg);
return j.echo(arg);
}
public List<byte[]> brpop(byte[] arg) {
Jedis j = getShard(arg);
return j.brpop(arg);
Jedis j = getShard(arg);
return j.brpop(arg);
}
public List<byte[]> blpop(byte[] arg) {
Jedis j = getShard(arg);
return j.blpop(arg);
Jedis j = getShard(arg);
return j.blpop(arg);
}
public Long bitcount(byte[] key) {
Jedis j = getShard(key);
return j.bitcount(key);
Jedis j = getShard(key);
return j.bitcount(key);
}
public Long bitcount(byte[] key, long start, long end) {
Jedis j = getShard(key);
return j.bitcount(key, start, end);
Jedis j = getShard(key);
return j.bitcount(key, start, end);
}
}

View File

@@ -1,11 +1,13 @@
package redis.clients.jedis;
import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
/**
* PoolableObjectFactory custom impl.
*/
class JedisFactory extends BasePoolableObjectFactory {
class JedisFactory implements PooledObjectFactory<Jedis> {
private final String host;
private final int port;
private final int timeout;
@@ -13,75 +15,80 @@ class JedisFactory extends BasePoolableObjectFactory {
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) {
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 JedisFactory(final String host, final int port, final int timeout,
final String password, final int database) {
this(host, port, timeout, password, database, null);
}
public Object makeObject() throws Exception {
final Jedis jedis = new Jedis(this.host, this.port, this.timeout);
jedis.connect();
if (null != this.password) {
jedis.auth(this.password);
}
if( database != 0 ) {
jedis.select(database);
}
if ( clientName != null ) {
jedis.clientSetname(clientName);
}
return jedis;
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;
}
@Override
public void activateObject(Object obj) throws Exception {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis)obj;
if (jedis.getDB() != database) {
jedis.select(database);
}
public void activateObject(PooledObject<Jedis> pooledJedis)
throws Exception {
final BinaryJedis jedis = pooledJedis.getObject();
if (jedis.getDB() != database) {
jedis.select(database);
}
}
@Override
public void destroyObject(PooledObject<Jedis> pooledJedis) throws Exception {
final BinaryJedis jedis = pooledJedis.getObject();
if (jedis.isConnected()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {
}
}
}
public void destroyObject(final Object obj) throws Exception {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis) obj;
if (jedis.isConnected()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {
@Override
public PooledObject<Jedis> makeObject() throws Exception {
final Jedis jedis = new Jedis(this.host, this.port, this.timeout);
}
}
}
jedis.connect();
if (null != this.password) {
jedis.auth(this.password);
}
if (database != 0) {
jedis.select(database);
}
if (clientName != null) {
jedis.clientSetname(clientName);
}
return new DefaultPooledObject<Jedis>(jedis);
}
public boolean validateObject(final Object obj) {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis) obj;
try {
return jedis.isConnected() && jedis.ping().equals("PONG");
} catch (final Exception e) {
return false;
}
} else {
return false;
}
@Override
public void passivateObject(PooledObject<Jedis> pooledJedis)
throws Exception {
// TODO maybe should select db 0? Not sure right now.
}
@Override
public boolean validateObject(PooledObject<Jedis> pooledJedis) {
final BinaryJedis jedis = pooledJedis.getObject();
try {
return jedis.isConnected() && jedis.ping().equals("PONG");
} catch (final Exception e) {
return false;
}
}
}

View File

@@ -2,19 +2,21 @@ package redis.clients.jedis;
import java.net.URI;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
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, null);
public JedisPool(final GenericObjectPoolConfig poolConfig, final String host) {
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, null);
this(new GenericObjectPoolConfig(), host, port,
Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null);
}
public JedisPool(final String host) {
@@ -24,12 +26,15 @@ public class JedisPool extends Pool<Jedis> {
int port = uri.getPort();
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, null), new Config());
this.internalPool = new GenericObjectPool<Jedis>(
new JedisFactory(h, port, Protocol.DEFAULT_TIMEOUT,
password, database, null),
new GenericObjectPoolConfig());
} else {
this.internalPool = new GenericObjectPool(new JedisFactory(host,
Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null,
Protocol.DEFAULT_DATABASE, null), new Config());
this.internalPool = new GenericObjectPool<Jedis>(new JedisFactory(
host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT,
null, Protocol.DEFAULT_DATABASE, null),
new GenericObjectPoolConfig());
}
}
@@ -38,39 +43,47 @@ public class JedisPool extends Pool<Jedis> {
int port = uri.getPort();
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, null), new Config());
this.internalPool = new GenericObjectPool<Jedis>(new JedisFactory(h,
port, Protocol.DEFAULT_TIMEOUT, password, database, null),
new GenericObjectPoolConfig());
}
public JedisPool(final Config poolConfig, final String host, int port,
int timeout, final String password) {
this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE, null);
public JedisPool(final GenericObjectPoolConfig poolConfig,
final String host, int port, int timeout, final String password) {
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, null);
public JedisPool(final GenericObjectPoolConfig poolConfig,
final String host, final int port) {
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, null);
public JedisPool(final GenericObjectPoolConfig poolConfig,
final String host, final int port, final int timeout) {
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) {
this(poolConfig, host, port, timeout, password, database, null);
public JedisPool(final GenericObjectPoolConfig poolConfig,
final String host, int port, int timeout, final String password,
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 JedisPool(final GenericObjectPoolConfig 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 returnBrokenResource(final Jedis resource) {
returnBrokenResourceObject(resource);
}
public void returnResource(final BinaryJedis resource) {
returnResourceObject(resource);
public void returnResource(final Jedis resource) {
returnResourceObject(resource);
}
}

View File

@@ -1,28 +1,8 @@
package redis.clients.jedis;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
/**
* Subclass of org.apache.commons.pool.impl.GenericObjectPool.Config that
* includes getters/setters so it can be more easily configured by Spring and
* other IoC frameworks.
*
* Spring example:
*
* <bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig"> <property
* name="testWhileIdle" value="true"/> </bean>
*
* <bean id="jedisPool" class="redis.clients.jedis.JedisPool"
* destroy-method="destroy"> <constructor-arg ref="jedisConfig" />
* <constructor-arg value="localhost" /> <constructor-arg type="int"
* value="6379" /> </bean>
*
* For information on parameters refer to:
*
* http://commons.apache.org/pool/apidocs/org/apache/commons/pool/impl/
* GenericObjectPool.html
*/
public class JedisPoolConfig extends Config {
public class JedisPoolConfig extends GenericObjectPoolConfig {
public JedisPoolConfig() {
// defaults to make your life with connection pool easier :)
setTestWhileIdle(true);
@@ -30,103 +10,4 @@ public class JedisPoolConfig extends Config {
setTimeBetweenEvictionRunsMillis(30000);
setNumTestsPerEvictionRun(-1);
}
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
public int getMinIdle() {
return minIdle;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public int getMaxActive() {
return maxActive;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public long getMaxWait() {
return maxWait;
}
public void setMaxWait(long maxWait) {
this.maxWait = maxWait;
}
public byte getWhenExhaustedAction() {
return whenExhaustedAction;
}
public void setWhenExhaustedAction(byte whenExhaustedAction) {
this.whenExhaustedAction = whenExhaustedAction;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
public boolean isTestOnReturn() {
return testOnReturn;
}
public void setTestOnReturn(boolean testOnReturn) {
this.testOnReturn = testOnReturn;
}
public boolean isTestWhileIdle() {
return testWhileIdle;
}
public void setTestWhileIdle(boolean testWhileIdle) {
this.testWhileIdle = testWhileIdle;
}
public long getTimeBetweenEvictionRunsMillis() {
return timeBetweenEvictionRunsMillis;
}
public void setTimeBetweenEvictionRunsMillis(
long timeBetweenEvictionRunsMillis) {
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
}
public int getNumTestsPerEvictionRun() {
return numTestsPerEvictionRun;
}
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
this.numTestsPerEvictionRun = numTestsPerEvictionRun;
}
public long getMinEvictableIdleTimeMillis() {
return minEvictableIdleTimeMillis;
}
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
}
public long getSoftMinEvictableIdleTimeMillis() {
return softMinEvictableIdleTimeMillis;
}
public void setSoftMinEvictableIdleTimeMillis(
long softMinEvictableIdleTimeMillis) {
this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
}
}

View File

@@ -7,14 +7,14 @@ import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.util.Pool;
public class JedisSentinelPool extends Pool<Jedis> {
protected Config poolConfig;
protected GenericObjectPoolConfig poolConfig;
protected int timeout = Protocol.DEFAULT_TIMEOUT;
@@ -27,57 +27,58 @@ public class JedisSentinelPool extends Pool<Jedis> {
protected Logger log = Logger.getLogger(getClass().getName());
public JedisSentinelPool(String masterName, Set<String> sentinels,
final Config poolConfig) {
final GenericObjectPoolConfig poolConfig) {
this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, null,
Protocol.DEFAULT_DATABASE);
}
public JedisSentinelPool(String masterName, Set<String> sentinels) {
this(masterName, sentinels, new Config(), Protocol.DEFAULT_TIMEOUT,
null, Protocol.DEFAULT_DATABASE);
this(masterName, sentinels, new GenericObjectPoolConfig(),
Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}
public JedisSentinelPool(String masterName, Set<String> sentinels,
String password) {
this(masterName, sentinels, new Config(), Protocol.DEFAULT_TIMEOUT,
password);
this(masterName, sentinels, new GenericObjectPoolConfig(),
Protocol.DEFAULT_TIMEOUT, password);
}
public JedisSentinelPool(String masterName, Set<String> sentinels,
final Config poolConfig, int timeout, final String password) {
final GenericObjectPoolConfig poolConfig, int timeout,
final String password) {
this(masterName, sentinels, poolConfig, timeout, password,
Protocol.DEFAULT_DATABASE);
}
public JedisSentinelPool(String masterName, Set<String> sentinels,
final Config poolConfig, final int timeout) {
final GenericObjectPoolConfig poolConfig, final int timeout) {
this(masterName, sentinels, poolConfig, timeout, null,
Protocol.DEFAULT_DATABASE);
}
public JedisSentinelPool(String masterName, Set<String> sentinels,
final Config poolConfig, final String password) {
final GenericObjectPoolConfig poolConfig, final String password) {
this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT,
password);
}
public JedisSentinelPool(String masterName, Set<String> sentinels,
final Config poolConfig, int timeout, final String password,
final int database) {
final GenericObjectPoolConfig poolConfig, int timeout,
final String password, final int database) {
this.poolConfig = poolConfig;
this.timeout = timeout;
this.password = password;
this.database = database;
HostAndPort master = initSentinels(sentinels, masterName);
initPool(master);
}
public void returnBrokenResource(final BinaryJedis resource) {
public void returnBrokenResource(final Jedis resource) {
returnBrokenResourceObject(resource);
}
public void returnResource(final BinaryJedis resource) {
public void returnResource(final Jedis resource) {
returnResourceObject(resource);
}

View File

@@ -3,83 +3,101 @@ package redis.clients.jedis;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.util.Hashing;
import redis.clients.util.Pool;
public class ShardedJedisPool extends Pool<ShardedJedis> {
public ShardedJedisPool(final GenericObjectPool.Config poolConfig,
List<JedisShardInfo> shards) {
this(poolConfig, shards, Hashing.MURMUR_HASH);
public ShardedJedisPool(final GenericObjectPoolConfig poolConfig,
List<JedisShardInfo> shards) {
this(poolConfig, shards, Hashing.MURMUR_HASH);
}
public ShardedJedisPool(final GenericObjectPool.Config poolConfig,
List<JedisShardInfo> shards, Hashing algo) {
this(poolConfig, shards, algo, null);
public ShardedJedisPool(final GenericObjectPoolConfig poolConfig,
List<JedisShardInfo> shards, Hashing algo) {
this(poolConfig, shards, algo, null);
}
public ShardedJedisPool(final GenericObjectPool.Config poolConfig,
List<JedisShardInfo> shards, Pattern keyTagPattern) {
this(poolConfig, shards, Hashing.MURMUR_HASH, keyTagPattern);
public ShardedJedisPool(final GenericObjectPoolConfig poolConfig,
List<JedisShardInfo> shards, Pattern keyTagPattern) {
this(poolConfig, shards, Hashing.MURMUR_HASH, keyTagPattern);
}
public ShardedJedisPool(final GenericObjectPool.Config poolConfig,
List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {
super(poolConfig, new ShardedJedisFactory(shards, algo, keyTagPattern));
public ShardedJedisPool(final GenericObjectPoolConfig poolConfig,
List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {
super(poolConfig, new ShardedJedisFactory(shards, algo, keyTagPattern));
}
/**
* PoolableObjectFactory custom impl.
*/
private static class ShardedJedisFactory extends BasePoolableObjectFactory {
private List<JedisShardInfo> shards;
private Hashing algo;
private Pattern keyTagPattern;
private static class ShardedJedisFactory implements
PooledObjectFactory<ShardedJedis> {
private List<JedisShardInfo> shards;
private Hashing algo;
private Pattern keyTagPattern;
public ShardedJedisFactory(List<JedisShardInfo> shards, Hashing algo,
Pattern keyTagPattern) {
this.shards = shards;
this.algo = algo;
this.keyTagPattern = keyTagPattern;
}
public ShardedJedisFactory(List<JedisShardInfo> shards, Hashing algo,
Pattern keyTagPattern) {
this.shards = shards;
this.algo = algo;
this.keyTagPattern = keyTagPattern;
}
public Object makeObject() throws Exception {
ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern);
return jedis;
}
@Override
public PooledObject<ShardedJedis> makeObject() throws Exception {
ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern);
return new DefaultPooledObject<ShardedJedis>(jedis);
}
public void destroyObject(final Object obj) throws Exception {
if ((obj != null) && (obj instanceof ShardedJedis)) {
ShardedJedis shardedJedis = (ShardedJedis) obj;
for (Jedis jedis : shardedJedis.getAllShards()) {
try {
try {
jedis.quit();
} catch (Exception e) {
@Override
public void destroyObject(PooledObject<ShardedJedis> pooledShardedJedis)
throws Exception {
final ShardedJedis shardedJedis = pooledShardedJedis.getObject();
for (Jedis jedis : shardedJedis.getAllShards()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {
}
}
}
}
}
}
}
public boolean validateObject(final Object obj) {
try {
ShardedJedis jedis = (ShardedJedis) obj;
for (Jedis shard : jedis.getAllShards()) {
if (!shard.ping().equals("PONG")) {
return false;
}
}
return true;
} catch (Exception ex) {
return false;
}
}
@Override
public boolean validateObject(
PooledObject<ShardedJedis> pooledShardedJedis) {
try {
ShardedJedis jedis = pooledShardedJedis.getObject();
for (Jedis shard : jedis.getAllShards()) {
if (!shard.ping().equals("PONG")) {
return false;
}
}
return true;
} catch (Exception ex) {
return false;
}
}
@Override
public void activateObject(PooledObject<ShardedJedis> p)
throws Exception {
}
@Override
public void passivateObject(PooledObject<ShardedJedis> p)
throws Exception {
}
}
}

View File

@@ -1,82 +1,84 @@
package redis.clients.util;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;
public abstract class Pool<T> {
protected GenericObjectPool internalPool;
protected GenericObjectPool<T> internalPool;
/**
* Using this constructor means you have to set
* and initialize the internalPool yourself.
* Using this constructor means you have to set and initialize the
* internalPool yourself.
*/
public Pool() {}
public Pool(final GenericObjectPool.Config poolConfig,
PoolableObjectFactory factory) {
initPool(poolConfig, factory);
public Pool() {
}
public void initPool(final GenericObjectPool.Config poolConfig, PoolableObjectFactory factory) {
if (this.internalPool != null) {
try {
closeInternalPool();
} catch (Exception e) {
}
}
this.internalPool = new GenericObjectPool(factory, poolConfig);
public Pool(final GenericObjectPoolConfig poolConfig,
PooledObjectFactory<T> factory) {
initPool(poolConfig, factory);
}
@SuppressWarnings("unchecked")
public void initPool(final GenericObjectPoolConfig poolConfig,
PooledObjectFactory<T> factory) {
if (this.internalPool != null) {
try {
closeInternalPool();
} catch (Exception e) {
}
}
this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
}
public T getResource() {
try {
return (T) internalPool.borrowObject();
} catch (Exception e) {
throw new JedisConnectionException(
"Could not get a resource from the pool", e);
}
try {
return internalPool.borrowObject();
} catch (Exception e) {
throw new JedisConnectionException(
"Could not get a resource from the pool", e);
}
}
public void returnResourceObject(final Object resource) {
try {
internalPool.returnObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
public void returnResourceObject(final T resource) {
try {
internalPool.returnObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
}
public void returnBrokenResource(final T resource) {
returnBrokenResourceObject(resource);
returnBrokenResourceObject(resource);
}
public void returnResource(final T resource) {
returnResourceObject(resource);
returnResourceObject(resource);
}
public void destroy() {
closeInternalPool();
closeInternalPool();
}
protected void returnBrokenResourceObject(final Object resource) {
try {
internalPool.invalidateObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
protected void returnBrokenResourceObject(final T resource) {
try {
internalPool.invalidateObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
}
protected void closeInternalPool() {
try {
internalPool.close();
} catch (Exception e) {
throw new JedisException("Could not destroy the pool", e);
}
try {
internalPool.close();
} catch (Exception e) {
throw new JedisException("Could not destroy the pool", e);
}
}
}