Skip to content

Commit f0d8135

Browse files
committed
Add RequestHandlerRetryAdvice.setBackOff()
The `RetryAdviceParser` uses a private constructor of the `org.springframework.core.retry.DefaultRetryPolicy` for almost nothing. It is better to propagate a `BackOff` bean definition down to the target `RequestHandlerRetryAdvice` API. * Add `RequestHandlerRetryAdvice.setBackOff()` and use it from the `RetryAdviceParser` instead of the `retryPolicy` property. * This also makes the end-user experience with `RequestHandlerRetryAdvice` better when we care really only about a `BackOff` Related to: spring-projects/spring-framework#35963
1 parent d9740d2 commit f0d8135

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/xml/RetryAdviceParser.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package org.springframework.integration.config.xml;
1818

19-
import java.time.Duration;
20-
import java.util.Set;
21-
2219
import org.w3c.dom.Element;
2320

2421
import org.springframework.beans.factory.support.AbstractBeanDefinition;
@@ -71,15 +68,7 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
7168
}
7269
}
7370

74-
BeanDefinitionBuilder retryPolicyBuilder =
75-
BeanDefinitionBuilder.genericBeanDefinition("org.springframework.core.retry.DefaultRetryPolicy")
76-
.addConstructorArgValue(Set.of())
77-
.addConstructorArgValue(Set.of())
78-
.addConstructorArgValue(null)
79-
.addConstructorArgValue(Duration.ZERO)
80-
.addConstructorArgValue(backOffBuilder.getBeanDefinition());
81-
82-
builder.addPropertyValue("retryPolicy", retryPolicyBuilder.getBeanDefinition());
71+
builder.addPropertyValue("backOff", backOffBuilder.getBeanDefinition());
8372
String recoveryChannelAttr = element.getAttribute("recovery-channel");
8473
if (StringUtils.hasText(recoveryChannelAttr)) {
8574
BeanDefinitionBuilder emsrBuilder =

spring-integration-core/src/main/java/org/springframework/integration/handler/advice/RequestHandlerRetryAdvice.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.integration.support.ErrorMessageUtils;
4040
import org.springframework.messaging.Message;
4141
import org.springframework.messaging.MessagingException;
42+
import org.springframework.util.backoff.BackOff;
4243
import org.springframework.util.backoff.BackOffExecution;
4344

4445
/**
@@ -99,13 +100,25 @@ public RequestHandlerRetryAdvice() {
99100
/**
100101
* Set a {@link RetryPolicy} to use.
101102
* Defaults to 3 attempts with no delay in between.
103+
* Mutually exclusive with {@link #setBackOff(BackOff)}.
102104
* @param retryPolicy the policy.
103105
*/
104106
public void setRetryPolicy(RetryPolicy retryPolicy) {
105107
this.retryPolicy = retryPolicy;
106108
this.retryTemplate.setRetryPolicy(this.retryPolicy);
107109
}
108110

111+
/**
112+
* Set a {@link BackOff} to be used in the internal {@link RetryPolicy} with rest options as defaults.
113+
* Mutually exclusive with {@link #setRetryPolicy(RetryPolicy)}.
114+
* @param backOff the back-off.
115+
* @since 7.0.1
116+
*/
117+
public void setBackOff(BackOff backOff) {
118+
this.retryPolicy = RetryPolicy.builder().backOff(backOff).build();
119+
this.retryTemplate.setRetryPolicy(this.retryPolicy);
120+
}
121+
109122
/**
110123
* Set a {@link RetryListener} to track retry cycle phases.
111124
* @param retryListener the listener to use.

0 commit comments

Comments
 (0)