Skip to content
Open
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 @@ -209,7 +209,7 @@ protected RetryTemplate buildRetryTemplate(ConsumerProperties properties) {
}
RetryPolicy retryPolicy =
RetryPolicy.builder()
.maxRetries(properties.getMaxAttempts())
.maxRetries(Math.max(0, properties.getMaxAttempts() - 1))
.delay(Duration.ofMillis(properties.getBackOffInitialInterval()))
.multiplier(properties.getBackOffMultiplier())
.maxDelay(Duration.ofMillis(properties.getBackOffMaxInterval()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.springframework.cloud.stream.binder;

import org.junit.jupiter.api.Test;
import org.springframework.core.retry.RetryTemplate;

import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;

class MaxAttemptsRetryTests {

@Test
void maxAttemptsShouldNotExceedConfiguredAttempts() {

ConsumerProperties props = new ConsumerProperties();
props.setMaxAttempts(2);

TestBinder binder = new TestBinder();

RetryTemplate retryTemplate = binder.buildRetryTemplate(props);

AtomicInteger attempts = new AtomicInteger();

try {
retryTemplate.execute(() -> {
attempts.incrementAndGet();
throw new RuntimeException("fail");
});
} catch (Exception ignored) {
}

assertThat(attempts.get()).isEqualTo(2);
}

static class TestBinder extends AbstractBinder<Object, ConsumerProperties, ProducerProperties> {

@Override
protected Binding<Object> doBindConsumer(String name, String group, Object inboundBindTarget,
ConsumerProperties consumerProperties) {
return null;
}

@Override
protected Binding<Object> doBindProducer(String name, Object outboundBindTarget,
ProducerProperties producerProperties) {
return null;
}
}
}