From bb1f02025b8184fa352f2aff42bd4b9541ead650 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Mon, 7 Aug 2023 21:19:16 +0200 Subject: [PATCH] GH-212 - Invert default for event republication to false. This is to accommodate cluster setups and avoid multiple instances from resubmitting outstanding event publications concurrently. --- .../PersistentApplicationEventMulticaster.java | 4 +--- .../META-INF/spring-configuration-metadata.json | 2 +- ...istentApplicationEventMulticasterUnitTests.java | 14 +++++++------- .../PersistentDomainEventIntegrationTest.java | 5 +++++ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/PersistentApplicationEventMulticaster.java b/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/PersistentApplicationEventMulticaster.java index 11fd8a09e..0292ccf2c 100644 --- a/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/PersistentApplicationEventMulticaster.java +++ b/spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/PersistentApplicationEventMulticaster.java @@ -15,7 +15,6 @@ */ package org.springframework.modulith.events.support; -import java.lang.reflect.Field; import java.util.Collection; import java.util.List; import java.util.function.Consumer; @@ -30,7 +29,6 @@ import org.springframework.context.PayloadApplicationEvent; import org.springframework.context.event.AbstractApplicationEventMulticaster; import org.springframework.context.event.ApplicationEventMulticaster; -import org.springframework.context.event.ApplicationListenerMethodAdapter; import org.springframework.core.ResolvableType; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.env.Environment; @@ -118,7 +116,7 @@ public void multicastEvent(ApplicationEvent event, ResolvableType eventType) { @Override public void afterSingletonsInstantiated() { - if (Boolean.FALSE.equals(environment.get().getProperty(REPUBLISH_ON_RESTART, Boolean.class))) { + if (!Boolean.TRUE.equals(environment.get().getProperty(REPUBLISH_ON_RESTART, Boolean.class))) { return; } diff --git a/spring-modulith-events/spring-modulith-events-core/src/main/resources/META-INF/spring-configuration-metadata.json b/spring-modulith-events/spring-modulith-events-core/src/main/resources/META-INF/spring-configuration-metadata.json index fce453967..392fa49d8 100644 --- a/spring-modulith-events/spring-modulith-events-core/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/spring-modulith-events/spring-modulith-events-core/src/main/resources/META-INF/spring-configuration-metadata.json @@ -10,7 +10,7 @@ "name": "spring.modulith.republish-outstanding-events-on-restart", "type": "java.lang.boolean", "description": "Whether to republish outstanding event publications on restarts of the application.", - "defaultValue": "true" + "defaultValue": "false" } ] } diff --git a/spring-modulith-events/spring-modulith-events-core/src/test/java/org/springframework/modulith/events/support/PersistentApplicationEventMulticasterUnitTests.java b/spring-modulith-events/spring-modulith-events-core/src/test/java/org/springframework/modulith/events/support/PersistentApplicationEventMulticasterUnitTests.java index 5ce8af2b2..73905f782 100644 --- a/spring-modulith-events/spring-modulith-events-core/src/test/java/org/springframework/modulith/events/support/PersistentApplicationEventMulticasterUnitTests.java +++ b/spring-modulith-events/spring-modulith-events-core/src/test/java/org/springframework/modulith/events/support/PersistentApplicationEventMulticasterUnitTests.java @@ -42,21 +42,21 @@ void setUp() { this.multicaster = new PersistentApplicationEventMulticaster(() -> registry, () -> environment); } - @Test // GH-240 - void doesNotRepublishEventsOnRestartIfExplicitlyDisabled() { - - var source = new MapPropertySource("test", - Map.of(PersistentApplicationEventMulticaster.REPUBLISH_ON_RESTART, "false")); - environment.getPropertySources().addFirst(source); + @Test // GH-240, GH-251 + void doesNotRepublishEventsOnRestartByDefault() { multicaster.afterSingletonsInstantiated(); verify(registry, never()).findIncompletePublications(); } - @Test // GH-240 + @Test // GH-240, GH-251 void triggersRepublicationIfExplicitlyEnabled() { + var source = new MapPropertySource("test", + Map.of(PersistentApplicationEventMulticaster.REPUBLISH_ON_RESTART, "true")); + environment.getPropertySources().addFirst(source); + multicaster.afterSingletonsInstantiated(); verify(registry).findIncompletePublications(); diff --git a/spring-modulith-events/spring-modulith-events-tests/src/test/java/example/events/PersistentDomainEventIntegrationTest.java b/spring-modulith-events/spring-modulith-events-tests/src/test/java/example/events/PersistentDomainEventIntegrationTest.java index 6600a9cc7..964cbb281 100644 --- a/spring-modulith-events/spring-modulith-events-tests/src/test/java/example/events/PersistentDomainEventIntegrationTest.java +++ b/spring-modulith-events/spring-modulith-events-tests/src/test/java/example/events/PersistentDomainEventIntegrationTest.java @@ -20,12 +20,15 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; +import java.util.Map; + import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.EventListener; +import org.springframework.core.env.MapPropertySource; import org.springframework.modulith.events.EventPublication; import org.springframework.modulith.events.EventPublicationRegistry; import org.springframework.modulith.events.PublicationTargetIdentifier; @@ -45,6 +48,8 @@ class PersistentDomainEventIntegrationTest { void exposesEventPublicationForFailedListener() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.getEnvironment().getPropertySources().addFirst( + new MapPropertySource("test", Map.of("spring.modulith.republish-outstanding-events-on-restart", "true"))); context.register(ApplicationConfiguration.class, InfrastructureConfiguration.class); context.refresh();