replace custom pool implementation with apache's
This commit is contained in:
@@ -2,110 +2,95 @@ package redis.clients.jedis;
|
||||
|
||||
import org.apache.commons.pool.BasePoolableObjectFactory;
|
||||
import org.apache.commons.pool.impl.GenericObjectPool;
|
||||
import org.apache.commons.pool.impl.GenericObjectPool.Config;
|
||||
|
||||
public class JedisPool {
|
||||
private final GenericObjectPool internalPool;
|
||||
import redis.clients.util.Pool;
|
||||
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig, final String host) {
|
||||
this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null);
|
||||
}
|
||||
public class JedisPool extends Pool<Jedis> {
|
||||
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig,final String host, final int port) {
|
||||
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null);
|
||||
}
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig,
|
||||
final String host) {
|
||||
this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT,
|
||||
null);
|
||||
}
|
||||
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig,final String host, final int port, final int timeout) {
|
||||
this(poolConfig, host, port, timeout, null);
|
||||
}
|
||||
public JedisPool(final Config poolConfig, final String host, int port,
|
||||
int timeout, final String password) {
|
||||
super(poolConfig, new JedisFactory(host, port, timeout, password));
|
||||
}
|
||||
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig,final String host, final int port, final int timeout, final String password) {
|
||||
final String lhost;
|
||||
final int lport;
|
||||
final int ltimeout;
|
||||
final String lpassword;
|
||||
lhost = host;
|
||||
lport = port;
|
||||
ltimeout = (timeout > 0) ? timeout : Protocol.DEFAULT_TIMEOUT;
|
||||
lpassword = password;
|
||||
|
||||
final JedisFactory factory = new JedisFactory(lhost, lport, ltimeout, lpassword);
|
||||
this.internalPool = new GenericObjectPool(factory, poolConfig);
|
||||
}
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig,
|
||||
final String host, final int port) {
|
||||
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null);
|
||||
}
|
||||
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig, final JedisShardInfo shardInfo) {
|
||||
this(poolConfig, shardInfo.getHost(), shardInfo.getPort(), shardInfo.getTimeout(), shardInfo.getPassword());
|
||||
}
|
||||
|
||||
public Jedis getResource() throws Exception {
|
||||
return (Jedis) internalPool.borrowObject();
|
||||
}
|
||||
|
||||
public void returnResource(final Jedis jedis) throws Exception {
|
||||
internalPool.returnObject(jedis);
|
||||
}
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig,
|
||||
final String host, final int port, final int timeout) {
|
||||
this(poolConfig, host, port, timeout, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* PoolableObjectFactory custom impl.
|
||||
*/
|
||||
private static class JedisFactory extends BasePoolableObjectFactory {
|
||||
private final String host;
|
||||
private final int port;
|
||||
private final int timeout;
|
||||
private final String password;
|
||||
/**
|
||||
* PoolableObjectFactory custom impl.
|
||||
*/
|
||||
private static class JedisFactory extends BasePoolableObjectFactory {
|
||||
private final String host;
|
||||
private final int port;
|
||||
private final int timeout;
|
||||
private final String password;
|
||||
|
||||
public JedisFactory(final String host, final int port, final int timeout, final String password) {
|
||||
super();
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = (timeout > 0) ? timeout : -1;
|
||||
this.password = password;
|
||||
}
|
||||
public JedisFactory(final String host, final int port,
|
||||
final int timeout, final String password) {
|
||||
super();
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = (timeout > 0) ? timeout : -1;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object makeObject() throws Exception {
|
||||
final Jedis jedis;
|
||||
if (timeout > 0) {
|
||||
jedis = new Jedis(this.host, this.port, this.timeout);
|
||||
} else {
|
||||
jedis = new Jedis(this.host, this.port);
|
||||
}
|
||||
@Override
|
||||
public Object makeObject() throws Exception {
|
||||
final Jedis jedis;
|
||||
if (timeout > 0) {
|
||||
jedis = new Jedis(this.host, this.port, this.timeout);
|
||||
} else {
|
||||
jedis = new Jedis(this.host, this.port);
|
||||
}
|
||||
|
||||
jedis.connect();
|
||||
if (null != this.password) {
|
||||
jedis.auth(this.password);
|
||||
}
|
||||
return jedis;
|
||||
}
|
||||
jedis.connect();
|
||||
if (null != this.password) {
|
||||
jedis.auth(this.password);
|
||||
}
|
||||
return jedis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyObject(final Object obj) throws Exception {
|
||||
if(obj instanceof Jedis) {
|
||||
final Jedis jedis = (Jedis) obj;
|
||||
if (jedis.isConnected()) {
|
||||
try {
|
||||
jedis.quit();
|
||||
jedis.disconnect();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void destroyObject(final Object obj) throws Exception {
|
||||
if (obj instanceof Jedis) {
|
||||
final Jedis jedis = (Jedis) obj;
|
||||
if (jedis.isConnected()) {
|
||||
try {
|
||||
jedis.quit();
|
||||
jedis.disconnect();
|
||||
} catch (Exception e) {
|
||||
|
||||
@Override
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/main/java/redis/clients/util/Pool.java
Normal file
30
src/main/java/redis/clients/util/Pool.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package redis.clients.util;
|
||||
|
||||
import org.apache.commons.pool.PoolableObjectFactory;
|
||||
import org.apache.commons.pool.impl.GenericObjectPool;
|
||||
|
||||
public abstract class Pool<T> {
|
||||
private final GenericObjectPool internalPool;
|
||||
|
||||
public Pool(final GenericObjectPool.Config poolConfig,
|
||||
PoolableObjectFactory factory) {
|
||||
this.internalPool = new GenericObjectPool(factory, poolConfig);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T getResource() throws Exception {
|
||||
return (T) internalPool.borrowObject();
|
||||
}
|
||||
|
||||
public void returnResource(final T resource) throws Exception {
|
||||
internalPool.returnObject(resource);
|
||||
}
|
||||
|
||||
public void returnBrokenResource(final T resource) throws Exception {
|
||||
internalPool.invalidateObject(resource);
|
||||
}
|
||||
|
||||
public void destroy() throws Exception {
|
||||
internalPool.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user