Skip to content

Commit

Permalink
Allow to override ShardedJedisPool object factory (#2612)
Browse files Browse the repository at this point in the history
  • Loading branch information
bokshitsky authored and sazzad16 committed Aug 3, 2021
1 parent 21d934e commit 91d6abb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/main/java/redis/clients/jedis/ShardedJedisPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public ShardedJedisPool(final GenericObjectPoolConfig<ShardedJedis> poolConfig,

public ShardedJedisPool(final GenericObjectPoolConfig<ShardedJedis> poolConfig,
List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {
super(poolConfig, new ShardedJedisFactory(shards, algo, keyTagPattern));
this(poolConfig, new ShardedJedisFactory(shards, algo, keyTagPattern));
}

public ShardedJedisPool(final GenericObjectPoolConfig<ShardedJedis> poolConfig,
PooledObjectFactory<ShardedJedis> shardedJedisPooledObjectFactory) {
super(poolConfig, shardedJedisPooledObjectFactory);
}

@Override
Expand All @@ -55,7 +60,7 @@ public void returnResource(final ShardedJedis resource) {
/**
* PoolableObjectFactory custom impl.
*/
private static class ShardedJedisFactory implements PooledObjectFactory<ShardedJedis> {
public static class ShardedJedisFactory implements PooledObjectFactory<ShardedJedis> {

private final List<JedisShardInfo> shards;
private final Hashing algo;
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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<ShardedJedis> pooledShardedJedis) {
counter.incrementAndGet();
return super.validateObject(pooledShardedJedis);
}
};

GenericObjectPoolConfig<ShardedJedis> 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();
}

}

0 comments on commit 91d6abb

Please sign in to comment.