32
32
33
33
/**
34
34
* A basic implementation of {@link RetryOperations} that invokes and potentially
35
- * retries a {@link RetryCallback} based on a configured {@link RetryPolicy} and
36
- * {@link BackOff} policy.
35
+ * retries a {@link Retryable} operation based on a configured {@link RetryPolicy}
36
+ * and {@link BackOff} policy.
37
37
*
38
- * <p>By default, a callback will be invoked at most 3 times with a fixed backoff
39
- * of 1 second.
38
+ * <p>By default, a retryable operation will be invoked at most 3 times with a
39
+ * fixed backoff of 1 second.
40
40
*
41
41
* <p>A {@link RetryListener} can be {@linkplain #setRetryListener(RetryListener)
42
42
* registered} to intercept and inject behavior during key retry phases (before a
52
52
* @see RetryPolicy
53
53
* @see BackOff
54
54
* @see RetryListener
55
- * @see RetryCallback
55
+ * @see Retryable
56
56
*/
57
57
public class RetryTemplate implements RetryOperations {
58
58
@@ -128,55 +128,58 @@ public void setRetryListener(RetryListener retryListener) {
128
128
}
129
129
130
130
/**
131
- * Execute the supplied {@link RetryCallback } according to the configured
132
- * retry and backoff policies.
133
- * <p>If the callback succeeds, its result will be returned. Otherwise, a
134
- * {@link RetryException} will be thrown to the caller.
135
- * @param retryCallback the callback to call initially and retry if needed
131
+ * Execute the supplied {@link Retryable } according to the configured retry
132
+ * and backoff policies.
133
+ * <p>If the {@code Retryable} succeeds, its result will be returned. Otherwise,
134
+ * a {@link RetryException} will be thrown to the caller.
135
+ * @param retryable the {@code Retryable} to execute and retry if needed
136
136
* @param <R> the type of the result
137
- * @return the result of the callback , if any
137
+ * @return the result of the {@code Retryable} , if any
138
138
* @throws RetryException if the {@code RetryPolicy} is exhausted; exceptions
139
139
* encountered during retry attempts are available as suppressed exceptions
140
140
*/
141
141
@ Override
142
- public <R extends @ Nullable Object > R execute (RetryCallback <R > retryCallback ) throws RetryException {
143
- String callbackName = retryCallback .getName ();
142
+ public <R extends @ Nullable Object > R execute (Retryable <R > retryable ) throws RetryException {
143
+ String retryableName = retryable .getName ();
144
144
// Initial attempt
145
145
try {
146
- logger .debug (() -> "Preparing to execute callback '" + callbackName + "'" );
147
- R result = retryCallback .run ();
148
- logger .debug (() -> "Callback '" + callbackName + "' completed successfully" );
146
+ logger .debug (() -> "Preparing to execute retryable operation '%s'" . formatted ( retryableName ) );
147
+ R result = retryable .run ();
148
+ logger .debug (() -> "Retryable operation '%s' completed successfully" . formatted ( retryableName ) );
149
149
return result ;
150
150
}
151
151
catch (Throwable initialException ) {
152
152
logger .debug (initialException ,
153
- () -> "Execution of callback '" + callbackName + "' failed; initiating the retry process" );
153
+ () -> "Execution of retryable operation '%s' failed; initiating the retry process"
154
+ .formatted (retryableName ));
154
155
// Retry process starts here
155
156
RetryExecution retryExecution = this .retryPolicy .start ();
156
157
BackOffExecution backOffExecution = this .backOffPolicy .start ();
157
158
List <Throwable > suppressedExceptions = new ArrayList <>();
158
159
159
160
Throwable retryException = initialException ;
160
161
while (retryExecution .shouldRetry (retryException )) {
161
- logger .debug (() -> "Preparing to retry callback '" + callbackName + "'" );
162
+ logger .debug (() -> "Preparing to retry operation '%s'" . formatted ( retryableName ) );
162
163
try {
163
164
this .retryListener .beforeRetry (retryExecution );
164
- R result = retryCallback .run ();
165
+ R result = retryable .run ();
165
166
this .retryListener .onRetrySuccess (retryExecution , result );
166
- logger .debug (() -> "Callback '" + callbackName + "' completed successfully after retry" );
167
+ logger .debug (() -> "Retryable operation '%s' completed successfully after retry"
168
+ .formatted (retryableName ));
167
169
return result ;
168
170
}
169
171
catch (Throwable currentAttemptException ) {
170
172
this .retryListener .onRetryFailure (retryExecution , currentAttemptException );
171
173
try {
172
174
long duration = backOffExecution .nextBackOff ();
173
- logger .debug (() -> "Retry callback '" + callbackName + " ' failed due to '" +
174
- currentAttemptException .getMessage () + "'; backing off for " + duration + "ms" );
175
+ logger .debug (() -> "Retryable operation '%s ' failed due to '%s'; backing off for %dms"
176
+ . formatted ( retryableName , currentAttemptException .getMessage (), duration ) );
175
177
Thread .sleep (duration );
176
178
}
177
179
catch (InterruptedException interruptedException ) {
178
180
Thread .currentThread ().interrupt ();
179
- throw new RetryException ("Unable to back off for retry callback '" + callbackName + "'" ,
181
+ throw new RetryException (
182
+ "Unable to back off for retryable operation '%s'" .formatted (retryableName ),
180
183
interruptedException );
181
184
}
182
185
suppressedExceptions .add (currentAttemptException );
@@ -185,8 +188,9 @@ public void setRetryListener(RetryListener retryListener) {
185
188
}
186
189
// The RetryPolicy has exhausted at this point, so we throw a RetryException with the
187
190
// initial exception as the cause and remaining exceptions as suppressed exceptions.
188
- RetryException finalException = new RetryException ("Retry policy for callback '" + callbackName +
189
- "' exhausted; aborting execution" , initialException );
191
+ RetryException finalException = new RetryException (
192
+ "Retry policy for operation '%s' exhausted; aborting execution" .formatted (retryableName ),
193
+ initialException );
190
194
suppressedExceptions .forEach (finalException ::addSuppressed );
191
195
this .retryListener .onRetryPolicyExhaustion (retryExecution , finalException );
192
196
throw finalException ;
0 commit comments