Skip to content
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

Fixes gh-697 Enhance SqsAutoConfiguration to use an available ObjectM… #845

Closed
wants to merge 1 commit into from
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 @@ -30,6 +30,7 @@
import io.awspring.cloud.sqs.listener.interceptor.MessageInterceptor;
import io.awspring.cloud.sqs.operations.SqsTemplate;
import io.awspring.cloud.sqs.operations.SqsTemplateBuilder;
import io.awspring.cloud.sqs.support.converter.SqsMessagingMessageConverter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
Expand All @@ -52,10 +53,10 @@
* @since 3.0
*/
@AutoConfiguration
@ConditionalOnClass({ SqsAsyncClient.class, SqsBootstrapConfiguration.class })
@ConditionalOnClass({SqsAsyncClient.class, SqsBootstrapConfiguration.class})
@EnableConfigurationProperties(SqsProperties.class)
@Import(SqsBootstrapConfiguration.class)
@AutoConfigureAfter({ CredentialsProviderAutoConfiguration.class, RegionProviderAutoConfiguration.class })
@AutoConfigureAfter({CredentialsProviderAutoConfiguration.class, RegionProviderAutoConfiguration.class})
@ConditionalOnProperty(name = "spring.cloud.aws.sqs.enabled", havingValue = "true", matchIfMissing = true)
public class SqsAutoConfiguration {

Expand All @@ -68,27 +69,28 @@ public SqsAutoConfiguration(SqsProperties sqsProperties) {
@ConditionalOnMissingBean
@Bean
public SqsAsyncClient sqsAsyncClient(AwsClientBuilderConfigurer awsClientBuilderConfigurer,
ObjectProvider<AwsClientCustomizer<SqsAsyncClientBuilder>> configurer) {
ObjectProvider<AwsClientCustomizer<SqsAsyncClientBuilder>> configurer) {
return awsClientBuilderConfigurer
.configure(SqsAsyncClient.builder(), this.sqsProperties, configurer.getIfAvailable()).build();
.configure(SqsAsyncClient.builder(), this.sqsProperties, configurer.getIfAvailable()).build();
}

@ConditionalOnMissingBean
@Bean
public SqsTemplate sqsTemplate(SqsAsyncClient sqsAsyncClient, ObjectProvider<ObjectMapper> objectMapperProvider) {
SqsTemplateBuilder builder = SqsTemplate.builder().sqsAsyncClient(sqsAsyncClient);
objectMapperProvider
.ifAvailable(om -> builder.configureDefaultConverter(converter -> converter.setObjectMapper(om)));
.ifAvailable(om -> builder.configureDefaultConverter(converter -> converter.setObjectMapper(om)));
return builder.build();
}

@ConditionalOnMissingBean
@Bean
public SqsMessageListenerContainerFactory<Object> defaultSqsListenerContainerFactory(
ObjectProvider<SqsAsyncClient> sqsAsyncClient, ObjectProvider<AsyncErrorHandler<Object>> asyncErrorHandler,
ObjectProvider<ErrorHandler<Object>> errorHandler,
ObjectProvider<AsyncMessageInterceptor<Object>> asyncInterceptors,
ObjectProvider<MessageInterceptor<Object>> interceptors) {
ObjectProvider<SqsAsyncClient> sqsAsyncClient, ObjectProvider<AsyncErrorHandler<Object>> asyncErrorHandler,
ObjectProvider<ErrorHandler<Object>> errorHandler,
ObjectProvider<AsyncMessageInterceptor<Object>> asyncInterceptors,
ObjectProvider<MessageInterceptor<Object>> interceptors,
ObjectProvider<ObjectMapper> objectMapperProvider) {

SqsMessageListenerContainerFactory<Object> factory = new SqsMessageListenerContainerFactory<>();
factory.configure(this::configureContainerOptions);
Expand All @@ -97,6 +99,11 @@ public SqsMessageListenerContainerFactory<Object> defaultSqsListenerContainerFac
errorHandler.ifAvailable(factory::setErrorHandler);
interceptors.forEach(factory::addMessageInterceptor);
asyncInterceptors.forEach(factory::addMessageInterceptor);
objectMapperProvider.ifAvailable(objectMapper -> {
var messageConverter = new SqsMessagingMessageConverter();
messageConverter.setObjectMapper(objectMapper);
factory.configure(options -> options.messageConverter(messageConverter));
});
return factory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.assertj.core.api.InstanceOfAssertFactories.type;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.awspring.cloud.autoconfigure.ConfiguredAwsClient;
import io.awspring.cloud.autoconfigure.core.AwsAutoConfiguration;
import io.awspring.cloud.autoconfigure.core.AwsClientCustomizer;
Expand All @@ -34,6 +35,7 @@
import io.awspring.cloud.sqs.listener.errorhandler.AsyncErrorHandler;
import io.awspring.cloud.sqs.listener.interceptor.AsyncMessageInterceptor;
import io.awspring.cloud.sqs.operations.SqsTemplate;
import io.awspring.cloud.sqs.support.converter.SqsMessagingMessageConverter;
import java.net.URI;
import java.time.Duration;
import java.util.Map;
Expand All @@ -44,6 +46,8 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
Expand All @@ -61,21 +65,21 @@ class SqsAutoConfigurationTest {
private static final String CUSTOM_OBJECT_MAPPER_BEAN_NAME = "customObjectMapper";

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.cloud.aws.region.static:eu-west-1")
.withConfiguration(AutoConfigurations.of(RegionProviderAutoConfiguration.class,
CredentialsProviderAutoConfiguration.class, SqsAutoConfiguration.class,
AwsAutoConfiguration.class));
.withPropertyValues("spring.cloud.aws.region.static:eu-west-1")
.withConfiguration(AutoConfigurations.of(RegionProviderAutoConfiguration.class,
CredentialsProviderAutoConfiguration.class, SqsAutoConfiguration.class,
AwsAutoConfiguration.class));

@Test
void sqsAutoConfigurationIsDisabled() {
this.contextRunner.withPropertyValues("spring.cloud.aws.sqs.enabled:false")
.run(context -> assertThat(context).doesNotHaveBean(SqsAsyncClient.class));
.run(context -> assertThat(context).doesNotHaveBean(SqsAsyncClient.class));
}

@Test
void sqsAutoConfigurationIsDisabledWhenSqsModuleIsNotInClassPath() {
this.contextRunner.withClassLoader(new FilteredClassLoader(SqsBootstrapConfiguration.class))
.run(context -> assertThat(context).doesNotHaveBean(SqsAsyncClient.class));
.run(context -> assertThat(context).doesNotHaveBean(SqsAsyncClient.class));
}

@Test
Expand Down Expand Up @@ -123,44 +127,99 @@ void customSqsClientConfigurer() {
});
});
}
// @formatter:on

@Test
void configuresFactoryComponentsAndOptions() {
this.contextRunner
.withPropertyValues("spring.cloud.aws.sqs.enabled:true",
"spring.cloud.aws.sqs.listener.max-concurrent-messages:19",
"spring.cloud.aws.sqs.listener.max-messages-per-poll:8",
"spring.cloud.aws.sqs.listener.poll-timeout:6s")
.withUserConfiguration(CustomComponentsConfiguration.class).run(context -> {
assertThat(context).hasSingleBean(SqsMessageListenerContainerFactory.class);
SqsMessageListenerContainerFactory<?> factory = context
.getBean(SqsMessageListenerContainerFactory.class);
assertThat(factory)
.hasFieldOrProperty("errorHandler")
.extracting("asyncMessageInterceptors").asList().isNotEmpty();
assertThat(factory)
.extracting("containerOptionsBuilder")
.withPropertyValues("spring.cloud.aws.sqs.enabled:true",
"spring.cloud.aws.sqs.listener.max-concurrent-messages:19",
"spring.cloud.aws.sqs.listener.max-messages-per-poll:8",
"spring.cloud.aws.sqs.listener.poll-timeout:6s")
.withUserConfiguration(CustomComponentsConfiguration.class, ObjectMapperConfiguration.class)
.run(context -> {
assertThat(context).hasSingleBean(SqsMessageListenerContainerFactory.class);
var factory = context.getBean(SqsMessageListenerContainerFactory.class);
assertThat(factory).hasFieldOrProperty("errorHandler").extracting("asyncMessageInterceptors")
.asList().isNotEmpty();
assertThat(factory).extracting("containerOptionsBuilder")
.asInstanceOf(type(ContainerOptionsBuilder.class))
.extracting(ContainerOptionsBuilder::build)
.isInstanceOfSatisfying(ContainerOptions.class, options -> {
assertThat(options.getMaxConcurrentMessages()).isEqualTo(19);
assertThat(options.getMaxMessagesPerPoll()).isEqualTo(8);
assertThat(options.getPollTimeout()).isEqualTo(Duration.ofSeconds(6));
assertThat(options.getMessageConverter()).isInstanceOfSatisfying(
SqsMessagingMessageConverter.class,
messageConverter -> assertThat(messageConverter.getPayloadMessageConverter())
.isInstanceOfSatisfying(CompositeMessageConverter.class,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very long chain, perhaps if we use extracting instead of isInstanceOfSatisfying we could make it simpler?

compositeMessageConverter -> assertThat(
compositeMessageConverter.getConverters()).element(1)
.isInstanceOfSatisfying(
MappingJackson2MessageConverter.class,
jackson2MessageConverter -> assertThat(
jackson2MessageConverter
.getObjectMapper())
.isInstanceOfSatisfying(
ObjectMapper.class,
objectMapper -> {
assertThat(
objectMapper)
.isEqualTo(
context.getBean(
ObjectMapper.class));
assertThat(
objectMapper
.getRegisteredModuleIds())
.contains(
"jackson-datatype-jsr310");
}))));
});
});
}

@Test
void configuresFactoryComponentsAndOptionsWithDefaults() {
this.contextRunner.withPropertyValues("spring.cloud.aws.sqs.enabled:true").run(context -> {
assertThat(context).hasSingleBean(SqsMessageListenerContainerFactory.class);
var factory = context.getBean(SqsMessageListenerContainerFactory.class);
assertThat(factory).hasFieldOrProperty("errorHandler").extracting("asyncMessageInterceptors").asList()
.isEmpty();
assertThat(factory).extracting("containerOptionsBuilder").asInstanceOf(type(ContainerOptionsBuilder.class))
.extracting(ContainerOptionsBuilder::build)
.isInstanceOfSatisfying(ContainerOptions.class, options -> {
assertThat(options.getMaxConcurrentMessages()).isEqualTo(10);
assertThat(options.getMaxMessagesPerPoll()).isEqualTo(10);
assertThat(options.getPollTimeout()).isEqualTo(Duration.ofSeconds(10));
assertThat(options.getMessageConverter()).isInstanceOfSatisfying(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, please let's try to simplify this a bit.

SqsMessagingMessageConverter.class,
messageConverter -> assertThat(messageConverter.getPayloadMessageConverter())
.isInstanceOfSatisfying(CompositeMessageConverter.class,
compositeMessageConverter -> assertThat(compositeMessageConverter
.getConverters()).element(1).isInstanceOfSatisfying(
MappingJackson2MessageConverter.class,
jackson2MessageConverter -> assertThat(
jackson2MessageConverter.getObjectMapper())
.isInstanceOfSatisfying(
ObjectMapper.class,
objectMapper -> assertThat(
objectMapper
.getRegisteredModuleIds())
.isEmpty()))));
});
});
}
// @formatter:on

@Test
void configuresObjectMapper() {
this.contextRunner.withPropertyValues("spring.cloud.aws.sqs.enabled:true")
.withUserConfiguration(ObjectMapperConfiguration.class).run(context -> {
SqsListenerAnnotationBeanPostProcessor bpp = context
.getBean(SqsListenerAnnotationBeanPostProcessor.class);
ObjectMapper objectMapper = context.getBean(CUSTOM_OBJECT_MAPPER_BEAN_NAME, ObjectMapper.class);
assertThat(bpp).extracting("endpointRegistrar").asInstanceOf(type(EndpointRegistrar.class))
.extracting(EndpointRegistrar::getObjectMapper).isEqualTo(objectMapper);
});
.withUserConfiguration(ObjectMapperConfiguration.class).run(context -> {
SqsListenerAnnotationBeanPostProcessor bpp = context
.getBean(SqsListenerAnnotationBeanPostProcessor.class);
ObjectMapper objectMapper = context.getBean(CUSTOM_OBJECT_MAPPER_BEAN_NAME, ObjectMapper.class);
assertThat(bpp).extracting("endpointRegistrar").asInstanceOf(type(EndpointRegistrar.class))
.extracting(EndpointRegistrar::getObjectMapper).isEqualTo(objectMapper);
});
}

@Configuration(proxyBeanMethods = false)
Expand All @@ -185,7 +244,7 @@ static class ObjectMapperConfiguration {

@Bean(name = CUSTOM_OBJECT_MAPPER_BEAN_NAME)
ObjectMapper objectMapper() {
return new ObjectMapper();
return new ObjectMapper().registerModule(new JavaTimeModule());
}

}
Expand Down