replace custom pool implementation with apache's
This commit is contained in:
@@ -3,80 +3,95 @@ package redis.clients.jedis;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import redis.clients.util.FixedResourcePool;
|
||||
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 FixedResourcePool<ShardedJedis> {
|
||||
private List<JedisShardInfo> shards;
|
||||
private Hashing algo = Hashing.MD5;
|
||||
private Pattern keyTagPattern;
|
||||
|
||||
public ShardedJedisPool(List<JedisShardInfo> shards) {
|
||||
this.shards = shards;
|
||||
public class ShardedJedisPool extends Pool<ShardedJedis> {
|
||||
public ShardedJedisPool(final GenericObjectPool.Config poolConfig,
|
||||
List<JedisShardInfo> shards) {
|
||||
this(poolConfig, shards, Hashing.MD5);
|
||||
}
|
||||
|
||||
public ShardedJedisPool(List<JedisShardInfo> shards, Hashing algo) {
|
||||
this.shards = shards;
|
||||
this.algo = algo;
|
||||
public ShardedJedisPool(final GenericObjectPool.Config poolConfig,
|
||||
List<JedisShardInfo> shards, Hashing algo) {
|
||||
this(poolConfig, shards, algo, null);
|
||||
}
|
||||
|
||||
public ShardedJedisPool(List<JedisShardInfo> shards, Pattern keyTagPattern) {
|
||||
this.shards = shards;
|
||||
this.keyTagPattern = keyTagPattern;
|
||||
public ShardedJedisPool(final GenericObjectPool.Config poolConfig,
|
||||
List<JedisShardInfo> shards, Pattern keyTagPattern) {
|
||||
this(poolConfig, shards, Hashing.MD5, keyTagPattern);
|
||||
}
|
||||
|
||||
public ShardedJedisPool(List<JedisShardInfo> shards, Hashing algo,
|
||||
Pattern keyTagPattern) {
|
||||
this.shards = shards;
|
||||
this.algo = algo;
|
||||
this.keyTagPattern = keyTagPattern;
|
||||
public ShardedJedisPool(final GenericObjectPool.Config poolConfig,
|
||||
List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {
|
||||
super(poolConfig, new ShardedJedisFactory(shards, algo, keyTagPattern));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ShardedJedis createResource() {
|
||||
ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern);
|
||||
boolean done = false;
|
||||
while (!done) {
|
||||
try {
|
||||
for (JedisShardInfo shard : jedis.getAllShards()) {
|
||||
if (!shard.getResource().isConnected()) {
|
||||
shard.getResource().connect();
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object makeObject() throws Exception {
|
||||
ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern);
|
||||
boolean done = false;
|
||||
while (!done) {
|
||||
try {
|
||||
for (JedisShardInfo shard : jedis.getAllShards()) {
|
||||
if (!shard.getResource().isConnected()) {
|
||||
shard.getResource().connect();
|
||||
}
|
||||
}
|
||||
done = true;
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e1) {
|
||||
}
|
||||
}
|
||||
done = true;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return jedis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyObject(final Object obj) throws Exception {
|
||||
if (obj != null) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e1) {
|
||||
((ShardedJedis) obj).disconnect();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return jedis;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void destroyResource(ShardedJedis jedis) {
|
||||
if (jedis != null) {
|
||||
@Override
|
||||
public boolean validateObject(final Object obj) {
|
||||
try {
|
||||
jedis.disconnect();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isResourceValid(ShardedJedis jedis) {
|
||||
try {
|
||||
for (JedisShardInfo shard : jedis.getAllShards()) {
|
||||
if (!shard.getResource().isConnected()
|
||||
|| !shard.getResource().ping().equals("PONG")) {
|
||||
return false;
|
||||
ShardedJedis jedis = (ShardedJedis) obj;
|
||||
for (JedisShardInfo shard : jedis.getAllShards()) {
|
||||
if (!shard.getResource().isConnected()
|
||||
|| !shard.getResource().ping().equals("PONG")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user