-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add Spring Cloud Stream retry support to ServiceBus Binder #47149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
19d7e38
Initial plan
Copilot f2c1549
Add Spring Cloud Stream retry support for ServiceBus Binder
Copilot 1a9398d
Add documentation for ServiceBus Binder retry configuration
Copilot 01e3bc4
Fix code style: remove extra blank line in setInstrumentationId
Copilot 9722515
Fix code style: replace double-brace initialization with explicit map…
Copilot 432cef9
Add support for custom RetryTemplate injection
Copilot 43a0e1c
Merge branch 'main' into copilot/fix-servicebus-backoff-settings
rujche 7af864f
Address PR feedback: remove retry doc file, fix changelog style, and …
Copilot f0be1c0
Fix changelog entry format in sdk/spring/CHANGELOG.md
Copilot fa45a5b
Merge branch 'main' into copilot/fix-servicebus-backoff-settings
rujche 463e525
Fix changelog headers and add retry execution test
Copilot 11d8ca6
Address trailing whitespace and derive retry defaults from ExtendedCo…
Copilot 2dc51cb
Fix CHANGELOG.md structure: remove empty top-level Features Added sec…
Copilot 1bf2489
Fix race condition and wire RetryTemplate from Spring context
Copilot e418dde
Add spring-retry direct dependency to binder pom and wire test
Copilot 5c5f7d0
Fix MockitoAnnotations resource leak: switch to @ExtendWith(MockitoEx…
Copilot 4207261
Simplify shouldConfigureRetry, fix retry+errorChannel, update tests
Copilot 6822a4e
Fix sendMessageDirectly false-return and shouldConfigureRetry semantics
Copilot 7f65c45
Fix misleading retry comment in ServiceBusMessageChannelBinder
Copilot 8961d7d
Replace Spring Retry reflection with public API and behavioral testing
Copilot 332f819
Replace ReflectionTestUtils with public getter for retryTemplate asse…
Copilot fae6b68
Address review: sendTimeout semantics, backoff reflection, exhausted-…
Copilot a123343
Clean up sendMessageDirectly, isolate binder per test, close context …
Copilot 6700e45
Fix unused import and qualify RetryTemplate injection to avoid ambiguity
Copilot 80c2c75
Merge origin/main into copilot/fix-servicebus-backoff-settings
Copilot af105d7
Merge origin/main: resolve CHANGELOG conflict, keep retry feature ent…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
...a/com/azure/spring/cloud/stream/binder/servicebus/implementation/ServiceBusRetryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
| package com.azure.spring.cloud.stream.binder.servicebus.implementation; | ||
|
|
||
| import com.azure.spring.cloud.service.servicebus.properties.ServiceBusEntityType; | ||
| import com.azure.spring.cloud.stream.binder.servicebus.core.implementation.provisioning.ServiceBusChannelProvisioner; | ||
| import com.azure.spring.cloud.stream.binder.servicebus.core.properties.ServiceBusBindingProperties; | ||
| import com.azure.spring.cloud.stream.binder.servicebus.core.properties.ServiceBusConsumerProperties; | ||
| import com.azure.spring.cloud.stream.binder.servicebus.core.properties.ServiceBusExtendedBindingProperties; | ||
| import com.azure.spring.integration.servicebus.inbound.ServiceBusInboundChannelAdapter; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.springframework.cloud.stream.binder.BinderHeaders; | ||
| import org.springframework.cloud.stream.binder.ExtendedConsumerProperties; | ||
| import org.springframework.cloud.stream.binder.HeaderMode; | ||
| import org.springframework.cloud.stream.provisioning.ConsumerDestination; | ||
| import org.springframework.context.support.GenericApplicationContext; | ||
| import org.springframework.integration.core.MessageProducer; | ||
| import org.springframework.retry.backoff.ExponentialBackOffPolicy; | ||
| import org.springframework.retry.support.RetryTemplate; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * Tests for retry functionality in ServiceBusMessageChannelBinder. | ||
| */ | ||
| @ExtendWith(MockitoExtension.class) | ||
| class ServiceBusRetryTest { | ||
|
|
||
| @Mock | ||
| private ConsumerDestination consumerDestination; | ||
|
|
||
| private final ServiceBusExtendedBindingProperties extendedBindingProperties = | ||
| new ServiceBusExtendedBindingProperties(); | ||
|
|
||
| private ExtendedConsumerProperties<ServiceBusConsumerProperties> consumerProperties; | ||
|
|
||
| private final ServiceBusConsumerProperties serviceBusConsumerProperties = new ServiceBusConsumerProperties(); | ||
|
|
||
| private ServiceBusMessageChannelTestBinder binder; | ||
|
|
||
| private GenericApplicationContext applicationContext; | ||
|
|
||
| private static final String ENTITY_NAME = "test-entity"; | ||
| private static final String GROUP = "test"; | ||
| private static final String NAMESPACE_NAME = "test-namespace"; | ||
|
|
||
| @BeforeEach | ||
| void init() { | ||
| binder = new ServiceBusMessageChannelTestBinder( | ||
| BinderHeaders.STANDARD_HEADERS, new ServiceBusChannelProvisioner()); | ||
| applicationContext = new GenericApplicationContext(); | ||
| applicationContext.refresh(); | ||
| binder.setApplicationContext(applicationContext); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| if (applicationContext != null) { | ||
| applicationContext.close(); | ||
| } | ||
| } | ||
|
rujche marked this conversation as resolved.
rujche marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| void testRetryTemplateConfiguredWhenMaxAttemptsGreaterThanOne() { | ||
| // Arrange | ||
| prepareConsumerProperties(); | ||
| consumerProperties.setMaxAttempts(3); | ||
| consumerProperties.setBackOffInitialInterval(1000); | ||
| consumerProperties.setBackOffMultiplier(2.0); | ||
| consumerProperties.setBackOffMaxInterval(5000); | ||
| when(consumerDestination.getName()).thenReturn(ENTITY_NAME); | ||
|
|
||
| // Act | ||
| MessageProducer producer = binder.createConsumerEndpoint(consumerDestination, GROUP, consumerProperties); | ||
|
|
||
| // Assert | ||
| assertThat(producer).isInstanceOf(ServiceBusInboundChannelAdapter.class); | ||
| ServiceBusInboundChannelAdapter adapter = (ServiceBusInboundChannelAdapter) producer; | ||
| RetryTemplate retryTemplate = adapter.getRetryTemplate(); | ||
| assertThat(retryTemplate).isNotNull(); | ||
|
|
||
| // Verify maxAttempts=3 by executing the template and counting actual attempts | ||
| AtomicInteger callCount = new AtomicInteger(0); | ||
| assertThatThrownBy(() -> retryTemplate.execute(ctx -> { | ||
| callCount.incrementAndGet(); | ||
| throw new RuntimeException("test"); | ||
| })).isInstanceOf(RuntimeException.class); | ||
| assertThat(callCount.get()).isEqualTo(3); | ||
|
|
||
| // Verify backoff policy configuration via the binder's factory method (no reflection needed) | ||
| ExponentialBackOffPolicy backOffPolicy = binder.createExponentialBackOffPolicy(consumerProperties); | ||
| assertThat(backOffPolicy.getInitialInterval()).isEqualTo(1000L); | ||
| assertThat(backOffPolicy.getMultiplier()).isEqualTo(2.0); | ||
| assertThat(backOffPolicy.getMaxInterval()).isEqualTo(5000L); | ||
| } | ||
|
rujche marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| void testRetryTemplateNotConfiguredWhenMaxAttemptsIsOne() { | ||
| // Arrange | ||
| prepareConsumerProperties(); | ||
| consumerProperties.setMaxAttempts(1); | ||
| when(consumerDestination.getName()).thenReturn(ENTITY_NAME); | ||
|
|
||
| // Act | ||
| MessageProducer producer = binder.createConsumerEndpoint(consumerDestination, GROUP, consumerProperties); | ||
|
|
||
| // Assert | ||
| assertThat(producer).isInstanceOf(ServiceBusInboundChannelAdapter.class); | ||
| assertThat(((ServiceBusInboundChannelAdapter) producer).getRetryTemplate()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void testRetryTemplateConfiguredWithDefaultSettings() { | ||
| // Arrange | ||
| prepareConsumerProperties(); | ||
| // Spring Cloud Stream default maxAttempts is 3 (> 1), so a RetryTemplate should be created. | ||
| when(consumerDestination.getName()).thenReturn(ENTITY_NAME); | ||
|
|
||
| // Act | ||
| MessageProducer producer = binder.createConsumerEndpoint(consumerDestination, GROUP, consumerProperties); | ||
|
|
||
| // Assert | ||
| assertThat(producer).isInstanceOf(ServiceBusInboundChannelAdapter.class); | ||
| ServiceBusInboundChannelAdapter adapter = (ServiceBusInboundChannelAdapter) producer; | ||
| RetryTemplate retryTemplate = adapter.getRetryTemplate(); | ||
| assertThat(retryTemplate).isNotNull(); | ||
|
|
||
| // Verify maxAttempts matches Spring Cloud Stream's default via observable behavior | ||
| int expectedMaxAttempts = new ExtendedConsumerProperties<>(new ServiceBusConsumerProperties()).getMaxAttempts(); | ||
| AtomicInteger callCount = new AtomicInteger(0); | ||
| assertThatThrownBy(() -> retryTemplate.execute(ctx -> { | ||
| callCount.incrementAndGet(); | ||
| throw new RuntimeException("test"); | ||
| })).isInstanceOf(RuntimeException.class); | ||
| assertThat(callCount.get()).isEqualTo(expectedMaxAttempts); | ||
| } | ||
|
rujche marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| void testCustomRetryTemplateIsUsed() { | ||
| // Arrange | ||
| prepareConsumerProperties(); | ||
| consumerProperties.setMaxAttempts(3); | ||
| when(consumerDestination.getName()).thenReturn(ENTITY_NAME); | ||
|
|
||
| // Create a custom RetryTemplate | ||
| RetryTemplate customRetryTemplate = new RetryTemplate(); | ||
| binder.setRetryTemplate(customRetryTemplate); | ||
|
|
||
| // Act | ||
| MessageProducer producer = binder.createConsumerEndpoint(consumerDestination, GROUP, consumerProperties); | ||
|
|
||
| // Assert | ||
| assertThat(producer).isInstanceOf(ServiceBusInboundChannelAdapter.class); | ||
| ServiceBusInboundChannelAdapter adapter = (ServiceBusInboundChannelAdapter) producer; | ||
| assertThat(adapter.getRetryTemplate()).isNotNull(); | ||
| assertThat(adapter.getRetryTemplate()).isSameAs(customRetryTemplate); | ||
| } | ||
|
|
||
| @Test | ||
| void testCustomRetryTemplateNotAppliedWhenMaxAttemptsIsOne() { | ||
| // Arrange: maxAttempts=1 disables retry even when a custom RetryTemplate bean is injected | ||
| prepareConsumerProperties(); | ||
| consumerProperties.setMaxAttempts(1); | ||
| when(consumerDestination.getName()).thenReturn(ENTITY_NAME); | ||
|
|
||
| RetryTemplate customRetryTemplate = new RetryTemplate(); | ||
| binder.setRetryTemplate(customRetryTemplate); | ||
|
|
||
| // Act | ||
| MessageProducer producer = binder.createConsumerEndpoint(consumerDestination, GROUP, consumerProperties); | ||
|
|
||
| // Assert | ||
| assertThat(producer).isInstanceOf(ServiceBusInboundChannelAdapter.class); | ||
| assertThat(((ServiceBusInboundChannelAdapter) producer).getRetryTemplate()).isNull(); | ||
| } | ||
|
|
||
| private void prepareConsumerProperties() { | ||
| serviceBusConsumerProperties.setEntityName(ENTITY_NAME); | ||
| serviceBusConsumerProperties.setSubscriptionName(GROUP); | ||
| serviceBusConsumerProperties.setEntityType(ServiceBusEntityType.TOPIC); | ||
| serviceBusConsumerProperties.setNamespace(NAMESPACE_NAME); | ||
| serviceBusConsumerProperties.getRetry().setTryTimeout(Duration.ofMinutes(5)); | ||
| serviceBusConsumerProperties.setAutoComplete(false); | ||
| ServiceBusBindingProperties bindingProperties = new ServiceBusBindingProperties(); | ||
| bindingProperties.setConsumer(serviceBusConsumerProperties); | ||
|
|
||
| Map<String, ServiceBusBindingProperties> bindings = new HashMap<>(); | ||
| bindings.put(ENTITY_NAME, bindingProperties); | ||
| extendedBindingProperties.setBindings(bindings); | ||
| binder.setBindingProperties(extendedBindingProperties); | ||
|
|
||
| consumerProperties = new ExtendedConsumerProperties<>(serviceBusConsumerProperties); | ||
| consumerProperties.setHeaderMode(HeaderMode.embeddedHeaders); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.