Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit d72c8d5

Browse files
author
raghavan chockalingam
committed
Alternate RetryConfigBuilder to group relevant configs together for user of use
AlternateRetryConfigBuilder is the alternate implementation Usage is described using AlternateRetryConfigBuilder contains few config groups Many subclasses are designed mutable for use like Builders Many subclasses could be extracted to become parent level classes in their own files
1 parent d0635b3 commit d72c8d5

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package com.evanlennick.retry4j.config;
2+
3+
import com.evanlennick.retry4j.backoff.*;
4+
import com.evanlennick.retry4j.exception.InvalidRetryConfigException;
5+
6+
import java.time.Duration;
7+
import java.time.temporal.ChronoUnit;
8+
import java.util.Arrays;
9+
import java.util.HashSet;
10+
import java.util.Set;
11+
import java.util.function.Function;
12+
13+
public class AlternateRetryConfigBuilder {
14+
15+
private Object valueToRetryOn;
16+
private Boolean retryOnValue = false;
17+
// Using some defaults here, but we need not declare defaults and validate for nulls during `build()`
18+
private BackoffStrategy backoffStrategy = BackoffStrategyRegistry.fixedBackoff;
19+
private ExceptionRetryConfig exceptionRetryConfig = new ExceptionRetryConfig()
20+
.retryOnAnyException();
21+
private TimingRetryConfig timingRetryConfig = TimingRetryConfig.retryIndefinitely()
22+
.withDelayBetweenTries(2, ChronoUnit.SECONDS);
23+
24+
private AlternateRetryConfigBuilder() {
25+
}
26+
27+
public static AlternateRetryConfigBuilder exceptionRetryConfig(ExceptionRetryConfig exceptionRetryConfig) {
28+
AlternateRetryConfigBuilder builder = new AlternateRetryConfigBuilder();
29+
builder.exceptionRetryConfig = exceptionRetryConfig;
30+
return builder;
31+
}
32+
33+
public static AlternateRetryConfigBuilder retryOnReturnValue(Object value) {
34+
AlternateRetryConfigBuilder builder = new AlternateRetryConfigBuilder();
35+
builder.valueToRetryOn = value;
36+
builder.retryOnValue = true;
37+
return builder;
38+
}
39+
40+
public static class ExceptionRetryConfig {
41+
private Boolean retryOnAnyException = false;
42+
private Function<Exception, Boolean> customRetryOnLogic;
43+
private ExceptionsCriteria exceptionsCriteriaBuilder = new ExceptionsCriteria();
44+
45+
public static ExceptionRetryConfig retryOnAnyException() {
46+
ExceptionRetryConfig config = new ExceptionRetryConfig();
47+
config.retryOnAnyException = true;
48+
return config;
49+
}
50+
51+
public static ExceptionRetryConfig failOnAnyException() {
52+
ExceptionRetryConfig config = new ExceptionRetryConfig();
53+
config.retryOnAnyException = false;
54+
return config;
55+
}
56+
57+
public static ExceptionRetryConfig retryOnExceptions(ExceptionsCriteria exceptionsCriteriaBuilder) {
58+
ExceptionRetryConfig config = new ExceptionRetryConfig();
59+
config.exceptionsCriteriaBuilder = exceptionsCriteriaBuilder;
60+
return config;
61+
}
62+
63+
public static ExceptionRetryConfig retryOnCustomExceptionLogic(Function<Exception, Boolean> customRetryFunction) {
64+
ExceptionRetryConfig config = new ExceptionRetryConfig();
65+
config.customRetryOnLogic = customRetryFunction;
66+
return config;
67+
}
68+
}
69+
70+
public static class ExceptionsCriteria {
71+
private Set<Class<? extends Exception>> retryOnSpecificExceptions = new HashSet<>();
72+
private Set<Class<? extends Exception>> retryOnAnyExceptionExcluding = new HashSet<>();
73+
private boolean retryOnCausedBy;
74+
75+
public ExceptionsCriteria retryOnSpecificExceptions(Class<? extends Exception>... exceptions) {
76+
this.retryOnSpecificExceptions = new HashSet<>(Arrays.asList(exceptions));
77+
return this;
78+
}
79+
80+
public ExceptionsCriteria retryOnAnyExceptionExcluding(Class<? extends Exception>... exceptions) {
81+
this.retryOnAnyExceptionExcluding = new HashSet<>(Arrays.asList(exceptions));
82+
return this;
83+
}
84+
85+
public ExceptionsCriteria retryOnCausedBy() {
86+
this.retryOnCausedBy = true;
87+
return this;
88+
}
89+
}
90+
91+
public static class TimingRetryConfig {
92+
93+
private Integer maxNumberOfTries;
94+
private Duration delayBetweenRetries;
95+
public static final String SHOULD_SPECIFY_DELAY_BETWEEN_RETRIES_AS_POSTIVE__ERROR_MSG
96+
= "Delay between retries must be a non-negative Duration.";
97+
98+
private TimingRetryConfig() {
99+
}
100+
101+
public static TimingRetryConfig withMaxTries(int maxTries) {
102+
TimingRetryConfig config = new TimingRetryConfig();
103+
config.maxNumberOfTries = maxTries;
104+
return config;
105+
}
106+
107+
public static TimingRetryConfig retryIndefinitely() {
108+
TimingRetryConfig config = new TimingRetryConfig();
109+
config.maxNumberOfTries = Integer.MAX_VALUE;
110+
return config;
111+
}
112+
113+
114+
public TimingRetryConfig withDelayBetweenTries(Duration duration) {
115+
if (duration.isNegative()) {
116+
throw new InvalidRetryConfigException(SHOULD_SPECIFY_DELAY_BETWEEN_RETRIES_AS_POSTIVE__ERROR_MSG);
117+
}
118+
119+
delayBetweenRetries = duration;
120+
return this;
121+
}
122+
123+
public TimingRetryConfig withDelayBetweenTries(long amount, ChronoUnit time) {
124+
delayBetweenRetries = Duration.of(amount, time);
125+
return this;
126+
}
127+
128+
}
129+
130+
public static class BackoffStrategyRegistry {
131+
public static final BackoffStrategy fixedBackoff = new FixedBackoffStrategy();
132+
public static final BackoffStrategy exponentialBackoff = new ExponentialBackoffStrategy();
133+
public static final BackoffStrategy fibonacciBackoff = new FibonacciBackoffStrategy();
134+
public static final BackoffStrategy noWaitBackoff= new NoWaitBackoffStrategy();
135+
public static final BackoffStrategy randomBackoff= new RandomBackoffStrategy();
136+
public static final BackoffStrategy randomExponentialBackoff = new RandomExponentialBackoffStrategy();
137+
}
138+
139+
public AlternateRetryConfigBuilder withBackoffStrategy(BackoffStrategy backoffStrategy) {
140+
this.backoffStrategy = backoffStrategy;
141+
return this;
142+
}
143+
144+
public AlternateRetryConfigBuilder withExceptionRetryconfig(ExceptionRetryConfig exceptionRetryConfig) {
145+
this.exceptionRetryConfig = exceptionRetryConfig;
146+
return this;
147+
}
148+
149+
public AlternateRetryConfigBuilder withTimingRetryConfig(TimingRetryConfig timingRetryConfig) {
150+
this.timingRetryConfig = timingRetryConfig;
151+
return this;
152+
}
153+
154+
public RetryConfig build() {
155+
RetryConfig retryConfig = new RetryConfig(
156+
exceptionRetryConfig.retryOnAnyException, exceptionRetryConfig.exceptionsCriteriaBuilder.retryOnSpecificExceptions,
157+
exceptionRetryConfig.exceptionsCriteriaBuilder.retryOnAnyExceptionExcluding, timingRetryConfig.maxNumberOfTries,
158+
timingRetryConfig.delayBetweenRetries, backoffStrategy, valueToRetryOn, retryOnValue,
159+
exceptionRetryConfig.customRetryOnLogic, exceptionRetryConfig.exceptionsCriteriaBuilder.retryOnCausedBy
160+
);
161+
return retryConfig;
162+
}
163+
164+
}
165+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.evanlennick.retry4j;
2+
3+
4+
import com.evanlennick.retry4j.config.AlternateRetryConfigBuilder;
5+
import com.evanlennick.retry4j.config.AlternateRetryConfigBuilder.BackoffStrategyRegistry;
6+
import com.evanlennick.retry4j.config.AlternateRetryConfigBuilder.ExceptionRetryConfig;
7+
import com.evanlennick.retry4j.config.AlternateRetryConfigBuilder.ExceptionsCriteria;
8+
import com.evanlennick.retry4j.config.AlternateRetryConfigBuilder.TimingRetryConfig;
9+
import org.testng.annotations.Test;
10+
11+
import java.io.FileNotFoundException;
12+
import java.io.IOException;
13+
import java.time.temporal.ChronoUnit;
14+
15+
public class AlternateRetryConfigBuilderTest {
16+
17+
@Test
18+
public void testConfig1() {
19+
AlternateRetryConfigBuilder
20+
.exceptionRetryConfig(
21+
new ExceptionRetryConfig().retryOnAnyException()
22+
)
23+
.withTimingRetryConfig(
24+
TimingRetryConfig.withMaxTries(200)
25+
)
26+
.withBackoffStrategy(BackoffStrategyRegistry.noWaitBackoff)
27+
.build();
28+
}
29+
30+
@Test
31+
public void testConfig2() {
32+
AlternateRetryConfigBuilder
33+
.exceptionRetryConfig(
34+
new ExceptionRetryConfig().retryOnExceptions(new ExceptionsCriteria()
35+
.retryOnSpecificExceptions(IllegalArgumentException.class))
36+
)
37+
.withTimingRetryConfig(
38+
TimingRetryConfig.retryIndefinitely().withDelayBetweenTries(3, ChronoUnit.MINUTES)
39+
)
40+
.withBackoffStrategy(BackoffStrategyRegistry.exponentialBackoff)
41+
.build();
42+
}
43+
44+
@Test
45+
public void testConfig3() {
46+
AlternateRetryConfigBuilder
47+
.exceptionRetryConfig(
48+
new ExceptionRetryConfig().retryOnExceptions(new ExceptionsCriteria()
49+
.retryOnSpecificExceptions(IOException.class)
50+
.retryOnAnyExceptionExcluding(FileNotFoundException.class)
51+
.retryOnCausedBy())
52+
)
53+
.withTimingRetryConfig(
54+
TimingRetryConfig.retryIndefinitely().withDelayBetweenTries(3, ChronoUnit.MINUTES)
55+
)
56+
.withBackoffStrategy(BackoffStrategyRegistry.exponentialBackoff)
57+
.build();
58+
}
59+
60+
@Test
61+
public void testConfig4() {
62+
AlternateRetryConfigBuilder
63+
.retryOnReturnValue("retry on this value!")
64+
.withTimingRetryConfig(
65+
TimingRetryConfig.retryIndefinitely()
66+
)
67+
.withBackoffStrategy(BackoffStrategyRegistry.fibonacciBackoff)
68+
.build();
69+
}
70+
71+
}

0 commit comments

Comments
 (0)