Skip to content

Commit

Permalink
GH-212 - Invert default for event republication to false.
Browse files Browse the repository at this point in the history
This is to accommodate cluster setups and avoid multiple instances from resubmitting outstanding event publications concurrently.
  • Loading branch information
odrotbohm committed Aug 7, 2023
1 parent 4c145dc commit bb1f020
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down

0 comments on commit bb1f020

Please sign in to comment.