Skip to content

Commit d8a8eef

Browse files
sazzad16ggivo
andauthored
Add retryable command execution example (#3780)
Co-authored-by: ggivo <ivo.gaydazhiev@redis.com>
1 parent 8ea3b6e commit d8a8eef

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package redis.clients.jedis.examples;
2+
3+
import java.time.Duration;
4+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
5+
import redis.clients.jedis.Connection;
6+
import redis.clients.jedis.ConnectionPoolConfig;
7+
import redis.clients.jedis.DefaultJedisClientConfig;
8+
import redis.clients.jedis.HostAndPort;
9+
import redis.clients.jedis.JedisClientConfig;
10+
import redis.clients.jedis.UnifiedJedis;
11+
import redis.clients.jedis.providers.PooledConnectionProvider;
12+
13+
/**
14+
* It is possible to retry command executions in case of connection failures in UnifiedJedis class.
15+
*
16+
* The retry-ability comes through RetryableCommandExecutor class. It is also possible to directly provide
17+
* RetryableCommandExecutor as a parameter.
18+
*
19+
* Note: RetryableCommandExecutor should not be considered for
20+
* <a href="https://redis.io/docs/reference/cluster-spec/">Open Source Redis Cluster mode</a> because it requires to
21+
* handle more than connection failures. These are done in ClusterCommandExecutor.
22+
*/
23+
public class RetryableCommandExecution {
24+
25+
public static void main(String[] args) {
26+
27+
// Connection and pool parameters
28+
HostAndPort hostAndPort = new HostAndPort("127.0.0.1", 6379);
29+
JedisClientConfig clientConfig = DefaultJedisClientConfig.builder().user("myuser").password("mypassword").build();
30+
GenericObjectPoolConfig<Connection> poolConfig = new ConnectionPoolConfig();
31+
32+
PooledConnectionProvider provider = new PooledConnectionProvider(hostAndPort, clientConfig, poolConfig);
33+
34+
// Retry parameters
35+
int maxAttempts = 5;
36+
Duration maxTotalRetriesDuration = Duration.ofSeconds(2);
37+
38+
UnifiedJedis jedis = new UnifiedJedis(provider, maxAttempts, maxTotalRetriesDuration);
39+
40+
jedis.set("foo", "bar");
41+
jedis.get("foo");
42+
43+
jedis.close();
44+
}
45+
}

0 commit comments

Comments
 (0)