|
44 | 44 | import org.springframework.kafka.support.ProducerListener; |
45 | 45 | import org.springframework.kafka.support.converter.RecordMessageConverter; |
46 | 46 | import org.springframework.kafka.transaction.KafkaTransactionManager; |
47 | | -import org.springframework.retry.backoff.BackOffPolicy; |
48 | 47 | import org.springframework.retry.backoff.BackOffPolicyBuilder; |
49 | 48 | import org.springframework.retry.backoff.SleepingBackOffPolicy; |
50 | 49 | import org.springframework.util.Assert; |
|
65 | 64 | @Import({ KafkaAnnotationDrivenConfiguration.class, KafkaStreamsAnnotationDrivenConfiguration.class }) |
66 | 65 | public class KafkaAutoConfiguration { |
67 | 66 |
|
| 67 | + private static final String RETRY_TOPIC_VALIDATION_ERROR_MSG = "Property spring.kafka.retry.topic.%s should be greater than or equal to %s. Provided value was %s."; |
| 68 | + |
68 | 69 | private final KafkaProperties properties; |
69 | 70 |
|
70 | 71 | public KafkaAutoConfiguration(KafkaProperties properties) { |
@@ -150,18 +151,32 @@ public KafkaAdmin kafkaAdmin() { |
150 | 151 | @ConditionalOnProperty(name = "spring.kafka.retry.topic.enabled") |
151 | 152 | public RetryTopicConfiguration kafkaRetryTopicConfiguration(KafkaOperations<Object, Object> kafkaOperations) { |
152 | 153 | KafkaProperties.Retry.Topic retryTopic = this.properties.getRetry().getTopic(); |
153 | | - return RetryTopicConfigurationBuilder.newInstance().maxAttempts(retryTopic.getAttempts()) |
154 | | - .customBackoff(getBackOffPolicy(retryTopic)).useSingleTopicForFixedDelays() |
155 | | - .suffixTopicsWithIndexValues().doNotAutoCreateRetryTopics().create(kafkaOperations); |
| 154 | + validateRetryTopicInput(retryTopic); |
| 155 | + RetryTopicConfigurationBuilder builder = RetryTopicConfigurationBuilder.newInstance() |
| 156 | + .maxAttempts(retryTopic.getAttempts()).useSingleTopicForFixedDelays().suffixTopicsWithIndexValues() |
| 157 | + .doNotAutoCreateRetryTopics(); |
| 158 | + setBackOffPolicy(builder, retryTopic); |
| 159 | + return builder.create(kafkaOperations); |
| 160 | + } |
| 161 | + |
| 162 | + private static void setBackOffPolicy(RetryTopicConfigurationBuilder builder, Topic retryTopic) { |
| 163 | + PropertyMapper.get().from(retryTopic.getDelayMillis()).whenEqualTo(0L).toCall(builder::noBackoff); |
| 164 | + PropertyMapper.get().from(retryTopic.getDelayMillis()).when((delay) -> delay > 0) |
| 165 | + .toCall(() -> builder.customBackoff((SleepingBackOffPolicy<?>) BackOffPolicyBuilder.newBuilder() |
| 166 | + .delay(retryTopic.getDelayMillis()).maxDelay(retryTopic.getMaxDelayMillis()) |
| 167 | + .multiplier(retryTopic.getMultiplier()).random(retryTopic.isRandomBackOff()).build())); |
| 168 | + } |
| 169 | + |
| 170 | + private static void validateRetryTopicInput(Topic retryTopic) { |
| 171 | + assertProperty("attempts", retryTopic.getAttempts() >= 1, 1, retryTopic.getMaxDelayMillis()); |
| 172 | + assertProperty("delay", retryTopic.getDelayMillis() >= 0, 0, retryTopic.getDelayMillis()); |
| 173 | + assertProperty("multiplier", retryTopic.getMultiplier() >= 0, 0, retryTopic.getMultiplier()); |
| 174 | + assertProperty("maxDelayMillis", retryTopic.getDelayMillis() >= 0, 0, retryTopic.getMaxDelayMillis()); |
156 | 175 | } |
157 | 176 |
|
158 | | - private SleepingBackOffPolicy<?> getBackOffPolicy(Topic retryTopic) { |
159 | | - BackOffPolicy policy = BackOffPolicyBuilder.newBuilder().delay(retryTopic.getDelayMillis()) |
160 | | - .maxDelay(retryTopic.getMaxDelayMillis()).multiplier(retryTopic.getMultiplier()) |
161 | | - .random(retryTopic.isRandomBackOff()).build(); |
162 | | - Assert.isInstanceOf(SleepingBackOffPolicy.class, policy, |
163 | | - () -> "BackOffPolicy must be an instance of SleepingBackOffPolicy. Provided: " + policy); |
164 | | - return (SleepingBackOffPolicy<?>) policy; |
| 177 | + private static void assertProperty(String propertyName, boolean condition, Object minValue, Object providedValue) { |
| 178 | + Assert.isTrue(condition, |
| 179 | + () -> String.format(RETRY_TOPIC_VALIDATION_ERROR_MSG, propertyName, minValue, providedValue)); |
165 | 180 | } |
166 | 181 |
|
167 | 182 | } |
0 commit comments