Skip to content

Commit e671eea

Browse files
garyrussellartembilan
authored andcommitted
GH-1274: Fix STCEH Deprecated CTOR
Fixes #1274 The deprecated CTORs taking `maxFailures` did not properly set up the `BackOff` when `maxFailures` was negative (infinite retries).
1 parent f77dcfb commit e671eea

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

spring-kafka/src/main/java/org/springframework/kafka/listener/FailedRecordProcessor.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,17 @@ protected FailedRecordProcessor(@Nullable BiConsumer<ConsumerRecord<?, ?>, Excep
6868
* @param maxFailures the max failures.
6969
*/
7070
FailedRecordProcessor(@Nullable BiConsumer<ConsumerRecord<?, ?>, Exception> recoverer, int maxFailures) {
71-
this.failureTracker = new FailedRecordTracker(recoverer, new FixedBackOff(0L, maxFailures - 1), LOGGER);
71+
this.failureTracker = new FailedRecordTracker(recoverer, maxFailuresToBackOff(maxFailures), LOGGER);
7272
this.classifier = configureDefaultClassifier();
7373
}
7474

75+
private static FixedBackOff maxFailuresToBackOff(int maxFailures) {
76+
if (maxFailures < 0) {
77+
return new FixedBackOff();
78+
}
79+
return new FixedBackOff(0L, maxFailures == 0 ? 0 : maxFailures - 1);
80+
}
81+
7582
/**
7683
* Return the exception classifier.
7784
* @return the classifier.

spring-kafka/src/test/java/org/springframework/kafka/listener/SeekToCurrentErrorHandlerTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import org.springframework.kafka.KafkaException;
3737
import org.springframework.kafka.support.serializer.DeserializationException;
38+
import org.springframework.kafka.test.utils.KafkaTestUtils;
3839

3940
/**
4041
* @author Gary Russell
@@ -79,4 +80,17 @@ consumer, mock(MessageListenerContainer.class)))
7980
inOrder.verifyNoMoreInteractions();
8081
}
8182

83+
@SuppressWarnings("deprecation")
84+
@Test
85+
public void testDeprecatedCtor() {
86+
SeekToCurrentErrorHandler handler = new SeekToCurrentErrorHandler(-1);
87+
assertThat(KafkaTestUtils.getPropertyValue(handler, "failureTracker.backOff.maxAttempts"))
88+
.isEqualTo(Long.MAX_VALUE);
89+
handler = new SeekToCurrentErrorHandler(0);
90+
assertThat(KafkaTestUtils.getPropertyValue(handler, "failureTracker.backOff.maxAttempts"))
91+
.isEqualTo(0L);
92+
handler = new SeekToCurrentErrorHandler(10);
93+
assertThat(KafkaTestUtils.getPropertyValue(handler, "failureTracker.backOff.maxAttempts"))
94+
.isEqualTo(9L);
95+
}
8296
}

0 commit comments

Comments
 (0)