-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-344 - Support for event externalization into AWS SNS and SQS.
Additional event externalization implementations for AWS SNS and SQS. Original pull request: GH-350.
- Loading branch information
1 parent
ca32c29
commit 1b83474
Showing
19 changed files
with
860 additions
and
1 deletion.
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
96 changes: 96 additions & 0 deletions
96
spring-modulith-events/spring-modulith-events-aws-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,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> |
85 changes: 85 additions & 0 deletions
85
...n/java/org/springframework/modulith/events/aws/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,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; | ||
} | ||
}); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...vents-aws-sns/src/main/java/org/springframework/modulith/events/aws/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 @@ | ||
/** | ||
* SNS event externalization support. | ||
*/ | ||
@org.springframework.lang.NonNullApi | ||
package org.springframework.modulith.events.aws.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 @@ | ||
org.springframework.modulith.events.aws.sns.SnsEventExternalizerConfiguration |
64 changes: 64 additions & 0 deletions
64
...gframework/modulith/events/aws/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,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)); | ||
} | ||
} |
Oops, something went wrong.