Skip to content

Commit

Permalink
Deprecate JedisPoolAbstract (#2361)
Browse files Browse the repository at this point in the history
* Remove JedisPoolAbstract and related changes

JedisPoolAbstract itself was doing nothing except acting as access point of Pool<Jedis> from redis.clients.jedis package. Using Pool<Jedis> directly seems better.

* backward compatibility by deprecating JedisPoolAbstract

* backward compatibility and deprecated

* added logs and removed unnecessary repeated codes

* Update src/main/java/redis/clients/jedis/JedisPool.java
  • Loading branch information
sazzad16 authored Feb 7, 2021
1 parent 6b49fa4 commit aa0158c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 61 deletions.
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands,
AdvancedJedisCommands, ScriptingCommands, BasicCommands, ClusterCommands, SentinelCommands, ModuleCommands {

/**
* @deprecated This will be private in future.
*/
@Deprecated
protected JedisPoolAbstract dataSource = null;

public Jedis() {
Expand Down
38 changes: 15 additions & 23 deletions src/main/java/redis/clients/jedis/JedisPool.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package redis.clients.jedis;

import java.net.URI;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.util.JedisURIHelper;

public class JedisPool extends JedisPoolAbstract {

private static final Logger log = LoggerFactory.getLogger(JedisPool.class);

public JedisPool() {
this(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT);
}
Expand All @@ -29,26 +30,24 @@ public JedisPool(String host, int port) {
public JedisPool(final String host) {
URI uri = URI.create(host);
if (JedisURIHelper.isValid(uri)) {
this.internalPool = new GenericObjectPool<>(new JedisFactory(uri,
Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null), new GenericObjectPoolConfig());
initPool(new GenericObjectPoolConfig(), new JedisFactory(uri, Protocol.DEFAULT_TIMEOUT,
Protocol.DEFAULT_TIMEOUT, null));
} else {
this.internalPool = new GenericObjectPool<>(new JedisFactory(host,
Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null,
Protocol.DEFAULT_DATABASE, null), new GenericObjectPoolConfig());
initPool(new GenericObjectPoolConfig(), new JedisFactory(host, Protocol.DEFAULT_PORT,
Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null));
}
}

public JedisPool(final String host, final SSLSocketFactory sslSocketFactory,
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
URI uri = URI.create(host);
if (JedisURIHelper.isValid(uri)) {
this.internalPool = new GenericObjectPool<>(new JedisFactory(uri,
Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, sslSocketFactory, sslParameters,
hostnameVerifier), new GenericObjectPoolConfig());
initPool(new GenericObjectPoolConfig(), new JedisFactory(uri, Protocol.DEFAULT_TIMEOUT,
Protocol.DEFAULT_TIMEOUT, null, sslSocketFactory, sslParameters, hostnameVerifier));
} else {
this.internalPool = new GenericObjectPool<>(new JedisFactory(host,
Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null,
Protocol.DEFAULT_DATABASE, null, false, null, null, null), new GenericObjectPoolConfig());
initPool(new GenericObjectPoolConfig(), new JedisFactory(host, Protocol.DEFAULT_PORT,
Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null,
false, null, null, null));
}
}

Expand Down Expand Up @@ -333,21 +332,14 @@ public Jedis getResource() {
}

@Override
protected void returnBrokenResource(final Jedis resource) {
if (resource != null) {
returnBrokenResourceObject(resource);
}
}

@Override
protected void returnResource(final Jedis resource) {
public void returnResource(final Jedis resource) {
if (resource != null) {
try {
resource.resetState();
returnResourceObject(resource);
} catch (Exception e) {
returnBrokenResource(resource);
throw new JedisException("Resource is returned to the pool as broken", e);
log.warn("Resource is returned to the pool as broken", e);
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/redis/clients/jedis/JedisPoolAbstract.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

import redis.clients.jedis.util.Pool;

/**
* @deprecated This class will be removed in future. If you are directly manipulating this class,
* you are suggested to change your code to use {@link Pool Pool&lt;Jedis&gt;} instead.
*/
@Deprecated
public class JedisPoolAbstract extends Pool<Jedis> {

public JedisPoolAbstract() {
Expand All @@ -14,14 +19,4 @@ public JedisPoolAbstract() {
public JedisPoolAbstract(GenericObjectPoolConfig poolConfig, PooledObjectFactory<Jedis> factory) {
super(poolConfig, factory);
}

@Override
protected void returnBrokenResource(Jedis resource) {
super.returnBrokenResource(resource);
}

@Override
protected void returnResource(Jedis resource) {
super.returnResource(resource);
}
}
26 changes: 11 additions & 15 deletions src/main/java/redis/clients/jedis/JedisSentinelPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
import redis.clients.jedis.exceptions.JedisException;

public class JedisSentinelPool extends JedisPoolAbstract {
protected Logger log = LoggerFactory.getLogger(getClass().getName());

/**
* @deprecated This will be private in future.
*/
@Deprecated
protected static Logger log = LoggerFactory.getLogger(JedisSentinelPool.class);

protected final GenericObjectPoolConfig poolConfig;

Expand Down Expand Up @@ -197,11 +202,9 @@ private void initPool(HostAndPort master) {
initPool(poolConfig, factory);
} else {
factory.setHostAndPort(currentHostMaster);
// although we clear the pool, we still have to check the
// returned object
// in getResource, this call only clears idle instances, not
// borrowed instances
internalPool.clear();
// although we clear the pool, we still have to check the returned object in getResource,
// this call only clears idle instances, not borrowed instances
clearInternalPool();
}

log.info("Created JedisPool to master at {}", master);
Expand Down Expand Up @@ -312,21 +315,14 @@ public Jedis getResource() {
}

@Override
protected void returnBrokenResource(final Jedis resource) {
if (resource != null) {
returnBrokenResourceObject(resource);
}
}

@Override
protected void returnResource(final Jedis resource) {
public void returnResource(final Jedis resource) {
if (resource != null) {
try {
resource.resetState();
returnResourceObject(resource);
} catch (Exception e) {
returnBrokenResource(resource);
throw new JedisException("Resource is returned to the pool as broken", e);
log.debug("Resource is returned to the pool as broken", e);
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/redis/clients/jedis/ShardedJedisPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,7 @@ public ShardedJedis getResource() {
}

@Override
protected void returnBrokenResource(final ShardedJedis resource) {
if (resource != null) {
returnBrokenResourceObject(resource);
}
}

@Override
protected void returnResource(final ShardedJedis resource) {
public void returnResource(final ShardedJedis resource) {
if (resource != null) {
resource.resetState();
returnResourceObject(resource);
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/redis/clients/jedis/util/Pool.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import redis.clients.jedis.exceptions.JedisExhaustedPoolException;

public abstract class Pool<T> implements Closeable {

/**
* @deprecated This will be private in future.
*/
@Deprecated
protected GenericObjectPool<T> internalPool;

/**
Expand Down Expand Up @@ -45,6 +50,12 @@ public void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFacto
this.internalPool = new GenericObjectPool<>(factory, poolConfig);
}

protected void clearInternalPool() {
if (internalPool != null) {
internalPool.clear();
}
}

public T getResource() {
try {
return internalPool.borrowObject();
Expand All @@ -61,23 +72,20 @@ public T getResource() {
}

protected void returnResourceObject(final T resource) {
if (resource == null) {
return;
}
try {
internalPool.returnObject(resource);
} catch (Exception e) {
throw new JedisException("Could not return the resource to the pool", e);
}
}

protected void returnBrokenResource(final T resource) {
public void returnBrokenResource(final T resource) {
if (resource != null) {
returnBrokenResourceObject(resource);
}
}

protected void returnResource(final T resource) {
public void returnResource(final T resource) {
if (resource != null) {
returnResourceObject(resource);
}
Expand Down

0 comments on commit aa0158c

Please sign in to comment.