Apache Jakarta commons-pool based JedisPool.
This commit is contained in:
@@ -18,7 +18,7 @@ public class Connection {
|
||||
private RedisOutputStream outputStream;
|
||||
private RedisInputStream inputStream;
|
||||
private int pipelinedCommands = 0;
|
||||
private int timeout = 2000;
|
||||
private int timeout = Protocol.DEFAULT_TIMEOUT;
|
||||
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
|
||||
@@ -1,82 +1,111 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import redis.clients.util.FixedResourcePool;
|
||||
import org.apache.commons.pool.BasePoolableObjectFactory;
|
||||
import org.apache.commons.pool.impl.GenericObjectPool;
|
||||
|
||||
public class JedisPool extends FixedResourcePool<Jedis> {
|
||||
private String host;
|
||||
private int port;
|
||||
private int timeout;
|
||||
private String password;
|
||||
public class JedisPool {
|
||||
private final GenericObjectPool internalPool;
|
||||
|
||||
public JedisPool(String host) {
|
||||
this.host = host;
|
||||
this.port = Protocol.DEFAULT_PORT;
|
||||
}
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig, final String host) {
|
||||
this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null);
|
||||
}
|
||||
|
||||
public JedisPool(String host, int port) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig,final String host, final int port) {
|
||||
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null);
|
||||
}
|
||||
|
||||
public JedisPool(String host, int port, int timeout) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig,final String host, final int port, final int timeout) {
|
||||
this(poolConfig, host, port, timeout, null);
|
||||
}
|
||||
|
||||
public JedisPool(String host, int port, int timeout, String password) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
this.password = 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(JedisShardInfo shardInfo) {
|
||||
this.host = shardInfo.getHost();
|
||||
this.port = shardInfo.getPort();
|
||||
this.timeout = shardInfo.getTimeout();
|
||||
this.password = shardInfo.getPassword();
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Jedis createResource() {
|
||||
Jedis jedis = new Jedis(this.host, this.port, this.timeout);
|
||||
boolean done = false;
|
||||
while (!done) {
|
||||
try {
|
||||
jedis.connect();
|
||||
if (password != null) {
|
||||
jedis.auth(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;
|
||||
}
|
||||
done = true;
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e1) {
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
return jedis;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void destroyResource(Jedis jedis) {
|
||||
if (jedis != null && 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
|
||||
protected boolean isResourceValid(Jedis jedis) {
|
||||
try {
|
||||
return jedis.isConnected() && jedis.ping().equals("PONG");
|
||||
} catch (Exception ex) {
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import static redis.clients.util.RedisOutputStream.CHARSET;
|
||||
public final class Protocol {
|
||||
|
||||
public static final int DEFAULT_PORT = 6379;
|
||||
public static final int DEFAULT_TIMEOUT = 2000;
|
||||
|
||||
public static final byte DOLLAR_BYTE = '$';
|
||||
public static final byte ASTERISK_BYTE = '*';
|
||||
|
||||
Reference in New Issue
Block a user