redis.clients.jedis.ConnectionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jedis_preview Show documentation
Show all versions of jedis_preview Show documentation
Jedis is a blazingly small and sane Redis java client.
The newest version!
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;
/**
* PoolableObjectFactory custom impl.
*/
public class ConnectionFactory implements PooledObjectFactory {
private static final Logger logger = LoggerFactory.getLogger(ConnectionFactory.class);
private final JedisSocketFactory jedisSocketFactory;
private final JedisClientConfig clientConfig;
private ClientSideCache clientSideCache = null;
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 = clientConfig;
this.jedisSocketFactory = new DefaultJedisSocketFactory(hostAndPort, this.clientConfig);
}
public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCache csCache) {
this.clientConfig = clientConfig;
this.jedisSocketFactory = new DefaultJedisSocketFactory(hostAndPort, this.clientConfig);
this.clientSideCache = csCache;
}
public ConnectionFactory(final JedisSocketFactory jedisSocketFactory, final JedisClientConfig clientConfig) {
this.clientConfig = clientConfig;
this.jedisSocketFactory = jedisSocketFactory;
}
@Override
public void activateObject(PooledObject pooledConnection) throws Exception {
// what to do ??
}
@Override
public void destroyObject(PooledObject pooledConnection) throws Exception {
final Connection jedis = pooledConnection.getObject();
if (jedis.isConnected()) {
try {
jedis.close();
} catch (RuntimeException e) {
logger.debug("Error while close", e);
}
}
}
@Override
public PooledObject makeObject() throws Exception {
try {
Connection jedis = clientSideCache == null
? new Connection(jedisSocketFactory, clientConfig)
: new Connection(jedisSocketFactory, clientConfig, clientSideCache);
return new DefaultPooledObject<>(jedis);
} catch (JedisException je) {
logger.debug("Error while makeObject", je);
throw je;
}
}
@Override
public void passivateObject(PooledObject pooledConnection) throws Exception {
// TODO maybe should select db 0? Not sure right now.
}
@Override
public boolean validateObject(PooledObject pooledConnection) {
final Connection jedis = pooledConnection.getObject();
try {
// check HostAndPort ??
return jedis.isConnected() && jedis.ping();
} catch (final Exception e) {
logger.error("Error while validating pooled Connection object.", e);
return false;
}
}
}