|
13 | 13 | // info@rabbitmq.com. |
14 | 14 | package com.rabbitmq.stream.impl; |
15 | 15 |
|
| 16 | +import static com.rabbitmq.stream.impl.Utils.namedRunnable; |
| 17 | + |
16 | 18 | import com.rabbitmq.stream.BackOffDelayPolicy; |
17 | 19 | import java.time.Duration; |
18 | 20 | import java.util.concurrent.Callable; |
@@ -42,47 +44,56 @@ private AsyncRetry( |
42 | 44 | AtomicReference<Runnable> retryableTaskReference = new AtomicReference<>(); |
43 | 45 | AtomicInteger attempts = new AtomicInteger(0); |
44 | 46 | Runnable retryableTask = |
45 | | - () -> { |
46 | | - if (Thread.currentThread().isInterrupted()) { |
47 | | - LOGGER.debug("Task '{}' interrupted, failing future", description); |
48 | | - this.completableFuture.completeExceptionally(new CancellationException()); |
49 | | - return; |
50 | | - } |
51 | | - try { |
52 | | - V result = task.call(); |
53 | | - LOGGER.debug("Task '{}' succeeded, completing future", description); |
54 | | - completableFuture.complete(result); |
55 | | - } catch (Exception e) { |
56 | | - int attemptCount = attempts.getAndIncrement(); |
57 | | - if (retry.test(e)) { |
58 | | - if (delayPolicy.delay(attemptCount).equals(BackOffDelayPolicy.TIMEOUT)) { |
59 | | - LOGGER.debug( |
60 | | - "Retryable attempts for task '{}' timed out, failing future", description); |
61 | | - this.completableFuture.completeExceptionally(new RetryTimeoutException()); |
62 | | - } else { |
63 | | - LOGGER.debug( |
64 | | - "Retryable exception ({}) for task '{}', scheduling another attempt", |
65 | | - e.getClass().getSimpleName(), |
66 | | - description); |
67 | | - scheduler.schedule( |
68 | | - retryableTaskReference.get(), |
69 | | - delayPolicy.delay(attemptCount).toMillis(), |
70 | | - TimeUnit.MILLISECONDS); |
| 47 | + namedRunnable( |
| 48 | + () -> { |
| 49 | + if (Thread.currentThread().isInterrupted()) { |
| 50 | + LOGGER.debug("Task '{}' interrupted, failing future", description); |
| 51 | + this.completableFuture.completeExceptionally(new CancellationException()); |
| 52 | + return; |
| 53 | + } |
| 54 | + try { |
| 55 | + V result = task.call(); |
| 56 | + LOGGER.debug("Task '{}' succeeded, completing future", description); |
| 57 | + completableFuture.complete(result); |
| 58 | + } catch (Exception e) { |
| 59 | + int attemptCount = attempts.getAndIncrement(); |
| 60 | + if (retry.test(e)) { |
| 61 | + if (delayPolicy.delay(attemptCount).equals(BackOffDelayPolicy.TIMEOUT)) { |
| 62 | + LOGGER.debug( |
| 63 | + "Retryable attempts for task '{}' timed out, failing future", description); |
| 64 | + this.completableFuture.completeExceptionally(new RetryTimeoutException()); |
| 65 | + } else { |
| 66 | + LOGGER.debug( |
| 67 | + "Retryable exception ({}) for task '{}', scheduling another attempt", |
| 68 | + e.getClass().getSimpleName(), |
| 69 | + description); |
| 70 | + schedule( |
| 71 | + scheduler, retryableTaskReference.get(), delayPolicy.delay(attemptCount)); |
| 72 | + } |
| 73 | + } else { |
| 74 | + LOGGER.debug( |
| 75 | + "Non-retryable exception for task '{}', failing future", description); |
| 76 | + this.completableFuture.completeExceptionally(e); |
| 77 | + } |
71 | 78 | } |
72 | | - } else { |
73 | | - LOGGER.debug("Non-retryable exception for task '{}', failing future", description); |
74 | | - this.completableFuture.completeExceptionally(e); |
75 | | - } |
76 | | - } |
77 | | - }; |
| 79 | + }, |
| 80 | + description); |
78 | 81 | retryableTaskReference.set(retryableTask); |
79 | 82 | Duration initialDelay = delayPolicy.delay(attempts.getAndIncrement()); |
80 | 83 | LOGGER.debug("Scheduling task '{}' with policy {}", description, delayPolicy); |
81 | 84 | if (initialDelay.isZero()) { |
82 | 85 | retryableTask.run(); |
83 | 86 | } else { |
84 | | - scheduler.schedule( |
85 | | - retryableTaskReference.get(), initialDelay.toMillis(), TimeUnit.MILLISECONDS); |
| 87 | + schedule(scheduler, retryableTaskReference.get(), initialDelay); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static void schedule( |
| 92 | + ScheduledExecutorService scheduler, Runnable command, Duration delay) { |
| 93 | + try { |
| 94 | + scheduler.schedule(command, delay.toMillis(), TimeUnit.MILLISECONDS); |
| 95 | + } catch (RuntimeException e) { |
| 96 | + LOGGER.debug("Error while scheduling command", e); |
86 | 97 | } |
87 | 98 | } |
88 | 99 |
|
|
0 commit comments