Skip to content

Commit

Permalink
Backport: Introduce order for @EnableRetry
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Jun 25, 2023
1 parent 5146d0e commit 79f0df0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2023 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.
Expand All @@ -24,6 +24,8 @@

import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AliasFor;

/**
* Global enabler for <code>@Retryable</code> annotations in Spring beans. If this is
Expand All @@ -32,7 +34,8 @@
* the annotations.
*
* @author Dave Syer
* @since 2.0
* @author Yanming Zhou
* @since 1.1
*
*/
@Target(ElementType.TYPE)
Expand All @@ -47,6 +50,17 @@
* standard Java interface-based proxies. The default is {@code false}.
* @return whether to proxy or not to proxy the class
*/
@AliasFor(annotation = EnableAspectJAutoProxy.class)
boolean proxyTargetClass() default false;

/**
* Indicate the order in which the {@link RetryConfiguration} AOP <b>advice</b> should
* be applied.
* <p>
* The default is {@code Ordered.LOWEST_PRECEDENCE - 1} in order to make sure the
* advice is applied before other advices with {@link Ordered#LOWEST_PRECEDENCE} order
* (e.g. an advice responsible for {@code @Transactional} behavior).
*/
int order() default Ordered.LOWEST_PRECEDENCE - 1;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 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.
Expand Down Expand Up @@ -40,9 +40,12 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ImportAware;
import org.springframework.context.annotation.Role;
import org.springframework.core.OrderComparator;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.retry.RetryListener;
import org.springframework.retry.backoff.Sleeper;
import org.springframework.retry.interceptor.MethodArgumentsKeyGenerator;
Expand All @@ -62,14 +65,17 @@
* @author Dave Syer
* @author Artem Bilan
* @author Markus Heiden
* @author Yanming Zhou
* @since 1.1
*
*/
@SuppressWarnings("serial")
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Component
public class RetryConfiguration extends AbstractPointcutAdvisor
implements IntroductionAdvisor, BeanFactoryAware, InitializingBean {
implements IntroductionAdvisor, BeanFactoryAware, InitializingBean, ImportAware {

protected AnnotationAttributes enableRetry;

private Advice advice;

Expand All @@ -87,6 +93,12 @@ public class RetryConfiguration extends AbstractPointcutAdvisor

private BeanFactory beanFactory;

@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableRetry = AnnotationAttributes
.fromMap(importMetadata.getAnnotationAttributes(EnableRetry.class.getName()));
}

@Override
public void afterPropertiesSet() throws Exception {
this.retryContextCache = findBean(RetryContextCache.class);
Expand All @@ -101,6 +113,9 @@ public void afterPropertiesSet() throws Exception {
if (this.advice instanceof BeanFactoryAware) {
((BeanFactoryAware) this.advice).setBeanFactory(this.beanFactory);
}
if (this.enableRetry != null) {
setOrder(enableRetry.getNumber("order").intValue());
}
}

private <T> List<T> findBeans(Class<? extends T> type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2006-2023 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.
Expand Down Expand Up @@ -55,6 +55,7 @@
* @author Artem Bilan
* @author Gary Russell
* @author Aldo Sinanaj
* @author Yanming Zhou
* @since 1.1
*/
public class EnableRetryTests {
Expand Down Expand Up @@ -98,6 +99,15 @@ public void proxyTargetClass() {
context.close();
}

@Test
public void order() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
TestOrderConfiguration.class);
RetryConfiguration config = context.getBean(RetryConfiguration.class);
assertEquals(1, config.getOrder());
context.close();
}

@Test
public void marker() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
Expand Down Expand Up @@ -325,6 +335,17 @@ public int getOrder() {

}

@Configuration
@EnableRetry(order = 1)
protected static class TestOrderConfiguration {

@Bean
public Service service() {
return new Service();
}

}

@Configuration
@EnableRetry
protected static class TestConfiguration {
Expand Down

0 comments on commit 79f0df0

Please sign in to comment.