|
| 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 | + |
0 commit comments