-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Introduce minimal retry functionality as a core framework feature #34716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
spring-core/src/main/java/org/springframework/core/retry/RetryCallback.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2002-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.core.retry; | ||
|
||
/** | ||
* Callback interface for a retryable piece of code. Used in conjunction with {@link RetryOperations}. | ||
* | ||
* @author Mahmoud Ben Hassine | ||
* @since 7.0 | ||
* @param <R> the type of the result | ||
* @see RetryOperations | ||
*/ | ||
@FunctionalInterface | ||
public interface RetryCallback<R> { | ||
|
||
/** | ||
* Method to execute and retry if needed. | ||
* @return the result of the callback | ||
* @throws Throwable if an error occurs during the execution of the callback | ||
*/ | ||
R run() throws Throwable; | ||
|
||
/** | ||
* A unique logical name for this callback to distinguish retries around | ||
* business operations. | ||
* @return the name of the callback. Defaults to the class name. | ||
*/ | ||
default String getName() { | ||
return getClass().getName(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
spring-core/src/main/java/org/springframework/core/retry/RetryException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2002-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.core.retry; | ||
|
||
import java.io.Serial; | ||
|
||
/** | ||
* Exception class for exhausted retries. | ||
* | ||
* @author Mahmoud Ben Hassine | ||
* @since 7.0 | ||
* @see RetryOperations | ||
*/ | ||
public class RetryException extends Exception { | ||
|
||
@Serial | ||
private static final long serialVersionUID = 5439915454935047936L; | ||
|
||
/** | ||
* Create a new exception with a message. | ||
* @param message the exception's message | ||
*/ | ||
public RetryException(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* Create a new exception with a message and a cause. | ||
* @param message the exception's message | ||
* @param cause the exception's cause | ||
*/ | ||
public RetryException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
spring-core/src/main/java/org/springframework/core/retry/RetryExecution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2002-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.core.retry; | ||
|
||
/** | ||
* Strategy interface to define a retry execution. | ||
* | ||
* <p>Implementations may be stateful but do not need to be thread-safe. | ||
* | ||
* @author Mahmoud Ben Hassine | ||
* @since 7.0 | ||
*/ | ||
public interface RetryExecution { | ||
|
||
/** | ||
* Specify if the operation should be retried based on the given throwable. | ||
* @param throwable the exception that caused the operation to fail | ||
* @return {@code true} if the operation should be retried, {@code false} otherwise | ||
*/ | ||
boolean shouldRetry(Throwable throwable); | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
spring-core/src/main/java/org/springframework/core/retry/RetryListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2002-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.core.retry; | ||
|
||
import org.springframework.core.retry.support.CompositeRetryListener; | ||
|
||
/** | ||
* An extension point that allows to inject code during key retry phases. | ||
* | ||
* <p>Typically registered in a {@link RetryTemplate}, and can be composed using | ||
* a {@link CompositeRetryListener}. | ||
* | ||
* @author Mahmoud Ben Hassine | ||
* @since 7.0 | ||
* @see CompositeRetryListener | ||
*/ | ||
public interface RetryListener { | ||
|
||
/** | ||
* Called before every retry attempt. | ||
* @param retryExecution the retry execution | ||
*/ | ||
default void beforeRetry(RetryExecution retryExecution) { | ||
sbrannen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Called after the first successful retry attempt. | ||
* @param retryExecution the retry execution | ||
* @param result the result of the callback | ||
*/ | ||
default void onRetrySuccess(RetryExecution retryExecution, Object result) { | ||
} | ||
|
||
/** | ||
* Called every time a retry attempt fails. | ||
* @param retryExecution the retry execution | ||
* @param throwable the throwable thrown by the callback | ||
*/ | ||
default void onRetryFailure(RetryExecution retryExecution, Throwable throwable) { | ||
} | ||
|
||
/** | ||
* Called once the retry policy is exhausted. | ||
* @param retryExecution the retry execution | ||
* @param throwable the last throwable thrown by the callback | ||
*/ | ||
default void onRetryPolicyExhaustion(RetryExecution retryExecution, Throwable throwable) { | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
spring-core/src/main/java/org/springframework/core/retry/RetryOperations.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2002-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.core.retry; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
/** | ||
* Main entry point to the core retry functionality. Defines a set of retryable operations. | ||
* | ||
* <p>Implemented by {@link RetryTemplate}. Not often used directly, but a useful | ||
* option to enhance testability, as it can easily be mocked or stubbed. | ||
* | ||
* @author Mahmoud Ben Hassine | ||
* @since 7.0 | ||
* @see RetryTemplate | ||
*/ | ||
public interface RetryOperations { | ||
|
||
/** | ||
* Retry the given callback (according to the retry policy configured at the implementation level) | ||
* until it succeeds or eventually throw an exception if the retry policy is exhausted. | ||
* @param retryCallback the callback to call initially and retry if needed | ||
* @param <R> the type of the callback's result | ||
* @return the callback's result | ||
* @throws RetryException thrown if the retry policy is exhausted. All attempt exceptions | ||
* should be added as suppressed exceptions to the final exception. | ||
*/ | ||
<R extends @Nullable Object> R execute(RetryCallback<R> retryCallback) throws RetryException; | ||
|
||
} | ||
|
33 changes: 33 additions & 0 deletions
33
spring-core/src/main/java/org/springframework/core/retry/RetryPolicy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2002-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.core.retry; | ||
|
||
/** | ||
* Strategy interface to define a retry policy. | ||
* | ||
* @author Mahmoud Ben Hassine | ||
* @since 7.0 | ||
*/ | ||
public interface RetryPolicy { | ||
|
||
/** | ||
* Start a new retry execution. | ||
* @return a fresh {@link RetryExecution} ready to be used | ||
*/ | ||
RetryExecution start(); | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.