| 1 | package fr.sii.ogham.core.retry; | |
| 2 | ||
| 3 | import java.time.Instant; | |
| 4 | ||
| 5 | /** | |
| 6 | * Retry several times with a fixed delay between each try until the maximum | |
| 7 | * attempts is reached. The next execution is based on the execution start date. | |
| 8 | * | |
| 9 | * If delay is 500ms and max retries is 5, it means that a retry will be | |
| 10 | * attempted every 500ms until 5 attempts are reached (inclusive). For example, | |
| 11 | * you want to connect to an external system at t1=0 and the connection timeout | |
| 12 | * (100ms) is triggered at t2=100ms. Using this retry will provide the following | |
| 13 | * behavior: | |
| 14 | * | |
| 15 | * <ul> | |
| 16 | * <li>0: connect</li> | |
| 17 | * <li>100: timeout</li> | |
| 18 | * <li>500: connect</li> | |
| 19 | * <li>600: timeout</li> | |
| 20 | * <li>1000: connect</li> | |
| 21 | * <li>1100: timeout</li> | |
| 22 | * <li>1500: connect</li> | |
| 23 | * <li>1600: timeout</li> | |
| 24 | * <li>2000: connect</li> | |
| 25 | * <li>2100: timeout</li> | |
| 26 | * <li>2500: connect</li> | |
| 27 | * <li>2600: timeout</li> | |
| 28 | * <li>fail</li> | |
| 29 | * </ul> | |
| 30 | * | |
| 31 | * | |
| 32 | * <strong>NOTE:</strong> The provided date doesn't take the duration of the | |
| 33 | * execution in account. If an execution takes 1s to execute while retry delay | |
| 34 | * is set to 500ms, there may have several executions in parallel. However, this | |
| 35 | * totally depends on the {@link RetryExecutor} implementation. For example | |
| 36 | * {@link SimpleRetryExecutor} won't run several executions in parallel. In this | |
| 37 | * case, it will execute the action as soon as the previous one has failed | |
| 38 | * therefore the delay may not be complied. | |
| 39 | * | |
| 40 | * @author Aurélien Baudet | |
| 41 | * | |
| 42 | */ | |
| 43 | public class FixedIntervalRetry implements RetryStrategy { | |
| 44 | private final int maxRetries; | |
| 45 | private final long interval; | |
| 46 | private Instant firstExecutionTime; | |
| 47 | private int retries; | |
| 48 | private int remainingRetries; | |
| 49 | ||
| 50 | /** | |
| 51 | * Initializes with the maximum attempts and the delay between each attempt. | |
| 52 | * | |
| 53 | * @param maxRetries | |
| 54 | * the maximum attempts | |
| 55 | * @param interval | |
| 56 | * the interval between attempts | |
| 57 | */ | |
| 58 | public FixedIntervalRetry(int maxRetries, long interval) { | |
| 59 | super(); | |
| 60 | this.maxRetries = maxRetries; | |
| 61 | this.interval = interval; | |
| 62 | remainingRetries = maxRetries; | |
| 63 | } | |
| 64 | ||
| 65 | @Override | |
| 66 | public boolean terminated() { | |
| 67 |
6
1. terminated : replaced boolean return with true for fr/sii/ogham/core/retry/FixedIntervalRetry::terminated → NO_COVERAGE 2. terminated : changed conditional boundary → NO_COVERAGE 3. terminated : negated conditional → NO_COVERAGE 4. terminated : replaced boolean return with true for fr/sii/ogham/core/retry/FixedIntervalRetry::terminated → KILLED 5. terminated : changed conditional boundary → KILLED 6. terminated : negated conditional → KILLED |
return remainingRetries <= 0; |
| 68 | } | |
| 69 | ||
| 70 | @Override | |
| 71 | public Instant nextDate(Instant executionStartTime, Instant executionFailureTime) { | |
| 72 |
2
1. nextDate : Replaced integer subtraction with addition → NO_COVERAGE 2. nextDate : Replaced integer subtraction with addition → KILLED |
remainingRetries--; |
| 73 |
2
1. nextDate : Replaced integer addition with subtraction → NO_COVERAGE 2. nextDate : Replaced integer addition with subtraction → KILLED |
retries++; |
| 74 |
2
1. nextDate : negated conditional → NO_COVERAGE 2. nextDate : negated conditional → KILLED |
if (firstExecutionTime == null) { |
| 75 | firstExecutionTime = executionStartTime; | |
| 76 | } | |
| 77 |
4
1. nextDate : Replaced long multiplication with division → NO_COVERAGE 2. nextDate : replaced return value with null for fr/sii/ogham/core/retry/FixedIntervalRetry::nextDate → NO_COVERAGE 3. nextDate : Replaced long multiplication with division → KILLED 4. nextDate : replaced return value with null for fr/sii/ogham/core/retry/FixedIntervalRetry::nextDate → KILLED |
return firstExecutionTime.plusMillis(interval * retries); |
| 78 | } | |
| 79 | ||
| 80 | public int getRemainingRetries() { | |
| 81 |
2
1. getRemainingRetries : replaced int return with 0 for fr/sii/ogham/core/retry/FixedIntervalRetry::getRemainingRetries → NO_COVERAGE 2. getRemainingRetries : replaced int return with 0 for fr/sii/ogham/core/retry/FixedIntervalRetry::getRemainingRetries → KILLED |
return remainingRetries; |
| 82 | } | |
| 83 | ||
| 84 | public int getMaxRetries() { | |
| 85 |
1
1. getMaxRetries : replaced int return with 0 for fr/sii/ogham/core/retry/FixedIntervalRetry::getMaxRetries → NO_COVERAGE |
return maxRetries; |
| 86 | } | |
| 87 | ||
| 88 | } | |
Mutations | ||
| 67 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
| 72 |
1.1 2.2 |
|
| 73 |
1.1 2.2 |
|
| 74 |
1.1 2.2 |
|
| 77 |
1.1 2.2 3.3 4.4 |
|
| 81 |
1.1 2.2 |
|
| 85 |
1.1 |