Skip to content

Commit 14c9893

Browse files
travisrieglerwilkinsona
authored andcommitted
Group Kafka back-off properties
Kafka back-off policy properties "delay", "max-delay", "multiplier", and "random-back-off" are now defined in a common "backoff" group: - spring.kafka.retry.topic.backoff.delay - spring.kafka.retry.topic.backoff.maxDelay - spring.kafka.retry.topic.backoff.multiplier - spring.kafka.retry.topic.backoff.random See gh-41335
1 parent d07fe47 commit 14c9893

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;
@@ -1585,36 +1586,114 @@ public void setAttempts(int attempts) {
15851586
this.attempts = attempts;
15861587
}
15871588

1589+
@DeprecatedConfigurationProperty(replacement = "spring.kafka.retry.topic.backoff.delay", since = "3.4.0")
1590+
@Deprecated(since = "3.4.0", forRemoval = true)
15881591
public Duration getDelay() {
1589-
return this.delay;
1592+
return getBackoff().getDelay();
15901593
}
15911594

1595+
@Deprecated(since = "3.4.0", forRemoval = true)
15921596
public void setDelay(Duration delay) {
1593-
this.delay = delay;
1597+
getBackoff().setDelay(delay);
15941598
}
15951599

1600+
@DeprecatedConfigurationProperty(replacement = "spring.kafka.retry.topic.backoff.multiplier",
1601+
since = "3.4.0")
1602+
@Deprecated(since = "3.4.0", forRemoval = true)
15961603
public double getMultiplier() {
1597-
return this.multiplier;
1604+
return getBackoff().getMultiplier();
15981605
}
15991606

1607+
@Deprecated(since = "3.4.0", forRemoval = true)
16001608
public void setMultiplier(double multiplier) {
1601-
this.multiplier = multiplier;
1609+
getBackoff().setMultiplier(multiplier);
16021610
}
16031611

1612+
@DeprecatedConfigurationProperty(replacement = "spring.kafka.retry.topic.backoff.maxDelay", since = "3.4.0")
1613+
@Deprecated(since = "3.4.0", forRemoval = true)
16041614
public Duration getMaxDelay() {
1605-
return this.maxDelay;
1615+
return getBackoff().getMaxDelay();
16061616
}
16071617

1618+
@Deprecated(since = "3.4.0", forRemoval = true)
16081619
public void setMaxDelay(Duration maxDelay) {
1609-
this.maxDelay = maxDelay;
1620+
getBackoff().setMaxDelay(maxDelay);
16101621
}
16111622

1623+
@DeprecatedConfigurationProperty(replacement = "spring.kafka.retry.topic.backoff.random",
1624+
since = "3.4.0")
1625+
@Deprecated(since = "3.4.0", forRemoval = true)
16121626
public boolean isRandomBackOff() {
1613-
return this.randomBackOff;
1627+
return getBackoff().isRandom();
16141628
}
16151629

1630+
@Deprecated(since = "3.4.0", forRemoval = true)
16161631
public void setRandomBackOff(boolean randomBackOff) {
1617-
this.randomBackOff = randomBackOff;
1632+
getBackoff().setRandom(randomBackOff);
1633+
}
1634+
1635+
private final Backoff backoff = new Backoff();
1636+
1637+
public Backoff getBackoff() {
1638+
return this.backoff;
1639+
}
1640+
1641+
public static class Backoff {
1642+
1643+
/**
1644+
* Canonical backoff period. Used as an initial value in the exponential
1645+
* case, and as a minimum value in the uniform case.
1646+
*/
1647+
private Duration delay = Duration.ofSeconds(1);
1648+
1649+
/**
1650+
* Multiplier to use for generating the next backoff delay.
1651+
*/
1652+
private double multiplier = 0.0;
1653+
1654+
/**
1655+
* Maximum wait between retries. If less than the delay then the default
1656+
* of 30 seconds is applied.
1657+
*/
1658+
private Duration maxDelay = Duration.ZERO;
1659+
1660+
/**
1661+
* Whether to have the backoff delays.
1662+
*/
1663+
private boolean random = false;
1664+
1665+
public Duration getDelay() {
1666+
return this.delay;
1667+
}
1668+
1669+
public void setDelay(Duration delay) {
1670+
this.delay = delay;
1671+
}
1672+
1673+
public double getMultiplier() {
1674+
return this.multiplier;
1675+
}
1676+
1677+
public void setMultiplier(double multiplier) {
1678+
this.multiplier = multiplier;
1679+
}
1680+
1681+
public Duration getMaxDelay() {
1682+
return this.maxDelay;
1683+
}
1684+
1685+
public void setMaxDelay(Duration maxDelay) {
1686+
this.maxDelay = maxDelay;
1687+
}
1688+
1689+
public boolean isRandom() {
1690+
return this.random;
1691+
}
1692+
1693+
public void setRandom(boolean random) {
1694+
this.random = random;
1695+
}
1696+
16181697
}
16191698

16201699
}

0 commit comments

Comments
 (0)