Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion retry/src/main/java/io/micronaut/retry/RetryState.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ default RetryPredicate getRetryPredicate() {
}

/**
* @return The captured exception type (default to {@link RuntimeException}
* @return The captured exception type (default to {@link Exception}
*/
Class<? extends Throwable> getCapturedException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@
Class<? extends RetryPredicate> predicate() default DefaultRetryPredicate.class;

/**
* @return The capture exception types (defaults to RuntimeException)
* @return The capture exception types (defaults to Exception)
*/
Class<? extends Throwable> capturedException() default RuntimeException.class;
Class<? extends Throwable> capturedException() default Exception.class;

/**
* @return The jitter factor used to apply random deviation to retry delays
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public RetryState build() {
Class<? extends RetryPredicate> predicateClass = (Class<? extends RetryPredicate>) retry.classValue(PREDICATE).orElse(DefaultRetryPredicate.class);
RetryPredicate predicate = createPredicate(predicateClass, retry);
@SuppressWarnings("unchecked")
Class<? extends RuntimeException> capturedException = (Class<? extends RuntimeException>) retry
Class<? extends Exception> capturedException = (Class<? extends Exception>) retry
.classValue(CAPTURED_EXCEPTION)
.orElse(RuntimeException.class);
.orElse(Exception.class);

return new SimpleRetry(
attempts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,22 @@ class SimpleRetrySpec extends Specification {
context.stop()
}

void "test retry with exception captured by default"() {
given:
ApplicationContext context = ApplicationContext.run()
CounterService counterService = context.getBean(CounterService)

when:
counterService.getCountExceptionDefault()

then: "retry kicked in because the exception thrown is captured by default"
noExceptionThrown()
counterService.countExceptionDefault == counterService.countThreshold

cleanup:
context.stop()
}

void "test retry with value"() {
given:
ApplicationContext context = ApplicationContext.run()
Expand Down Expand Up @@ -397,6 +413,7 @@ class SimpleRetrySpec extends Specification {
int countPreThreshold = 3
int countThrowable = 0;
int countThrowableUncaptured = 0;
int countExceptionDefault = 0;

@Retryable(attempts = '5', delay = '5ms')
int getCountSync() {
Expand Down Expand Up @@ -544,5 +561,14 @@ class SimpleRetrySpec extends Specification {
}
return countThrowableUncaptured
}

@Retryable(attempts = '5', delay = '5ms')
Integer getCountExceptionDefault() {
countExceptionDefault++
if(countExceptionDefault < countThreshold - 1) {
throw new Exception()
}
return countExceptionDefault
}
}
}
2 changes: 1 addition & 1 deletion src/main/docs/guide/aop/retry.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ For example:

snippet::io.micronaut.docs.aop.retry.BookService[tags="simple", indent=0, title="Simple Retry Example"]

With the above example if the `listBooks()` method throws a RuntimeException, it is retried until the maximum number of attempts is reached.
With the above example if the `listBooks()` method throws an Exception, it is retried until the maximum number of attempts is reached.

The `multiplier` value of the `@Retryable` annotation can be used to configure a multiplier used to calculate the delay between retries, allowing exponential retry support.

Expand Down
Loading