Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retryable command execution example #3780

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package redis.clients.jedis.examples;

import java.util.Collections;
import org.junit.Assert;

import org.locationtech.jts.geom.Coordinate;
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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
* <a href="https://redis.io/docs/reference/cluster-spec/">Open Source Redis Cluster mode</a> 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<Connection> 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();
}
}