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

Commit 158dbbd

Browse files
author
raghavan chockalingam
committed
Alternate RetryConfigBuilder to group relevant configs together for user of use
contains few groups AlternateRetryConfigBuilder is the alternate implementation Usage is described using AlternateRetryConfigBuilder 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 158dbbd

File tree

2 files changed

+255
-0
lines changed

2 files changed

+255
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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 = BackoffStrategyFactory.withFixedBackoff();
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 BackoffStrategyFactory {
131+
132+
public static BackoffStrategy withFixedBackoff() {
133+
return new FixedBackoffStrategy();
134+
}
135+
136+
public static BackoffStrategy withExponentialBackoff() {
137+
return new ExponentialBackoffStrategy();
138+
}
139+
140+
public static BackoffStrategy withFibonacciBackoff() {
141+
return new FibonacciBackoffStrategy();
142+
}
143+
144+
public static BackoffStrategy withNoWaitBackoff() {
145+
return new NoWaitBackoffStrategy();
146+
}
147+
148+
public static BackoffStrategy withRandomBackoff() {
149+
return new RandomBackoffStrategy();
150+
}
151+
152+
public static BackoffStrategy withRandomExponentialBackoff() {
153+
return new RandomExponentialBackoffStrategy();
154+
}
155+
156+
}
157+
158+
public AlternateRetryConfigBuilder withBackoffStrategy(BackoffStrategy backoffStrategy) {
159+
this.backoffStrategy = backoffStrategy;
160+
return this;
161+
}
162+
163+
public AlternateRetryConfigBuilder withExceptionRetryconfig(ExceptionRetryConfig exceptionRetryConfig) {
164+
this.exceptionRetryConfig = exceptionRetryConfig;
165+
return this;
166+
}
167+
168+
public AlternateRetryConfigBuilder withTimingRetryConfig(TimingRetryConfig timingRetryConfig) {
169+
this.timingRetryConfig = timingRetryConfig;
170+
return this;
171+
}
172+
173+
public RetryConfig build() {
174+
RetryConfig retryConfig = new RetryConfig(
175+
exceptionRetryConfig.retryOnAnyException, exceptionRetryConfig.exceptionsCriteriaBuilder.retryOnSpecificExceptions,
176+
exceptionRetryConfig.exceptionsCriteriaBuilder.retryOnAnyExceptionExcluding, timingRetryConfig.maxNumberOfTries,
177+
timingRetryConfig.delayBetweenRetries, backoffStrategy, valueToRetryOn, retryOnValue,
178+
exceptionRetryConfig.customRetryOnLogic, exceptionRetryConfig.exceptionsCriteriaBuilder.retryOnCausedBy
179+
);
180+
return retryConfig;
181+
}
182+
183+
}
184+
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.BackoffStrategyFactory;
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(BackoffStrategyFactory.withNoWaitBackoff())
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(BackoffStrategyFactory.withExponentialBackoff())
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(BackoffStrategyFactory.withExponentialBackoff())
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(BackoffStrategyFactory.withFibonacciBackoff())
68+
.build();
69+
}
70+
71+
}

0 commit comments

Comments
 (0)