Apache Jakarta commons-pool based JedisPool.
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -56,6 +56,13 @@
|
|||||||
<type>jar</type>
|
<type>jar</type>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-pool</groupId>
|
||||||
|
<artifactId>commons-pool</artifactId>
|
||||||
|
<version>1.5.5</version>
|
||||||
|
<type>jar</type>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public class Connection {
|
|||||||
private RedisOutputStream outputStream;
|
private RedisOutputStream outputStream;
|
||||||
private RedisInputStream inputStream;
|
private RedisInputStream inputStream;
|
||||||
private int pipelinedCommands = 0;
|
private int pipelinedCommands = 0;
|
||||||
private int timeout = 2000;
|
private int timeout = Protocol.DEFAULT_TIMEOUT;
|
||||||
|
|
||||||
public int getTimeout() {
|
public int getTimeout() {
|
||||||
return timeout;
|
return timeout;
|
||||||
|
|||||||
@@ -1,67 +1,87 @@
|
|||||||
package redis.clients.jedis;
|
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> {
|
public class JedisPool {
|
||||||
private String host;
|
private final GenericObjectPool internalPool;
|
||||||
private int port;
|
|
||||||
private int timeout;
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
public JedisPool(String host) {
|
public JedisPool(final GenericObjectPool.Config poolConfig, final String host) {
|
||||||
this.host = host;
|
this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null);
|
||||||
this.port = Protocol.DEFAULT_PORT;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JedisPool(String host, int port) {
|
public JedisPool(final GenericObjectPool.Config poolConfig,final String host, final int port) {
|
||||||
this.host = host;
|
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null);
|
||||||
this.port = port;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JedisPool(String host, int port, int timeout) {
|
public JedisPool(final GenericObjectPool.Config poolConfig,final String host, final int port, final int timeout) {
|
||||||
this.host = host;
|
this(poolConfig, host, port, timeout, null);
|
||||||
this.port = port;
|
|
||||||
this.timeout = timeout;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JedisPool(String host, int port, int timeout, String 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 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.host = host;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.timeout = timeout;
|
this.timeout = (timeout > 0) ? timeout : -1;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JedisPool(JedisShardInfo shardInfo) {
|
@Override
|
||||||
this.host = shardInfo.getHost();
|
public Object makeObject() throws Exception {
|
||||||
this.port = shardInfo.getPort();
|
final Jedis jedis;
|
||||||
this.timeout = shardInfo.getTimeout();
|
if (timeout > 0) {
|
||||||
this.password = shardInfo.getPassword();
|
jedis = new Jedis(this.host, this.port, this.timeout);
|
||||||
|
} else {
|
||||||
|
jedis = new Jedis(this.host, this.port);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Jedis createResource() {
|
|
||||||
Jedis jedis = new Jedis(this.host, this.port, this.timeout);
|
|
||||||
boolean done = false;
|
|
||||||
while (!done) {
|
|
||||||
try {
|
|
||||||
jedis.connect();
|
jedis.connect();
|
||||||
if (password != null) {
|
if (null != this.password) {
|
||||||
jedis.auth(password);
|
jedis.auth(this.password);
|
||||||
}
|
|
||||||
done = true;
|
|
||||||
} catch (Exception e) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(100);
|
|
||||||
} catch (InterruptedException e1) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return jedis;
|
return jedis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void destroyResource(Jedis jedis) {
|
public void destroyObject(final Object obj) throws Exception {
|
||||||
if (jedis != null && jedis.isConnected()) {
|
if(obj instanceof Jedis) {
|
||||||
|
final Jedis jedis = (Jedis) obj;
|
||||||
|
if (jedis.isConnected()) {
|
||||||
try {
|
try {
|
||||||
jedis.quit();
|
jedis.quit();
|
||||||
jedis.disconnect();
|
jedis.disconnect();
|
||||||
@@ -70,13 +90,22 @@ public class JedisPool extends FixedResourcePool<Jedis> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isResourceValid(Jedis jedis) {
|
public boolean validateObject(final Object obj) {
|
||||||
|
if(obj instanceof Jedis) {
|
||||||
|
final Jedis jedis = (Jedis) obj;
|
||||||
try {
|
try {
|
||||||
return jedis.isConnected() && jedis.ping().equals("PONG");
|
return jedis.isConnected() && jedis.ping().equals("PONG");
|
||||||
} catch (Exception ex) {
|
} catch (final Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import static redis.clients.util.RedisOutputStream.CHARSET;
|
|||||||
public final class Protocol {
|
public final class Protocol {
|
||||||
|
|
||||||
public static final int DEFAULT_PORT = 6379;
|
public static final int DEFAULT_PORT = 6379;
|
||||||
|
public static final int DEFAULT_TIMEOUT = 2000;
|
||||||
|
|
||||||
public static final byte DOLLAR_BYTE = '$';
|
public static final byte DOLLAR_BYTE = '$';
|
||||||
public static final byte ASTERISK_BYTE = '*';
|
public static final byte ASTERISK_BYTE = '*';
|
||||||
|
|||||||
Reference in New Issue
Block a user