-
-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Spring Modulith integration with SQS and SNS
- Loading branch information
Maciej Walkowiak
committed
Mar 3, 2024
1 parent
e189ea5
commit 2d6f487
Showing
29 changed files
with
870 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="https://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>io.awspring.cloud</groupId> | ||
<artifactId>spring-cloud-aws</artifactId> | ||
<version>3.1.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>spring-cloud-aws-modulith</artifactId> | ||
<name>Spring Cloud AWS - Spring Modulith Integrations</name> | ||
<description>Spring Cloud AWS - Spring Modulith Integrations</description> | ||
<packaging>pom</packaging> | ||
|
||
<modules> | ||
<module>spring-cloud-aws-modulith-events-sns</module> | ||
<module>spring-cloud-aws-modulith-events-sqs</module> | ||
</modules> | ||
</project> |
83 changes: 83 additions & 0 deletions
83
spring-cloud-aws-modulith/spring-cloud-aws-modulith-events-sns/pom.xml
This file contains 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,83 @@ | ||
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.awspring.cloud</groupId> | ||
<artifactId>spring-cloud-aws-modulith</artifactId> | ||
<version>3.1.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<name>Spring Cloud AWS Modulith - Events - SNS support</name> | ||
<artifactId>spring-cloud-aws-modulith-events-sns</artifactId> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.modulith</groupId> | ||
<artifactId>spring-modulith-api</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.modulith</groupId> | ||
<artifactId>spring-modulith-events-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.awspring.cloud</groupId> | ||
<artifactId>spring-cloud-aws-sns</artifactId> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
|
||
<dependency> | ||
<groupId>org.springframework.modulith</groupId> | ||
<artifactId>spring-modulith-starter-jdbc</artifactId> | ||
<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> |
77 changes: 77 additions & 0 deletions
77
...rc/main/java/io/awspring/cloud/modulith/events/sns/SnsEventExternalizerConfiguration.java
This file contains 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,77 @@ | ||
/* | ||
* Copyright 2013-2024 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 io.awspring.cloud.modulith.events.sns; | ||
|
||
import io.awspring.cloud.sns.core.SnsNotification; | ||
import io.awspring.cloud.sns.core.SnsOperations; | ||
import io.awspring.cloud.sns.core.SnsTemplate; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
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.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 | ||
* @author Oliver Drotbohm | ||
* @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); | ||
} | ||
|
||
operations.sendNotification(routing.getTarget(), builder.build()); | ||
|
||
return CompletableFuture.completedFuture(null); | ||
}); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...modulith-events-sns/src/main/java/io/awspring/cloud/modulith/events/sns/package-info.java
This file contains 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,5 @@ | ||
/** | ||
* AWS SNS event externalization support. | ||
*/ | ||
@org.springframework.lang.NonNullApi | ||
package io.awspring.cloud.modulith.events.sns; |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains 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 @@ | ||
io.awspring.cloud.modulith.events.sns.SnsEventExternalizerConfiguration |
59 changes: 59 additions & 0 deletions
59
...awspring/cloud/modulith/events/sns/SnsEventExternalizerConfigurationIntegrationTests.java
This file contains 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,59 @@ | ||
/* | ||
* Copyright 2013-2024 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 io.awspring.cloud.modulith.events.sns; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
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; | ||
|
||
/** | ||
* Integration tests for {@link SnsEventExternalizerConfiguration}. | ||
* | ||
* @author Maciej Walkowiak | ||
* @since 1.1 | ||
*/ | ||
class SnsEventExternalizerConfigurationIntegrationTests { | ||
|
||
@Test // GH-344 | ||
void registersExternalizerByDefault() { | ||
|
||
basicSetup().run(ctxt -> { | ||
assertThat(ctxt).hasSingleBean(DelegatingEventExternalizer.class); | ||
}); | ||
} | ||
|
||
@Test // GH-344 | ||
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)); | ||
} | ||
} |
Oops, something went wrong.