Skip to content

Commit debc4ad

Browse files
authored
GH-22: Add @EnableRetry.order()
Fixes #22 Configure `@Retryable` before `@Transactional`, so we can handle transaction with stateless retries
1 parent 0b37cc0 commit debc4ad

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.context.annotation.EnableAspectJAutoProxy;
2626
import org.springframework.context.annotation.Import;
27+
import org.springframework.core.Ordered;
2728
import org.springframework.core.annotation.AliasFor;
2829

2930
/**
@@ -33,7 +34,8 @@
3334
* the annotations.
3435
*
3536
* @author Dave Syer
36-
* @since 2.0
37+
* @author Yanming Zhou
38+
* @since 1.1
3739
*
3840
*/
3941
@Target(ElementType.TYPE)
@@ -51,4 +53,13 @@
5153
@AliasFor(annotation = EnableAspectJAutoProxy.class)
5254
boolean proxyTargetClass() default false;
5355

56+
/**
57+
* Indicate the order in which the {@link RetryConfiguration} should be applied.
58+
* <p>
59+
* The default is {@link Ordered#LOWEST_PRECEDENCE} in order to run after all other
60+
* post-processors, so that it can add an advisor to existing proxies rather than
61+
* double-proxy.
62+
*/
63+
int order() default Ordered.LOWEST_PRECEDENCE;
64+
5465
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,9 +41,13 @@
4141
import org.springframework.beans.factory.ListableBeanFactory;
4242
import org.springframework.beans.factory.SmartInitializingSingleton;
4343
import org.springframework.beans.factory.config.BeanDefinition;
44+
import org.springframework.context.annotation.ImportAware;
4445
import org.springframework.context.annotation.Role;
4546
import org.springframework.core.OrderComparator;
47+
import org.springframework.core.annotation.AnnotationAttributes;
4648
import org.springframework.core.annotation.AnnotationUtils;
49+
import org.springframework.core.type.AnnotationMetadata;
50+
import org.springframework.lang.Nullable;
4751
import org.springframework.retry.RetryListener;
4852
import org.springframework.retry.backoff.Sleeper;
4953
import org.springframework.retry.interceptor.MethodArgumentsKeyGenerator;
@@ -63,14 +67,18 @@
6367
* @author Artem Bilan
6468
* @author Markus Heiden
6569
* @author Gary Russell
70+
* @author Yanming Zhou
6671
* @since 1.1
6772
*
6873
*/
6974
@SuppressWarnings("serial")
7075
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
7176
@Component
7277
public class RetryConfiguration extends AbstractPointcutAdvisor
73-
implements IntroductionAdvisor, BeanFactoryAware, InitializingBean, SmartInitializingSingleton {
78+
implements IntroductionAdvisor, BeanFactoryAware, InitializingBean, SmartInitializingSingleton, ImportAware {
79+
80+
@Nullable
81+
protected AnnotationAttributes enableRetry;
7482

7583
private AnnotationAwareRetryOperationsInterceptor advice;
7684

@@ -88,6 +96,12 @@ public class RetryConfiguration extends AbstractPointcutAdvisor
8896

8997
private BeanFactory beanFactory;
9098

99+
@Override
100+
public void setImportMetadata(AnnotationMetadata importMetadata) {
101+
this.enableRetry = AnnotationAttributes
102+
.fromMap(importMetadata.getAnnotationAttributes(EnableRetry.class.getName()));
103+
}
104+
91105
@Override
92106
public void afterPropertiesSet() throws Exception {
93107
this.retryContextCache = findBean(RetryContextCache.class);
@@ -98,8 +112,9 @@ public void afterPropertiesSet() throws Exception {
98112
retryableAnnotationTypes.add(Retryable.class);
99113
this.pointcut = buildPointcut(retryableAnnotationTypes);
100114
this.advice = buildAdvice();
101-
if (this.advice instanceof BeanFactoryAware) {
102-
((BeanFactoryAware) this.advice).setBeanFactory(this.beanFactory);
115+
this.advice.setBeanFactory(this.beanFactory);
116+
if (this.enableRetry != null) {
117+
setOrder(enableRetry.getNumber("order"));
103118
}
104119
}
105120

src/test/java/org/springframework/retry/annotation/EnableRetryTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,6 +57,7 @@
5757
* @author Gary Russell
5858
* @author Aldo Sinanaj
5959
* @author Henning Pöttker
60+
* @author Yanming Zhou
6061
* @since 1.1
6162
*/
6263
public class EnableRetryTests {
@@ -105,6 +106,15 @@ public void proxyTargetClass() {
105106
context.close();
106107
}
107108

109+
@Test
110+
public void order() {
111+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
112+
TestOrderConfiguration.class);
113+
RetryConfiguration config = context.getBean(RetryConfiguration.class);
114+
assertThat(config.getOrder()).isEqualTo(1);
115+
context.close();
116+
}
117+
108118
@Test
109119
public void marker() {
110120
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
@@ -348,6 +358,17 @@ public int getOrder() {
348358

349359
}
350360

361+
@Configuration
362+
@EnableRetry(order = 1)
363+
protected static class TestOrderConfiguration {
364+
365+
@Bean
366+
public Service service() {
367+
return new Service();
368+
}
369+
370+
}
371+
351372
@Configuration
352373
@EnableRetry
353374
protected static class TestConfiguration {

0 commit comments

Comments
 (0)