Browse Source

Polishing

pull/35990/head
Sam Brannen 1 week ago
parent
commit
68f8139206
  1. 21
      spring-context/src/test/java/org/springframework/resilience/ReactiveRetryInterceptorTests.java
  2. 28
      spring-context/src/test/java/org/springframework/resilience/RetryInterceptorTests.java
  3. 8
      spring-core/src/main/java/org/springframework/core/retry/RetryTemplate.java

21
spring-context/src/test/java/org/springframework/resilience/ReactiveRetryInterceptorTests.java

@ -70,12 +70,7 @@ class ReactiveRetryInterceptorTests {
@Test @Test
void withPostProcessorForMethod() { void withPostProcessorForMethod() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AnnotatedMethodBean proxy = getProxiedAnnotatedMethodBean();
bf.registerBeanDefinition("bean", new RootBeanDefinition(AnnotatedMethodBean.class));
RetryAnnotationBeanPostProcessor bpp = new RetryAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
AnnotatedMethodBean proxy = bf.getBean(AnnotatedMethodBean.class);
AnnotatedMethodBean target = (AnnotatedMethodBean) AopProxyUtils.getSingletonTarget(proxy); AnnotatedMethodBean target = (AnnotatedMethodBean) AopProxyUtils.getSingletonTarget(proxy);
assertThatIllegalStateException() assertThatIllegalStateException()
@ -329,13 +324,23 @@ class ReactiveRetryInterceptorTests {
return ex -> assertThat(ex).matches(Exceptions::isRetryExhausted, "is RetryExhaustedException"); return ex -> assertThat(ex).matches(Exceptions::isRetryExhausted, "is RetryExhaustedException");
} }
private static AnnotatedMethodBean getProxiedAnnotatedMethodBean() {
DefaultListableBeanFactory bf = createBeanFactoryFor(AnnotatedMethodBean.class);
return bf.getBean(AnnotatedMethodBean.class);
}
private static AnnotatedClassBean getProxiedAnnotatedClassBean() { private static AnnotatedClassBean getProxiedAnnotatedClassBean() {
DefaultListableBeanFactory bf = createBeanFactoryFor(AnnotatedClassBean.class);
return bf.getBean(AnnotatedClassBean.class);
}
private static DefaultListableBeanFactory createBeanFactoryFor(Class<?> beanClass) {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("bean", new RootBeanDefinition(AnnotatedClassBean.class)); bf.registerBeanDefinition("bean", new RootBeanDefinition(beanClass));
RetryAnnotationBeanPostProcessor bpp = new RetryAnnotationBeanPostProcessor(); RetryAnnotationBeanPostProcessor bpp = new RetryAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf); bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp); bf.addBeanPostProcessor(bpp);
return bf.getBean(AnnotatedClassBean.class); return bf;
} }

28
spring-context/src/test/java/org/springframework/resilience/RetryInterceptorTests.java

@ -99,11 +99,7 @@ class RetryInterceptorTests {
@Test @Test
void withPostProcessorForMethod() { void withPostProcessorForMethod() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = createBeanFactoryFor(AnnotatedMethodBean.class);
bf.registerBeanDefinition("bean", new RootBeanDefinition(AnnotatedMethodBean.class));
RetryAnnotationBeanPostProcessor bpp = new RetryAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
AnnotatedMethodBean proxy = bf.getBean(AnnotatedMethodBean.class); AnnotatedMethodBean proxy = bf.getBean(AnnotatedMethodBean.class);
AnnotatedMethodBean target = (AnnotatedMethodBean) AopProxyUtils.getSingletonTarget(proxy); AnnotatedMethodBean target = (AnnotatedMethodBean) AopProxyUtils.getSingletonTarget(proxy);
@ -113,11 +109,7 @@ class RetryInterceptorTests {
@Test @Test
void withPostProcessorForMethodWithInterface() { void withPostProcessorForMethodWithInterface() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = createBeanFactoryFor(AnnotatedMethodBeanWithInterface.class);
bf.registerBeanDefinition("bean", new RootBeanDefinition(AnnotatedMethodBeanWithInterface.class));
RetryAnnotationBeanPostProcessor bpp = new RetryAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
AnnotatedInterface proxy = bf.getBean(AnnotatedInterface.class); AnnotatedInterface proxy = bf.getBean(AnnotatedInterface.class);
AnnotatedMethodBeanWithInterface target = (AnnotatedMethodBeanWithInterface) AopProxyUtils.getSingletonTarget(proxy); AnnotatedMethodBeanWithInterface target = (AnnotatedMethodBeanWithInterface) AopProxyUtils.getSingletonTarget(proxy);
@ -179,11 +171,7 @@ class RetryInterceptorTests {
@Test @Test
void withPostProcessorForClass() { void withPostProcessorForClass() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = createBeanFactoryFor(AnnotatedClassBean.class);
bf.registerBeanDefinition("bean", new RootBeanDefinition(AnnotatedClassBean.class));
RetryAnnotationBeanPostProcessor bpp = new RetryAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
AnnotatedClassBean proxy = bf.getBean(AnnotatedClassBean.class); AnnotatedClassBean proxy = bf.getBean(AnnotatedClassBean.class);
AnnotatedClassBean target = (AnnotatedClassBean) AopProxyUtils.getSingletonTarget(proxy); AnnotatedClassBean target = (AnnotatedClassBean) AopProxyUtils.getSingletonTarget(proxy);
@ -324,6 +312,16 @@ class RetryInterceptorTests {
} }
private static DefaultListableBeanFactory createBeanFactoryFor(Class<?> beanClass) {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("bean", new RootBeanDefinition(beanClass));
RetryAnnotationBeanPostProcessor bpp = new RetryAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
return bf;
}
static class NonAnnotatedBean implements PlainInterface { static class NonAnnotatedBean implements PlainInterface {
int counter = 0; int counter = 0;

8
spring-core/src/main/java/org/springframework/core/retry/RetryTemplate.java

@ -155,13 +155,13 @@ public class RetryTemplate implements RetryOperations {
Throwable lastException = initialException; Throwable lastException = initialException;
while (this.retryPolicy.shouldRetry(lastException)) { while (this.retryPolicy.shouldRetry(lastException)) {
try { try {
long duration = backOffExecution.nextBackOff(); long sleepTime = backOffExecution.nextBackOff();
if (duration == BackOffExecution.STOP) { if (sleepTime == BackOffExecution.STOP) {
break; break;
} }
logger.debug(() -> "Backing off for %dms after retryable operation '%s'" logger.debug(() -> "Backing off for %dms after retryable operation '%s'"
.formatted(duration, retryableName)); .formatted(sleepTime, retryableName));
Thread.sleep(duration); Thread.sleep(sleepTime);
} }
catch (InterruptedException interruptedException) { catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();

Loading…
Cancel
Save