-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test module commands at 6479 port and test JSON commands
Also, fix JSON commands
- Loading branch information
Showing
42 changed files
with
938 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/main/java/redis/clients/jedis/ConnectionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package redis.clients.jedis; | ||
|
||
|
||
import org.apache.commons.pool2.PooledObject; | ||
import org.apache.commons.pool2.PooledObjectFactory; | ||
import org.apache.commons.pool2.impl.DefaultPooledObject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import redis.clients.jedis.exceptions.JedisException; | ||
import redis.clients.jedis.util.Pool; | ||
|
||
/** | ||
* PoolableObjectFactory custom impl. | ||
*/ | ||
public class ConnectionFactory implements PooledObjectFactory<Connection> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ConnectionFactory.class); | ||
|
||
private final JedisSocketFactory jedisSocketFactory; | ||
|
||
private final JedisClientConfig clientConfig; | ||
|
||
private volatile Pool<Connection> pool; | ||
|
||
public ConnectionFactory(final HostAndPort hostAndPort) { | ||
this.clientConfig = DefaultJedisClientConfig.builder().build(); | ||
this.jedisSocketFactory = new DefaultJedisSocketFactory(hostAndPort); | ||
} | ||
|
||
public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig) { | ||
this.clientConfig = DefaultJedisClientConfig.copyConfig(clientConfig); | ||
this.jedisSocketFactory = new DefaultJedisSocketFactory(hostAndPort, this.clientConfig); | ||
} | ||
|
||
public ConnectionFactory(final JedisSocketFactory jedisSocketFactory, final JedisClientConfig clientConfig) { | ||
this.clientConfig = DefaultJedisClientConfig.copyConfig(clientConfig); | ||
this.jedisSocketFactory = jedisSocketFactory; | ||
} | ||
|
||
public void setPool(Pool<Connection> pool) { | ||
this.pool = pool; | ||
} | ||
|
||
public void setPassword(final String password) { | ||
this.clientConfig.updatePassword(password); | ||
} | ||
|
||
@Override | ||
public void activateObject(PooledObject<Connection> pooledConnection) throws Exception { | ||
// final Connection jedis = pooledConnection.getObject(); | ||
// if (jedis.getDB() != clientConfig.getDatabase()) { | ||
// jedis.select(clientConfig.getDatabase()); | ||
// } | ||
} | ||
|
||
@Override | ||
public void destroyObject(PooledObject<Connection> pooledConnection) throws Exception { | ||
final Connection jedis = pooledConnection.getObject(); | ||
if (jedis.isConnected()) { | ||
try { | ||
// need a proper test, probably with mock | ||
if (!jedis.isBroken()) { | ||
jedis.quit(); | ||
} | ||
} catch (RuntimeException e) { | ||
logger.warn("Error while QUIT", e); | ||
} | ||
try { | ||
jedis.close(); | ||
} catch (RuntimeException e) { | ||
logger.warn("Error while close", e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public PooledObject<Connection> makeObject() throws Exception { | ||
if (pool == null) { | ||
throw new IllegalStateException("Pool is not set."); | ||
} | ||
Connection jedis = null; | ||
try { | ||
jedis = new Connection(jedisSocketFactory, clientConfig, pool); | ||
jedis.connect(); | ||
return new DefaultPooledObject<>(jedis); | ||
} catch (JedisException je) { | ||
if (jedis != null) { | ||
try { | ||
jedis.quit(); | ||
} catch (RuntimeException e) { | ||
logger.warn("Error while QUIT", e); | ||
} | ||
try { | ||
jedis.close(); | ||
} catch (RuntimeException e) { | ||
logger.warn("Error while close", e); | ||
} | ||
} | ||
throw je; | ||
} | ||
} | ||
|
||
@Override | ||
public void passivateObject(PooledObject<Connection> pooledConnection) throws Exception { | ||
// TODO maybe should select db 0? Not sure right now. | ||
} | ||
|
||
@Override | ||
public boolean validateObject(PooledObject<Connection> pooledConnection) { | ||
final Connection jedis = pooledConnection.getObject(); | ||
try { | ||
// String host = jedisSocketFactory.getHost(); | ||
// int port = jedisSocketFactory.getPort(); | ||
// | ||
// | ||
// String connectionHost = jedis.getClient().getHost(); | ||
// int connectionPort = jedis.getClient().getPort(); | ||
// | ||
// return host.equals(connectionHost) | ||
// && port == connectionPort && jedis.isConnected() | ||
// && jedis.ping().equals("PONG"); | ||
return jedis.isConnected() && jedis.ping(); | ||
} catch (final Exception e) { | ||
logger.error("Error while validating pooled Connection object.", e); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.