|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2014-2016 Ilkka Seppälä |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.iluwatar.retry; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Random; |
| 32 | +import java.util.concurrent.atomic.AtomicInteger; |
| 33 | +import java.util.function.Predicate; |
| 34 | + |
| 35 | +/** |
| 36 | + * Decorates {@link BusinessOperation business operation} with "retry" capabilities. |
| 37 | + * |
| 38 | + * @author George Aristy (george.aristy@gmail.com) |
| 39 | + * @param <T> the remote op's return type |
| 40 | + */ |
| 41 | +public final class RetryExponentialBackoff<T> implements BusinessOperation<T> { |
| 42 | + private final BusinessOperation<T> op; |
| 43 | + private final int maxAttempts; |
| 44 | + private final long maxDelay; |
| 45 | + private final AtomicInteger attempts; |
| 46 | + private final Predicate<Exception> test; |
| 47 | + private final List<Exception> errors; |
| 48 | + |
| 49 | + /** |
| 50 | + * Ctor. |
| 51 | + * |
| 52 | + * @param op the {@link BusinessOperation} to retry |
| 53 | + * @param maxAttempts number of times to retry |
| 54 | + * @param ignoreTests tests to check whether the remote exception can be ignored. No exceptions |
| 55 | + * will be ignored if no tests are given |
| 56 | + */ |
| 57 | + @SafeVarargs |
| 58 | + public RetryExponentialBackoff( |
| 59 | + BusinessOperation<T> op, |
| 60 | + int maxAttempts, |
| 61 | + long maxDelay, |
| 62 | + Predicate<Exception>... ignoreTests |
| 63 | + ) { |
| 64 | + this.op = op; |
| 65 | + this.maxAttempts = maxAttempts; |
| 66 | + this.maxDelay = maxDelay; |
| 67 | + this.attempts = new AtomicInteger(); |
| 68 | + this.test = Arrays.stream(ignoreTests).reduce(Predicate::or).orElse(e -> false); |
| 69 | + this.errors = new ArrayList<>(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * The errors encountered while retrying, in the encounter order. |
| 74 | + * |
| 75 | + * @return the errors encountered while retrying |
| 76 | + */ |
| 77 | + public List<Exception> errors() { |
| 78 | + return Collections.unmodifiableList(this.errors); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * The number of retries performed. |
| 83 | + * |
| 84 | + * @return the number of retries performed |
| 85 | + */ |
| 86 | + public int attempts() { |
| 87 | + return this.attempts.intValue(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public T perform() throws BusinessException { |
| 92 | + do { |
| 93 | + try { |
| 94 | + return this.op.perform(); |
| 95 | + } catch (BusinessException e) { |
| 96 | + this.errors.add(e); |
| 97 | + |
| 98 | + if (this.attempts.incrementAndGet() >= this.maxAttempts || !this.test.test(e)) { |
| 99 | + throw e; |
| 100 | + } |
| 101 | + |
| 102 | + try { |
| 103 | + Random rand = new Random(); |
| 104 | + long testDelay = (long) Math.pow(2, this.attempts()) * 1000 + rand.nextInt(1000); |
| 105 | + long delay = testDelay < this.maxDelay ? testDelay : maxDelay; |
| 106 | + Thread.sleep(delay); |
| 107 | + } catch (InterruptedException f) { |
| 108 | + //ignore |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + while (true); |
| 113 | + } |
| 114 | +} |
| 115 | + |
0 commit comments