Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ public RetryTopicConfiguration processAnnotation(String[] topics, Class<?> clazz

Integer attempts = resolveExpressionAsInteger(annotation.attempts(), "attempts", true);
if (attempts != null) {
if (attempts <= 0) {
throw new IllegalArgumentException("Retry attempts must be greater than 0, but got: " + attempts);
}
builder.maxAttempts(attempts);
}
Integer concurrency = resolveExpressionAsInteger(annotation.concurrency(), "concurrency", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willAnswer;
Expand Down Expand Up @@ -377,6 +378,38 @@ void shouldCreateExceptionBasedDltRoutingSpec(boolean isMethod) {
assertThat(destinationTopicProperties.get(2).suffix()).isEqualTo("-dlt");
}

@Test
void shouldThrowExceptionForZeroRetryAttempts() {
// setup
given(this.beanFactory.getBean(RetryTopicBeanNames.DEFAULT_KAFKA_TEMPLATE_BEAN_NAME, KafkaOperations.class))
.willReturn(kafkaOperationsFromDefaultName);
RetryableTopicAnnotationProcessor processor = new RetryableTopicAnnotationProcessor(beanFactory);

// given - then
assertThatIllegalArgumentException()
.isThrownBy(() -> processor.processAnnotation(topics, listenWithRetryAndDlt,
AnnotationUtils.findAnnotation(
ReflectionUtils.findMethod(InvalidRetryAttemptsFactory.class, "listenWithZeroAttempts"),
RetryableTopic.class), beanWithDlt))
.withMessage("Retry attempts must be greater than 0, but got: 0");
}

@Test
void shouldThrowExceptionForNegativeRetryAttempts() {
// setup
given(this.beanFactory.getBean(RetryTopicBeanNames.DEFAULT_KAFKA_TEMPLATE_BEAN_NAME, KafkaOperations.class))
.willReturn(kafkaOperationsFromDefaultName);
RetryableTopicAnnotationProcessor processor = new RetryableTopicAnnotationProcessor(beanFactory);

// given - then
assertThatIllegalArgumentException()
.isThrownBy(() -> processor.processAnnotation(topics, listenWithRetryAndDlt,
AnnotationUtils.findAnnotation(
ReflectionUtils.findMethod(InvalidRetryAttemptsFactory.class, "listenWithNegativeAttempts"),
RetryableTopic.class), beanWithDlt))
.withMessage("Retry attempts must be greater than 0, but got: -1");
}

static class RetryableTopicAnnotationFactory {

@KafkaListener
Expand Down Expand Up @@ -446,4 +479,19 @@ void listenWithRetry() {
static class RetryableTopicClassLevelAnnotationFactoryWithCustomDltRouting {
}

static class InvalidRetryAttemptsFactory {

@KafkaListener
@RetryableTopic(attempts = "0", kafkaTemplate = RetryableTopicAnnotationProcessorTests.kafkaTemplateName)
void listenWithZeroAttempts() {
// NoOps
}

@KafkaListener
@RetryableTopic(attempts = "-1", kafkaTemplate = RetryableTopicAnnotationProcessorTests.kafkaTemplateName)
void listenWithNegativeAttempts() {
// NoOps
}
}

}
Loading