1
1
package de .malkusch .niu ;
2
2
3
- import java .io .IOException ;
4
- import java .time .Duration ;
5
-
6
3
import dev .failsafe .Failsafe ;
7
4
import dev .failsafe .FailsafeException ;
8
5
import dev .failsafe .FailsafeExecutor ;
9
6
import dev .failsafe .RetryPolicy ;
10
7
8
+ import java .io .IOException ;
9
+ import java .time .Duration ;
10
+
11
11
interface Retry <T > {
12
12
13
- public record Configuration (int retries , Duration delay ) {
13
+ record Configuration (int retries , Duration delay ) {
14
14
15
15
static final Configuration DISABLED = new Configuration (0 , Duration .ZERO );
16
16
17
- static final Configuration DEFAULT = new Configuration (3 , Duration .ofSeconds (10 ));
18
-
19
17
boolean isDisabled () {
20
18
return this == DISABLED ;
21
19
}
22
- };
20
+ }
23
21
24
- public static <T > Retry <T > build (Configuration configuration ) {
22
+ static <T > Retry <T > build (Configuration configuration ) {
25
23
if (configuration .isDisabled ()) {
26
24
return new DisabledRetry <>();
27
25
@@ -31,27 +29,27 @@ public static <T> Retry<T> build(Configuration configuration) {
31
29
}
32
30
33
31
@ FunctionalInterface
34
- static interface Operation <T , E1 extends Throwable , E2 extends Throwable > {
32
+ interface Operation <T , E1 extends Throwable , E2 extends Throwable > {
35
33
T execute () throws E1 , E2 ;
36
34
}
37
35
38
36
<E1 extends Throwable , E2 extends Throwable > T retry (Operation <T , E1 , E2 > operation ) throws E1 , E2 ;
39
37
40
- static final class DisabledRetry <T > implements Retry <T > {
38
+ final class DisabledRetry <T > implements Retry <T > {
41
39
42
40
@ Override
43
41
public <E1 extends Throwable , E2 extends Throwable > T retry (Operation <T , E1 , E2 > operation ) throws E1 , E2 {
44
42
return operation .execute ();
45
43
}
46
44
}
47
45
48
- static final class FailSafeRetry <T > implements Retry <T > {
46
+ final class FailSafeRetry <T > implements Retry <T > {
49
47
50
48
private final FailsafeExecutor <T > failsafe ;
51
49
52
50
FailSafeRetry (Configuration configuration ) {
53
51
failsafe = Failsafe .with ( //
54
- RetryPolicy .<T > builder () //
52
+ RetryPolicy .<T >builder () //
55
53
.handle (IOException .class ) //
56
54
.withMaxRetries (configuration .retries ()) //
57
55
.withDelay (configuration .delay ()) //
0 commit comments