Skip to content

Commit e057139

Browse files
committed
Group Kafka back-off properties
Kafka back-off policy properties "delay", "maxDelay", "multiplier", and "randomBackOff" are now grouped under a common prefix of "backoff": - spring.kafka.retry.topic.backoff.delay - spring.kafka.retry.topic.backoff.maxDelay - spring.kafka.retry.topic.backoff.multiplier - spring.kafka.retry.topic.backoff.random
1 parent 69630bb commit e057139

File tree

2 files changed

+94
-15
lines changed

2 files changed

+94
-15
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaAutoConfiguration.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3333
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
3434
import org.springframework.boot.autoconfigure.kafka.KafkaProperties.Jaas;
35-
import org.springframework.boot.autoconfigure.kafka.KafkaProperties.Retry.Topic;
35+
import org.springframework.boot.autoconfigure.kafka.KafkaProperties.Retry.Topic.Backoff;
3636
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3737
import org.springframework.boot.context.properties.PropertyMapper;
3838
import org.springframework.boot.ssl.SslBundles;
@@ -186,7 +186,7 @@ public RetryTopicConfiguration kafkaRetryTopicConfiguration(KafkaTemplate<?, ?>
186186
.useSingleTopicForSameIntervals()
187187
.suffixTopicsWithIndexValues()
188188
.doNotAutoCreateRetryTopics();
189-
setBackOffPolicy(builder, retryTopic);
189+
setBackOffPolicy(builder, retryTopic.getBackoff());
190190
return builder.create(kafkaTemplate);
191191
}
192192

@@ -214,15 +214,15 @@ private void applyKafkaConnectionDetailsForAdmin(Map<String, Object> properties,
214214
}
215215
}
216216

217-
private static void setBackOffPolicy(RetryTopicConfigurationBuilder builder, Topic retryTopic) {
218-
long delay = (retryTopic.getDelay() != null) ? retryTopic.getDelay().toMillis() : 0;
217+
private static void setBackOffPolicy(RetryTopicConfigurationBuilder builder, Backoff retryTopicBackoff) {
218+
long delay = (retryTopicBackoff.getDelay() != null) ? retryTopicBackoff.getDelay().toMillis() : 0;
219219
if (delay > 0) {
220220
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
221221
BackOffPolicyBuilder backOffPolicy = BackOffPolicyBuilder.newBuilder();
222222
map.from(delay).to(backOffPolicy::delay);
223-
map.from(retryTopic.getMaxDelay()).as(Duration::toMillis).to(backOffPolicy::maxDelay);
224-
map.from(retryTopic.getMultiplier()).to(backOffPolicy::multiplier);
225-
map.from(retryTopic.isRandomBackOff()).to(backOffPolicy::random);
223+
map.from(retryTopicBackoff.getMaxDelay()).as(Duration::toMillis).to(backOffPolicy::maxDelay);
224+
map.from(retryTopicBackoff.getMultiplier()).to(backOffPolicy::multiplier);
225+
map.from(retryTopicBackoff.isRandom()).to(backOffPolicy::random);
226226
builder.customBackoff((SleepingBackOffPolicy<?>) backOffPolicy.build());
227227
}
228228
else {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaProperties.java

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.kafka.common.serialization.StringSerializer;
3535

3636
import org.springframework.boot.context.properties.ConfigurationProperties;
37+
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
3738
import org.springframework.boot.context.properties.PropertyMapper;
3839
import org.springframework.boot.context.properties.source.MutuallyExclusiveConfigurationPropertiesException;
3940
import org.springframework.boot.convert.DurationUnit;
@@ -1644,36 +1645,114 @@ public void setAttempts(int attempts) {
16441645
this.attempts = attempts;
16451646
}
16461647

1648+
@DeprecatedConfigurationProperty(replacement = "spring.kafka.retry.topic.backoff.delay", since = "3.4.0")
1649+
@Deprecated(since = "3.4.0", forRemoval = true)
16471650
public Duration getDelay() {
1648-
return this.delay;
1651+
return getBackoff().getDelay();
16491652
}
16501653

1654+
@Deprecated(since = "3.4.0", forRemoval = true)
16511655
public void setDelay(Duration delay) {
1652-
this.delay = delay;
1656+
getBackoff().setDelay(delay);
16531657
}
16541658

1659+
@DeprecatedConfigurationProperty(replacement = "spring.kafka.retry.topic.backoff.multiplier",
1660+
since = "3.4.0")
1661+
@Deprecated(since = "3.4.0", forRemoval = true)
16551662
public double getMultiplier() {
1656-
return this.multiplier;
1663+
return getBackoff().getMultiplier();
16571664
}
16581665

1666+
@Deprecated(since = "3.4.0", forRemoval = true)
16591667
public void setMultiplier(double multiplier) {
1660-
this.multiplier = multiplier;
1668+
getBackoff().setMultiplier(multiplier);
16611669
}
16621670

1671+
@DeprecatedConfigurationProperty(replacement = "spring.kafka.retry.topic.backoff.maxDelay", since = "3.4.0")
1672+
@Deprecated(since = "3.4.0", forRemoval = true)
16631673
public Duration getMaxDelay() {
1664-
return this.maxDelay;
1674+
return getBackoff().getMaxDelay();
16651675
}
16661676

1677+
@Deprecated(since = "3.4.0", forRemoval = true)
16671678
public void setMaxDelay(Duration maxDelay) {
1668-
this.maxDelay = maxDelay;
1679+
getBackoff().setMaxDelay(maxDelay);
16691680
}
16701681

1682+
@DeprecatedConfigurationProperty(replacement = "spring.kafka.retry.topic.backoff.randomBackOff",
1683+
since = "3.4.0")
1684+
@Deprecated(since = "3.4.0", forRemoval = true)
16711685
public boolean isRandomBackOff() {
1672-
return this.randomBackOff;
1686+
return getBackoff().isRandom();
16731687
}
16741688

1689+
@Deprecated(since = "3.4.0", forRemoval = true)
16751690
public void setRandomBackOff(boolean randomBackOff) {
1676-
this.randomBackOff = randomBackOff;
1691+
getBackoff().setRandom(randomBackOff);
1692+
}
1693+
1694+
private final Backoff backoff = new Backoff();
1695+
1696+
public Backoff getBackoff() {
1697+
return this.backoff;
1698+
}
1699+
1700+
public static class Backoff {
1701+
1702+
/**
1703+
* Canonical backoff period. Used as an initial value in the exponential
1704+
* case, and as a minimum value in the uniform case.
1705+
*/
1706+
private Duration delay = Duration.ofSeconds(1);
1707+
1708+
/**
1709+
* Multiplier to use for generating the next backoff delay.
1710+
*/
1711+
private double multiplier = 0.0;
1712+
1713+
/**
1714+
* Maximum wait between retries. If less than the delay then the default
1715+
* of 30 seconds is applied.
1716+
*/
1717+
private Duration maxDelay = Duration.ZERO;
1718+
1719+
/**
1720+
* Whether to have the backoff delays.
1721+
*/
1722+
private boolean random = false;
1723+
1724+
public Duration getDelay() {
1725+
return this.delay;
1726+
}
1727+
1728+
public void setDelay(Duration delay) {
1729+
this.delay = delay;
1730+
}
1731+
1732+
public double getMultiplier() {
1733+
return this.multiplier;
1734+
}
1735+
1736+
public void setMultiplier(double multiplier) {
1737+
this.multiplier = multiplier;
1738+
}
1739+
1740+
public Duration getMaxDelay() {
1741+
return this.maxDelay;
1742+
}
1743+
1744+
public void setMaxDelay(Duration maxDelay) {
1745+
this.maxDelay = maxDelay;
1746+
}
1747+
1748+
public boolean isRandom() {
1749+
return this.random;
1750+
}
1751+
1752+
public void setRandom(boolean random) {
1753+
this.random = random;
1754+
}
1755+
16771756
}
16781757

16791758
}

0 commit comments

Comments
 (0)