From 8ac40f40c12154378fa443fc9834f075b181ff82 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 20 Mar 2024 15:54:34 +0600 Subject: [PATCH] Add retryable command execution example --- .../GeoShapeFieldsUsageInRediSearch.java | 2 - .../examples/RetryableCommandExecution.java | 45 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/test/java/redis/clients/jedis/examples/RetryableCommandExecution.java diff --git a/src/test/java/redis/clients/jedis/examples/GeoShapeFieldsUsageInRediSearch.java b/src/test/java/redis/clients/jedis/examples/GeoShapeFieldsUsageInRediSearch.java index fd309def50..6cb059db3d 100644 --- a/src/test/java/redis/clients/jedis/examples/GeoShapeFieldsUsageInRediSearch.java +++ b/src/test/java/redis/clients/jedis/examples/GeoShapeFieldsUsageInRediSearch.java @@ -1,6 +1,5 @@ package redis.clients.jedis.examples; -import java.util.Collections; import org.junit.Assert; import org.locationtech.jts.geom.Coordinate; @@ -14,7 +13,6 @@ import redis.clients.jedis.JedisPooled; import redis.clients.jedis.UnifiedJedis; import redis.clients.jedis.search.FTSearchParams; -import redis.clients.jedis.search.RediSearchUtil; import redis.clients.jedis.search.SearchResult; import redis.clients.jedis.search.schemafields.GeoShapeField; diff --git a/src/test/java/redis/clients/jedis/examples/RetryableCommandExecution.java b/src/test/java/redis/clients/jedis/examples/RetryableCommandExecution.java new file mode 100644 index 0000000000..af8a72d00e --- /dev/null +++ b/src/test/java/redis/clients/jedis/examples/RetryableCommandExecution.java @@ -0,0 +1,45 @@ +package redis.clients.jedis.examples; + +import java.time.Duration; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import redis.clients.jedis.Connection; +import redis.clients.jedis.ConnectionPoolConfig; +import redis.clients.jedis.DefaultJedisClientConfig; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisClientConfig; +import redis.clients.jedis.UnifiedJedis; +import redis.clients.jedis.providers.PooledConnectionProvider; + +/** + * It is possible to retry command executions in case of connection failures in UnifiedJedis class. + * + * The retry-ability comes through RetryableCommandExecutor class. It is also possible to directly provide + * RetryableCommandExecutor as a parameter. + * + * Note: RetryableCommandExecutor should not be considered for + * Open Source Redis Cluster mode because it requires to + * handle more than connection failures. These are done in ClusterCommandExecutor. + */ +public class RetryableCommandExecution { + + public static void main(String[] args) { + + // Connection and pool parameters + HostAndPort hostAndPort = new HostAndPort("127.0.0.1", 6379); + JedisClientConfig clientConfig = DefaultJedisClientConfig.builder().user("myuser").password("mypassword").build(); + GenericObjectPoolConfig poolConfig = new ConnectionPoolConfig(); + + PooledConnectionProvider provider = new PooledConnectionProvider(hostAndPort, clientConfig, poolConfig); + + // Retry parameters + int maxAttempts = 5; + Duration maxTotalRetriesDuration = Duration.ofSeconds(2); + + UnifiedJedis jedis = new UnifiedJedis(provider, maxAttempts, maxTotalRetriesDuration); + + jedis.set("foo", "bar"); + jedis.get("foo"); + + jedis.close(); + } +}