Skip to content

Commit ce9656b

Browse files
Improve ExceptionClassifier JavaDoc
Also add assertions to the LCFC new methods to warn the user if they already set the blocking configurations.
1 parent 29ced81 commit ce9656b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ public boolean removeNotRetryableException(Class<? extends Exception> exceptionT
181181
* </ul>
182182
* All others will be retried, unless {@link #defaultFalse()} has been called.
183183
* @param exceptionType the exception type.
184-
* @return true if the removal was successful.
184+
* @return the classification of the exception if removal was successful;
185+
* null otherwise.
185186
* @since 2.8.4
186187
* @see #addNotRetryableExceptions(Class...)
187188
* @see #setClassifications(Map, boolean)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.kafka.retrytopic;
1818

1919
import java.time.Clock;
20+
import java.util.Arrays;
2021
import java.util.Comparator;
2122
import java.util.HashSet;
2223
import java.util.List;
@@ -173,6 +174,10 @@ public KafkaListenerContainerFactory<?> decorateFactoryWithoutSettingContainerPr
173174
*/
174175
public void setBlockingRetriesBackOff(BackOff blockingBackOff) {
175176
Assert.notNull(blockingBackOff, "The provided BackOff cannot be null");
177+
Assert.state(this.providedBlockingBackOff == null, () ->
178+
"Blocking retries back off has already been set. Current: "
179+
+ this.providedBlockingBackOff
180+
+ " You provided: " + blockingBackOff);
176181
this.providedBlockingBackOff = blockingBackOff;
177182
}
178183

@@ -187,6 +192,10 @@ public void setBlockingRetriesBackOff(BackOff blockingBackOff) {
187192
public final void setBlockingRetryableExceptions(Class<? extends Exception>... exceptionTypes) {
188193
Assert.notNull(exceptionTypes, "The exception types cannot be null");
189194
Assert.noNullElements(exceptionTypes, "The exception types cannot have null elements");
195+
Assert.state(this.blockingExceptionTypes == null,
196+
() -> "Blocking retryable exceptions have already been set."
197+
+ "Current ones: " + Arrays.toString(this.blockingExceptionTypes)
198+
+ " You provided: " + Arrays.toString(exceptionTypes));
190199
this.blockingExceptionTypes = exceptionTypes;
191200
}
192201

spring-kafka/src/test/java/org/springframework/kafka/retrytopic/ListenerContainerFactoryConfigurerTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.kafka.retrytopic;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2021
import static org.mockito.ArgumentMatchers.any;
2122
import static org.mockito.ArgumentMatchers.anyLong;
2223
import static org.mockito.ArgumentMatchers.eq;
@@ -61,15 +62,17 @@
6162
import org.springframework.kafka.listener.adapter.KafkaBackoffAwareMessageListenerAdapter;
6263
import org.springframework.kafka.support.Acknowledgment;
6364
import org.springframework.kafka.support.converter.ConversionException;
65+
import org.springframework.kafka.support.serializer.DeserializationException;
6466
import org.springframework.util.backoff.BackOff;
6567
import org.springframework.util.backoff.BackOffExecution;
68+
import org.springframework.util.backoff.FixedBackOff;
6669

6770
/**
6871
* @author Tomaz Fernandes
6972
* @since 2.7
7073
*/
7174
@ExtendWith(MockitoExtension.class)
72-
@SuppressWarnings({"unchecked", "rawtypes"})
75+
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
7376
class ListenerContainerFactoryConfigurerTests {
7477

7578
@Mock
@@ -447,6 +450,24 @@ void shouldUseGivenBackOffAndExceptions() {
447450

448451
}
449452

453+
454+
@Test
455+
void shouldThrowIfBackOffOrRetryablesAlreadySet() {
456+
// given
457+
BackOff backOff = new FixedBackOff();
458+
ListenerContainerFactoryConfigurer configurer =
459+
new ListenerContainerFactoryConfigurer(kafkaConsumerBackoffManager,
460+
deadLetterPublishingRecovererFactory, clock);
461+
configurer.setBlockingRetriesBackOff(backOff);
462+
configurer.setBlockingRetryableExceptions(IllegalArgumentException.class, IllegalStateException.class);
463+
464+
// when / then
465+
assertThatThrownBy(() -> configurer.setBlockingRetriesBackOff(backOff)).isInstanceOf(IllegalStateException.class);
466+
assertThatThrownBy(() -> configurer.setBlockingRetryableExceptions(ConversionException.class, DeserializationException.class))
467+
.isInstanceOf(IllegalStateException.class);
468+
}
469+
470+
450471
@Test
451472
void shouldCacheFactoryInstances() {
452473

0 commit comments

Comments
 (0)