From 97ca28fc677122ce02767a1c431b96855e5dfa25 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 12 Sep 2024 10:49:29 -0400 Subject: [PATCH] GH-188: Expose `@CircuitBreaker(recover)` attribute Fixes: #188 Issue link: https://github.com/spring-projects/spring-retry/issues/188 All the logic to determine a recover method is there. We are just missing the `CircuitBreaker(recover)` attribute with an `@AliasFor(annotation = Retryable.class)` --- .../retry/annotation/CircuitBreaker.java | 9 +++++++++ .../CircuitBreakerInterceptorStatisticsTests.java | 12 +++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/retry/annotation/CircuitBreaker.java b/src/main/java/org/springframework/retry/annotation/CircuitBreaker.java index a45f10f..31fc5f1 100644 --- a/src/main/java/org/springframework/retry/annotation/CircuitBreaker.java +++ b/src/main/java/org/springframework/retry/annotation/CircuitBreaker.java @@ -190,4 +190,13 @@ public @interface CircuitBreaker { */ boolean throwLastExceptionOnExhausted() default false; + /** + * Name of method in this class to use for recover. Method had to be marked with + * {@link Recover} annotation. + * @return the name of recover method + * @since 2.0.9 + */ + @AliasFor(annotation = Retryable.class) + String recover() default ""; + } diff --git a/src/test/java/org/springframework/retry/stats/CircuitBreakerInterceptorStatisticsTests.java b/src/test/java/org/springframework/retry/stats/CircuitBreakerInterceptorStatisticsTests.java index 7e60c9f..b56dd12 100644 --- a/src/test/java/org/springframework/retry/stats/CircuitBreakerInterceptorStatisticsTests.java +++ b/src/test/java/org/springframework/retry/stats/CircuitBreakerInterceptorStatisticsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2023 the original author or authors. + * Copyright 2006-2024 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. @@ -34,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author Dave Syer + * @author Artem Bilan * */ public class CircuitBreakerInterceptorStatisticsTests { @@ -106,7 +107,7 @@ public class CircuitBreakerInterceptorStatisticsTests { private RetryContext status; - @CircuitBreaker(label = "test", maxAttempts = 1) + @CircuitBreaker(label = "test", maxAttempts = 1, recover = "recover") public Object service(String input) throws Exception { this.status = RetrySynchronizationManager.getContext(); Integer attempts = (Integer) status.getAttribute("attempts"); @@ -122,11 +123,16 @@ public class CircuitBreakerInterceptorStatisticsTests { } @Recover - public Object recover() { + public Object recover(String input) { this.status.setAttribute(RECOVERED, true); return RECOVERED; } + @Recover + public Object anotherRecover(Object input) { + return null; + } + public boolean isOpen() { return this.status != null && this.status.getAttribute("open") == Boolean.TRUE; }