Skip to content

Commit

Permalink
GH-344 - Support for event externalization into AWS SNS and SQS.
Browse files Browse the repository at this point in the history
Additional event externalization implementations for AWS SNS and SQS.

Original pull request: GH-350.
  • Loading branch information
maciejwalkowiak authored and odrotbohm committed Nov 1, 2023
1 parent 2b4419f commit f41d337
Show file tree
Hide file tree
Showing 19 changed files with 860 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pom.xml
Expand Up @@ -42,7 +42,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.version>6.1.0-RC1</spring.version> <!-- For Javadoc links only -->
<spring-boot.version>3.2.0-RC1</spring-boot.version>

<spring-cloud-aws-bom.version>3.0.2</spring-cloud-aws-bom.version>
</properties>

<developers>
Expand Down Expand Up @@ -407,6 +407,13 @@ limitations under the License.
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-dependencies</artifactId>
<version>${spring-cloud-aws-bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
2 changes: 2 additions & 0 deletions spring-modulith-events/pom.xml
Expand Up @@ -16,6 +16,8 @@
<modules>
<module>spring-modulith-events-amqp</module>
<module>spring-modulith-events-api</module>
<module>spring-modulith-events-aws-sns</module>
<module>spring-modulith-events-aws-sqs</module>
<module>spring-modulith-events-core</module>
<module>spring-modulith-events-jackson</module>
<module>spring-modulith-events-jdbc</module>
Expand Down
96 changes: 96 additions & 0 deletions spring-modulith-events/spring-modulith-events-aws-sns/pom.xml
@@ -0,0 +1,96 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-events</artifactId>
<version>1.1.0-SNAPSHOT</version>
</parent>

<name>Spring Modulith - Events - AWS SNS support</name>
<artifactId>spring-modulith-events-aws-sns</artifactId>

<properties>
<module.name>org.springframework.modulith.events.aws.sns</module.name>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-events-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-sns</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<optional>true</optional>
</dependency>

<!-- Test dependencies -->

<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-starter-jdbc</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter-sns</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter-sqs</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>localstack</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
@@ -0,0 +1,85 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.modulith.events.aws.sns;

import io.awspring.cloud.sns.core.SnsNotification;
import io.awspring.cloud.sns.core.SnsOperations;
import io.awspring.cloud.sns.core.SnsTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.sns.model.InvalidParameterException;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.messaging.MessageDeliveryException;
import org.springframework.modulith.events.EventExternalizationConfiguration;
import org.springframework.modulith.events.config.EventExternalizationAutoConfiguration;
import org.springframework.modulith.events.support.BrokerRouting;
import org.springframework.modulith.events.support.DelegatingEventExternalizer;

/**
* Auto-configuration to set up a {@link DelegatingEventExternalizer} to externalize events to SNS.
*
* @author Maciej Walkowiak
* @since 1.1
*/
@AutoConfiguration
@AutoConfigureAfter(EventExternalizationAutoConfiguration.class)
@ConditionalOnClass(SnsTemplate.class)
@ConditionalOnProperty(name = "spring.modulith.events.externalization.enabled",
havingValue = "true",
matchIfMissing = true)
class SnsEventExternalizerConfiguration {

private static final Logger logger = LoggerFactory.getLogger(SnsEventExternalizerConfiguration.class);

@Bean
DelegatingEventExternalizer snsEventExternalizer(EventExternalizationConfiguration configuration,
SnsOperations operations, BeanFactory factory) {

logger.debug("Registering domain event externalization to SNS…");

var context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(factory));

return new DelegatingEventExternalizer(configuration, (target, payload) -> {

var routing = BrokerRouting.of(target, context);

var builder = SnsNotification.builder(payload);
var key = routing.getKey(payload);
// when routing key is set, SNS topic must be a FIFO topic
if (key != null) {
builder.groupId(key);
}
try {
operations.sendNotification(routing.getTarget(), builder.build());
} catch (MessageDeliveryException e) {
// message delivery may fail if groupId is set and topic is not a FIFO topic, or content based deduplication has not been set on topic attributes.
if (e.getCause() instanceof InvalidParameterException) {
logger.error("Failed to send notification to SNS topic {}:{}", routing.getTarget(), e.getCause().getMessage());
}
throw e;
}
});
}
}
@@ -0,0 +1,5 @@
/**
* SNS event externalization support.
*/
@org.springframework.lang.NonNullApi
package org.springframework.modulith.events.aws.sns;
@@ -0,0 +1 @@
org.springframework.modulith.events.aws.sns.SnsEventExternalizerConfiguration
@@ -0,0 +1,64 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.modulith.events.aws.sns;

import io.awspring.cloud.sns.core.SnsOperations;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.modulith.events.EventExternalizationConfiguration;
import org.springframework.modulith.events.support.DelegatingEventExternalizer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* Integration tests for {@link SnsEventExternalizerConfiguration}.
*
* @author Maciej Walkowiak
* @since 1.1
*/
class SnsEventExternalizerConfigurationIntegrationTests {

@Test // GH-342
void registersExternalizerByDefault() {

basicSetup()
.run(ctxt -> {
assertThat(ctxt).hasSingleBean(DelegatingEventExternalizer.class);
});
}

@Test // GH-342
void disablesExternalizationIfConfigured() {

basicSetup()
.withPropertyValues("spring.modulith.events.externalization.enabled=false")
.run(ctxt -> {
assertThat(ctxt).doesNotHaveBean(DelegatingEventExternalizer.class);
});
}

private ApplicationContextRunner basicSetup() {

return new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(SnsEventExternalizerConfiguration.class))
.withBean(EventExternalizationConfiguration.class, () -> EventExternalizationConfiguration.disabled())
.withBean(SnsOperations.class, () -> mock(SnsOperations.class));
}
}

0 comments on commit f41d337

Please sign in to comment.