diff --git a/src/main/java/redis/clients/jedis/ShardedJedisPool.java b/src/main/java/redis/clients/jedis/ShardedJedisPool.java index 75916145bb..87f9b6dd31 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedisPool.java +++ b/src/main/java/redis/clients/jedis/ShardedJedisPool.java @@ -34,7 +34,12 @@ public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, List shards, Hashing algo, Pattern keyTagPattern) { - super(poolConfig, new ShardedJedisFactory(shards, algo, keyTagPattern)); + this(poolConfig, new ShardedJedisFactory(shards, algo, keyTagPattern)); + } + + public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, + PooledObjectFactory shardedJedisPooledObjectFactory) { + super(poolConfig, shardedJedisPooledObjectFactory); } @Override @@ -55,7 +60,7 @@ public void returnResource(final ShardedJedis resource) { /** * PoolableObjectFactory custom impl. */ - private static class ShardedJedisFactory implements PooledObjectFactory { + public static class ShardedJedisFactory implements PooledObjectFactory { private final List shards; private final Hashing algo; diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java index f0fb5489e7..2e8f300b99 100644 --- a/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java @@ -8,7 +8,9 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.Before; import org.junit.Test; @@ -20,6 +22,7 @@ import redis.clients.jedis.ShardedJedisPipeline; import redis.clients.jedis.ShardedJedisPool; import redis.clients.jedis.exceptions.JedisExhaustedPoolException; +import redis.clients.jedis.util.Hashing; public class ShardedJedisPoolTest { private static HostAndPort redis1 = HostAndPortUtil.getRedisServers().get(0); @@ -262,4 +265,28 @@ public void checkResourceIsCloseable() throws URISyntaxException { } } + @Test + public void checkOverrideFactoryValidateMethod() { + AtomicInteger counter = new AtomicInteger(0); + + ShardedJedisPool.ShardedJedisFactory overriddenFactory = new ShardedJedisPool.ShardedJedisFactory( + shards, Hashing.MURMUR_HASH, null) { + @Override public boolean validateObject(PooledObject pooledShardedJedis) { + counter.incrementAndGet(); + return super.validateObject(pooledShardedJedis); + } + }; + + GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig<>(); + poolConfig.setTestOnReturn(true); + ShardedJedisPool pool = new ShardedJedisPool(poolConfig, overriddenFactory); + try (ShardedJedis jedis = pool.getResource();) { + jedis.set("foo", "bar"); + assertEquals(0, counter.get()); + } + assertEquals(1, counter.get()); + + pool.destroy(); + } + }