https://github.com/cmccarthyirl/retryaspect
RetryAspect is a lightweight Java library for adding automatic retry functionality to methods without using Spring. Easily integrate retry logic into your applications for handling transient errors
https://github.com/cmccarthyirl/retryaspect
annotations aop aspect-oriented-programming aspectj error-handling java java-library retry retry-logic weaving
Last synced: 8 days ago
JSON representation
RetryAspect is a lightweight Java library for adding automatic retry functionality to methods without using Spring. Easily integrate retry logic into your applications for handling transient errors
- Host: GitHub
- URL: https://github.com/cmccarthyirl/retryaspect
- Owner: cmccarthyIrl
- License: mit
- Created: 2024-06-15T16:21:15.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-12-02T21:24:06.000Z (6 months ago)
- Last Synced: 2025-12-05T20:52:58.848Z (6 months ago)
- Topics: annotations, aop, aspect-oriented-programming, aspectj, error-handling, java, java-library, retry, retry-logic, weaving
- Language: Java
- Homepage: https://central.sonatype.com/artifact/io.github.cmccarthyirl/retryaspect
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# RetryAspect
This `RetryAspect` class is an implementation of the retry mechanism in Java using Aspect-Oriented Programming (AOP). It
allows methods annotated with `@Retryable` to be executed multiple times in case of failure, with configurable retry
attempts and backoff delay.
## Features
- **Retry Mechanism**: Automatically retries a method execution if it fails, based on the configuration provided by
the `@Retryable` annotation.
- **Configurable Attempts and Delay**: Allows configuration of the maximum number of retry attempts and the delay
between retries.
- **Exception Handling**: Only retries for specified exceptions, providing fine-grained control over which failures
should trigger a retry.
## Usage
To use the `RetryAspect` class, follow these steps:
### 1. Annotate Methods with `@Retryable`
Annotate the methods you want to be retryable with the `@Retryable` annotation.
```java
public class MyService {
@Retryable(maxAttempts = 3, backoff = @Backoff(delay = 1000), include = {RuntimeException.class})
public void performTask() {
// Method implementation
}
}
```
In this example, `performTask` will make up to `3` total attempts (1 initial + 2 retries) with a delay of `1000` milliseconds between retries,
but only if a `RuntimeException` is thrown.
**Note:** Methods should be idempotent as they may be executed multiple times.
### 2. Configure the `@Retryable` Annotation
The `@Retryable` annotation allows you to configure the retry behavior for a method. It has the following attributes:
- **maxAttempts**: The maximum number of retry attempts (default is 3).
- **backoff**: Specifies the backoff policy, including the delay between retries.
- **include**: The exceptions that should trigger a retry.
```java
@Retryable(maxAttempts = 5, backoff = @Backoff(delay = 2000), include = {IOException.class})
public void unreliableMethod() {
// Method implementation
}
```
In this example, `unreliableMethod` will make up to `5` total attempts (1 initial + 4 retries) with a delay of `2000` milliseconds between retries,
but only if an `IOException` is thrown.
### Important Configuration Notes
- **maxAttempts**: Must be at least 1. Represents total attempts (initial + retries)
- **include**: Cannot be empty. Must specify at least one exception type to retry
- **delay**: Must be non-negative (in milliseconds)
- **multiplier**: Use values > 1.0 for exponential backoff, 1.0 for fixed delay
## Usage
To use the RetryAspect library in your project, include the following dependencies and plugins in your `pom.xml` file:
1. Add `retryaspect` as a dependency
```xml
...
io.github.cmccarthyirl
retryaspect
1.0.5
...
```
2. Configure the AspectJ Maven Plugin
```xml
org.codehaus.mojo
aspectj-maven-plugin
1.15.0
16.0
16.0
16.0
true
true
ignore
UTF-8
io.github.cmccarthyirl
retryaspect
compile
test-compile
```
## Contribute
Contributions are welcome! [Here's](https://github.com/cmccarthyIrl/RetryAspect/blob/master/CONTRIBUTING.md) how you can get started!!
## License
This project is licensed under the Apache License 2.0 - see the [LICENSE](https://github.com/cmccarthyIrl/RetryAspect/blob/master/LICENSE) file for details.