Skip to content

Commit 9ccd314

Browse files
GH-3476: Minor Code refactoring
Fixes: #3476 #3476 Following code is refactored. 1) `generateFromSleepingBackOffPolicy` method in `org.springframework.kafka.retrytopic.BackOffValuesGenerator` Removed the code to set the `maxBackOffPeriod`, if the BackOffPolicy is `UniformRandomBackOffPolicy`. This is not required since, when sleeper is added to the `UniformRandomBackOffPolicy` `maxBackOffPeriod` is not modified. This could be an issue with the earlier versions of spring-retry. But, latest version of spring-kafka 3.3 and spring-retry 2.0.8, this is not an issue. 2) Modified `addIfAbsent` in `org.springframework.kafka.retrytopic.ListenerContainerFactoryResolver.Cache` to use `putIfAbsent`, rather than explictly checking for the key, if it is not there than adding it to the `HashMap`.
1 parent 95b750a commit 9ccd314

File tree

2 files changed

+2
-11
lines changed

2 files changed

+2
-11
lines changed

spring-kafka/src/main/java/org/springframework/kafka/retrytopic/BackOffValuesGenerator.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.retry.backoff.NoBackOffPolicy;
2929
import org.springframework.retry.backoff.Sleeper;
3030
import org.springframework.retry.backoff.SleepingBackOffPolicy;
31-
import org.springframework.retry.backoff.UniformRandomBackOffPolicy;
3231
import org.springframework.retry.support.RetrySynchronizationManager;
3332

3433
/**
@@ -82,12 +81,6 @@ private List<Long> generateFromSleepingBackOffPolicy(int maxAttempts, BackOffPol
8281
BackoffRetainerSleeper sleeper = new BackoffRetainerSleeper();
8382
SleepingBackOffPolicy<?> retainingBackOffPolicy =
8483
((SleepingBackOffPolicy<?>) providedBackOffPolicy).withSleeper(sleeper);
85-
86-
// UniformRandomBackOffPolicy loses the max value when a sleeper is set.
87-
if (providedBackOffPolicy instanceof UniformRandomBackOffPolicy) {
88-
((UniformRandomBackOffPolicy) retainingBackOffPolicy)
89-
.setMaxBackOffPeriod(((UniformRandomBackOffPolicy) providedBackOffPolicy).getMaxBackOffPeriod());
90-
}
9184
BackOffContext backOffContext = retainingBackOffPolicy.start(RetrySynchronizationManager.getContext());
9285
IntStream.range(0, maxAttempts)
9386
.forEach(index -> retainingBackOffPolicy.backOff(backOffContext));

spring-kafka/src/main/java/org/springframework/kafka/retrytopic/ListenerContainerFactoryResolver.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2022 the original author or authors.
2+
* Copyright 2018-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -205,9 +205,7 @@ static class Cache {
205205
ConcurrentKafkaListenerContainerFactory<?, ?> resolvedFactory) {
206206
synchronized (this.cacheMap) {
207207
Key key = cacheKey(factoryFromKafkaListenerAnnotation, config);
208-
if (!this.cacheMap.containsKey(key)) {
209-
this.cacheMap.put(key, resolvedFactory);
210-
}
208+
this.cacheMap.putIfAbsent(key, resolvedFactory);
211209
return resolvedFactory;
212210
}
213211
}

0 commit comments

Comments
 (0)