From db1c2b813e6e80ef7182831d078e6930e18fc0ae Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Sun, 15 Oct 2023 17:13:45 +0200 Subject: [PATCH] =?UTF-8?q?GH-322=20-=20Move=20@ApplicationModuleListener?= =?UTF-8?q?=20into=20=E2=80=A6.modulith.events=20in=20spring-modulith-even?= =?UTF-8?q?ts-api.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modulith/ApplicationModuleListener.java | 17 +++--- ...abbitEventPublicationIntegrationTests.java | 2 +- .../spring-modulith-events-api/pom.xml | 7 +-- .../events/ApplicationModuleListener.java | 57 +++++++++++++++++++ .../spring-modulith-events-core/pom.xml | 6 -- .../support/DelegatingEventExternalizer.java | 2 +- .../support/EventExternalizationSupport.java | 2 +- .../JmsEventPublicationIntegrationTests.java | 2 +- .../inventory/InventoryManagement.java | 2 +- .../inventory/InventoryManagement.java | 2 +- .../inventory/InventoryManagement.java | 2 +- 11 files changed, 77 insertions(+), 24 deletions(-) create mode 100644 spring-modulith-events/spring-modulith-events-api/src/main/java/org/springframework/modulith/events/ApplicationModuleListener.java diff --git a/spring-modulith-api/src/main/java/org/springframework/modulith/ApplicationModuleListener.java b/spring-modulith-api/src/main/java/org/springframework/modulith/ApplicationModuleListener.java index 059d4574..744d8b03 100644 --- a/spring-modulith-api/src/main/java/org/springframework/modulith/ApplicationModuleListener.java +++ b/spring-modulith-api/src/main/java/org/springframework/modulith/ApplicationModuleListener.java @@ -28,18 +28,20 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.event.TransactionalEventListener; /** - * An {@link ApplicationModuleListener} is an {@link Async} Spring {@link TransactionalEventListener} that - * runs in a transaction itself. Thus, the annotation serves as syntactic sugar for the generally recommend setup to - * integrate application modules via events. The setup makes sure that an original business transaction completes - * successfully and the integration asynchronously runs in a transaction itself to decouple the integration as much as - * possible from the original unit of work. + * An {@link ApplicationModuleListener} is an {@link Async} Spring {@link TransactionalEventListener} that runs in a + * transaction itself. Thus, the annotation serves as syntactic sugar for the generally recommend setup to integrate + * application modules via events. The setup makes sure that an original business transaction completes successfully and + * the integration asynchronously runs in a transaction itself to decouple the integration as much as possible from the + * original unit of work. *

* It is advisable that you use these integration listeners in combination with the Spring Modulith Event Publication * Registry to make sure that the event publication does not get lost in case of an application or listener failure. * * @author Oliver Drotbohm - * @see Spring - * Modulith Event Publication Registry - Reference Documentation + * @deprecated since, 1.1. Prefer {@code org.springframework.modulith.events.ApplicationModuleListener} in + * {@code spring-modulith-events-api}. + * @see Spring Modulith + * Event Publication Registry - Reference Documentation */ @Async @Transactional(propagation = Propagation.REQUIRES_NEW) @@ -47,6 +49,7 @@ import org.springframework.transaction.event.TransactionalEventListener; @Documented @Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE }) @Retention(RetentionPolicy.RUNTIME) +@Deprecated(since = "1.1", forRemoval = true) public @interface ApplicationModuleListener { /** diff --git a/spring-modulith-events/spring-modulith-events-amqp/src/test/java/org/springframework/modulith/events/amqp/RabbitEventPublicationIntegrationTests.java b/spring-modulith-events/spring-modulith-events-amqp/src/test/java/org/springframework/modulith/events/amqp/RabbitEventPublicationIntegrationTests.java index fd4dfe65..c55c30c5 100644 --- a/spring-modulith-events/spring-modulith-events-amqp/src/test/java/org/springframework/modulith/events/amqp/RabbitEventPublicationIntegrationTests.java +++ b/spring-modulith-events/spring-modulith-events-amqp/src/test/java/org/springframework/modulith/events/amqp/RabbitEventPublicationIntegrationTests.java @@ -31,7 +31,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.testcontainers.service.connection.ServiceConnection; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Bean; -import org.springframework.modulith.ApplicationModuleListener; +import org.springframework.modulith.events.ApplicationModuleListener; import org.springframework.modulith.events.Externalized; import org.springframework.transaction.annotation.Transactional; import org.testcontainers.containers.RabbitMQContainer; diff --git a/spring-modulith-events/spring-modulith-events-api/pom.xml b/spring-modulith-events/spring-modulith-events-api/pom.xml index ba97f1f6..4d249178 100644 --- a/spring-modulith-events/spring-modulith-events-api/pom.xml +++ b/spring-modulith-events/spring-modulith-events-api/pom.xml @@ -18,14 +18,13 @@ - org.springframework.modulith - spring-modulith-api - ${project.version} + org.springframework + spring-context org.springframework - spring-context + spring-tx diff --git a/spring-modulith-events/spring-modulith-events-api/src/main/java/org/springframework/modulith/events/ApplicationModuleListener.java b/spring-modulith-events/spring-modulith-events-api/src/main/java/org/springframework/modulith/events/ApplicationModuleListener.java new file mode 100644 index 00000000..7bc7ada9 --- /dev/null +++ b/spring-modulith-events/spring-modulith-events-api/src/main/java/org/springframework/modulith/events/ApplicationModuleListener.java @@ -0,0 +1,57 @@ +/* + * Copyright 2022-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; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.core.annotation.AliasFor; +import org.springframework.scheduling.annotation.Async; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.event.TransactionalEventListener; + +/** + * An {@link ApplicationModuleListener} is an {@link Async} Spring {@link TransactionalEventListener} that runs in a + * transaction itself. Thus, the annotation serves as syntactic sugar for the generally recommend setup to integrate + * application modules via events. The setup makes sure that an original business transaction completes successfully and + * the integration asynchronously runs in a transaction itself to decouple the integration as much as possible from the + * original unit of work. + *

+ * It is advisable that you use these integration listeners in combination with the Spring Modulith Event Publication + * Registry to make sure that the event publication does not get lost in case of an application or listener failure. + * + * @author Oliver Drotbohm + * @see Spring Modulith + * Event Publication Registry - Reference Documentation + */ +@Async +@Transactional(propagation = Propagation.REQUIRES_NEW) +@TransactionalEventListener +@Documented +@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE }) +@Retention(RetentionPolicy.RUNTIME) +public @interface ApplicationModuleListener { + + /** + * Whether the transaction to be run for the event listener is supposed to be read-only (default {@literal false}). + */ + @AliasFor(annotation = Transactional.class, attribute = "readOnly") + boolean readOnlyTransaction() default false; +} diff --git a/spring-modulith-events/spring-modulith-events-core/pom.xml b/spring-modulith-events/spring-modulith-events-core/pom.xml index a78b4bf8..d4fadacb 100644 --- a/spring-modulith-events/spring-modulith-events-core/pom.xml +++ b/spring-modulith-events/spring-modulith-events-core/pom.xml @@ -18,12 +18,6 @@ - - org.springframework.modulith - spring-modulith-api - ${project.version} - - org.springframework.modulith spring-modulith-events-api diff --git a/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/DelegatingEventExternalizer.java b/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/DelegatingEventExternalizer.java index 9bcae04c..cd17b2d9 100644 --- a/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/DelegatingEventExternalizer.java +++ b/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/DelegatingEventExternalizer.java @@ -17,7 +17,7 @@ package org.springframework.modulith.events.support; import java.util.function.BiConsumer; -import org.springframework.modulith.ApplicationModuleListener; +import org.springframework.modulith.events.ApplicationModuleListener; import org.springframework.modulith.events.EventExternalizationConfiguration; import org.springframework.modulith.events.RoutingTarget; import org.springframework.stereotype.Component; diff --git a/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/EventExternalizationSupport.java b/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/EventExternalizationSupport.java index 197a7a85..3f26d93b 100644 --- a/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/EventExternalizationSupport.java +++ b/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/EventExternalizationSupport.java @@ -17,7 +17,7 @@ package org.springframework.modulith.events.support; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.modulith.ApplicationModuleListener; +import org.springframework.modulith.events.ApplicationModuleListener; import org.springframework.modulith.events.EventExternalizationConfiguration; import org.springframework.modulith.events.RoutingTarget; import org.springframework.modulith.events.core.ConditionalEventListener; diff --git a/spring-modulith-events/spring-modulith-events-jms/src/test/java/org/springframework/modulith/events/jms/JmsEventPublicationIntegrationTests.java b/spring-modulith-events/spring-modulith-events-jms/src/test/java/org/springframework/modulith/events/jms/JmsEventPublicationIntegrationTests.java index 4b6f3b57..18e55991 100644 --- a/spring-modulith-events/spring-modulith-events-jms/src/test/java/org/springframework/modulith/events/jms/JmsEventPublicationIntegrationTests.java +++ b/spring-modulith-events/spring-modulith-events-jms/src/test/java/org/springframework/modulith/events/jms/JmsEventPublicationIntegrationTests.java @@ -26,7 +26,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Bean; -import org.springframework.modulith.ApplicationModuleListener; +import org.springframework.modulith.events.ApplicationModuleListener; import org.springframework.modulith.events.Externalized; import org.springframework.transaction.annotation.Transactional; diff --git a/spring-modulith-examples/spring-modulith-example-epr-jdbc/src/main/java/example/inventory/InventoryManagement.java b/spring-modulith-examples/spring-modulith-example-epr-jdbc/src/main/java/example/inventory/InventoryManagement.java index 52d96746..3f84a48e 100644 --- a/spring-modulith-examples/spring-modulith-example-epr-jdbc/src/main/java/example/inventory/InventoryManagement.java +++ b/spring-modulith-examples/spring-modulith-example-epr-jdbc/src/main/java/example/inventory/InventoryManagement.java @@ -21,7 +21,7 @@ import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationEventPublisher; -import org.springframework.modulith.ApplicationModuleListener; +import org.springframework.modulith.events.ApplicationModuleListener; import org.springframework.stereotype.Service; /** diff --git a/spring-modulith-examples/spring-modulith-example-epr-mongodb/src/main/java/example/inventory/InventoryManagement.java b/spring-modulith-examples/spring-modulith-example-epr-mongodb/src/main/java/example/inventory/InventoryManagement.java index 52d96746..3f84a48e 100644 --- a/spring-modulith-examples/spring-modulith-example-epr-mongodb/src/main/java/example/inventory/InventoryManagement.java +++ b/spring-modulith-examples/spring-modulith-example-epr-mongodb/src/main/java/example/inventory/InventoryManagement.java @@ -21,7 +21,7 @@ import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationEventPublisher; -import org.springframework.modulith.ApplicationModuleListener; +import org.springframework.modulith.events.ApplicationModuleListener; import org.springframework.stereotype.Service; /** diff --git a/spring-modulith-examples/spring-modulith-example-epr-neo4j/src/main/java/example/inventory/InventoryManagement.java b/spring-modulith-examples/spring-modulith-example-epr-neo4j/src/main/java/example/inventory/InventoryManagement.java index 52d96746..3f84a48e 100644 --- a/spring-modulith-examples/spring-modulith-example-epr-neo4j/src/main/java/example/inventory/InventoryManagement.java +++ b/spring-modulith-examples/spring-modulith-example-epr-neo4j/src/main/java/example/inventory/InventoryManagement.java @@ -21,7 +21,7 @@ import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationEventPublisher; -import org.springframework.modulith.ApplicationModuleListener; +import org.springframework.modulith.events.ApplicationModuleListener; import org.springframework.stereotype.Service; /**