Skip to content

Commit c02e546

Browse files
garyrussellartembilan
authored andcommitted
GH-299: Fix @recover with Class Level @retryable
Resolves #299 Caused by the fix for #244, recover methods should not be considered as retryable, with class level annotation. **cherry-pick to 1.3.x**
1 parent 47c1e52 commit c02e546

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

src/main/java/org/springframework/retry/annotation/AnnotationAwareRetryOperationsInterceptor.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private MethodInterceptor getDelegate(Object target, Method method) {
174174
MethodInterceptor interceptor = NULL_INTERCEPTOR;
175175
Retryable retryable = AnnotatedElementUtils.findMergedAnnotation(method, Retryable.class);
176176
if (retryable == null) {
177-
retryable = AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), Retryable.class);
177+
retryable = classLevelAnnotation(method, Retryable.class);
178178
}
179179
if (retryable == null) {
180180
retryable = findAnnotationOnTarget(target, method, Retryable.class);
@@ -203,7 +203,7 @@ private <A extends Annotation> A findAnnotationOnTarget(Object target, Method me
203203
Method targetMethod = target.getClass().getMethod(method.getName(), method.getParameterTypes());
204204
A retryable = AnnotatedElementUtils.findMergedAnnotation(targetMethod, annotation);
205205
if (retryable == null) {
206-
retryable = AnnotatedElementUtils.findMergedAnnotation(targetMethod.getDeclaringClass(), annotation);
206+
retryable = classLevelAnnotation(targetMethod, annotation);
207207
}
208208

209209
return retryable;
@@ -213,6 +213,17 @@ private <A extends Annotation> A findAnnotationOnTarget(Object target, Method me
213213
}
214214
}
215215

216+
/*
217+
* With a class level annotation, exclude @Recover methods.
218+
*/
219+
private <A extends Annotation> A classLevelAnnotation(Method method, Class<A> annotation) {
220+
A ann = AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), annotation);
221+
if (ann != null && AnnotatedElementUtils.findMergedAnnotation(method, Recover.class) != null) {
222+
ann = null;
223+
}
224+
return ann;
225+
}
226+
216227
private MethodInterceptor getStatelessInterceptor(Object target, Method method, Retryable retryable) {
217228
RetryTemplate template = createTemplate(retryable.listeners());
218229
template.setRetryPolicy(getRetryPolicy(retryable, true));
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.retry.annotation;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
28+
29+
/**
30+
* @author Gary Russell
31+
* @since 1.3.4
32+
*/
33+
@SpringJUnitConfig
34+
public class DontRetryRecovererTests {
35+
36+
@Test
37+
void dontRetry(@Autowired Service service) {
38+
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> service.foo("x")).withMessage("test");
39+
assertThat(service.getCallCount()).isEqualTo(3);
40+
assertThat(service.getRecoverCount()).isEqualTo(1);
41+
}
42+
43+
@Configuration
44+
@EnableRetry
45+
public static class Config {
46+
47+
@Bean
48+
Service service() {
49+
return new Service();
50+
}
51+
52+
}
53+
54+
@Retryable
55+
public static class Service {
56+
57+
int callCount;
58+
59+
int recoverCount;
60+
61+
public void foo(String in) {
62+
callCount++;
63+
throw new RuntimeException();
64+
}
65+
66+
@Recover
67+
public void recover(Exception ex, String in) {
68+
this.recoverCount++;
69+
throw new RuntimeException("test");
70+
}
71+
72+
public int getCallCount() {
73+
return callCount;
74+
}
75+
76+
public int getRecoverCount() {
77+
return recoverCount;
78+
}
79+
80+
}
81+
82+
}

0 commit comments

Comments
 (0)