93 lines
3.0 KiB
Java
93 lines
3.0 KiB
Java
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 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.MD5);
|
|
}
|
|
|
|
public ShardedJedisPool(final GenericObjectPool.Config 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.MD5, keyTagPattern);
|
|
}
|
|
|
|
public ShardedJedisPool(final GenericObjectPool.Config 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;
|
|
|
|
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);
|
|
boolean done = false;
|
|
while (!done) {
|
|
try {
|
|
for (Jedis shard : jedis.getAllShards()) {
|
|
if (!shard.isConnected()) {
|
|
shard.connect();
|
|
}
|
|
}
|
|
done = true;
|
|
} catch (Exception e) {
|
|
try {
|
|
Thread.sleep(100);
|
|
} catch (InterruptedException e1) {
|
|
}
|
|
}
|
|
}
|
|
return jedis;
|
|
}
|
|
|
|
public void destroyObject(final Object obj) throws Exception {
|
|
if (obj != null) {
|
|
try {
|
|
((ShardedJedis) obj).disconnect();
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public boolean validateObject(final Object obj) {
|
|
try {
|
|
ShardedJedis jedis = (ShardedJedis) obj;
|
|
for (Jedis shard : jedis.getAllShards()) {
|
|
if (!shard.isConnected() || !shard.ping().equals("PONG")) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
} catch (Exception ex) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
} |