Skip to content

Commit e8be6d5

Browse files
committed
GH-734 - Only create event publication registry entries for after commit listeners.
1 parent 1700249 commit e8be6d5

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/PersistentApplicationEventMulticaster.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,16 @@ private void invokeTargetListener(TargetEventPublication publication) {
187187
.map(it -> executeListenerWithCompletion(publication, it)) //
188188
.orElseGet(() -> {
189189

190-
LOGGER.error("Listener {} not found! Skipping invocation and leaving event publication {} incomplete.", publication.getTargetIdentifier(), publication.getIdentifier());
190+
LOGGER.error("Listener {} not found! Skipping invocation and leaving event publication {} incomplete.",
191+
publication.getTargetIdentifier(), publication.getIdentifier());
191192
return null;
192193
});
193194
}
194195

195196
private void doResubmitUncompletedPublicationsOlderThan(@Nullable Duration duration,
196197
Predicate<EventPublication> filter) {
197198

198-
var message = duration != null ? " older than %s".formatted(duration): "";;
199+
var message = duration != null ? " older than %s".formatted(duration) : "";
199200
var registry = this.registry.get();
200201

201202
LOGGER.debug("Looking up incomplete event publications {}… ", message);
@@ -317,32 +318,11 @@ public TransactionalEventListeners(Collection<ApplicationListener<?>> listeners)
317318
this.listeners = (List) listeners.stream()
318319
.filter(TransactionalApplicationListener.class::isInstance)
319320
.map(TransactionalApplicationListener.class::cast)
321+
.filter(it -> it.getTransactionPhase().equals(TransactionPhase.AFTER_COMMIT))
320322
.sorted(AnnotationAwareOrderComparator.INSTANCE)
321323
.toList();
322324
}
323325

324-
private TransactionalEventListeners(
325-
List<TransactionalApplicationListener<ApplicationEvent>> listeners) {
326-
this.listeners = listeners;
327-
}
328-
329-
/**
330-
* Returns all {@link TransactionalEventListeners} for the given {@link TransactionPhase}.
331-
*
332-
* @param phase must not be {@literal null}.
333-
* @return will never be {@literal null}.
334-
*/
335-
public TransactionalEventListeners forPhase(TransactionPhase phase) {
336-
337-
Assert.notNull(phase, "TransactionPhase must not be null!");
338-
339-
List<TransactionalApplicationListener<ApplicationEvent>> collect = listeners.stream()
340-
.filter(it -> it.getTransactionPhase().equals(phase))
341-
.toList();
342-
343-
return new TransactionalEventListeners(collect);
344-
}
345-
346326
/**
347327
* Invokes the given {@link Consumer} for all transactional event listeners.
348328
*

spring-modulith-events/spring-modulith-events-core/src/test/java/org/springframework/modulith/events/support/PersistentApplicationEventMulticasterUnitTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import lombok.AllArgsConstructor;
2222

23+
import java.util.List;
2324
import java.util.Map;
2425

2526
import org.junit.jupiter.api.BeforeEach;
@@ -33,6 +34,8 @@
3334
import org.springframework.core.env.StandardEnvironment;
3435
import org.springframework.modulith.events.core.EventPublicationRegistry;
3536
import org.springframework.stereotype.Component;
37+
import org.springframework.transaction.event.TransactionPhase;
38+
import org.springframework.transaction.event.TransactionalApplicationListener;
3639
import org.springframework.transaction.event.TransactionalEventListener;
3740

3841
/**
@@ -87,6 +90,20 @@ void honorsListenerCondition() throws Exception {
8790
}
8891
}
8992

93+
@Test // GH-726
94+
void onlyConsidersAfterCommitListeners() {
95+
96+
var afterCommitListener = TransactionalApplicationListener.forPayload(TransactionPhase.AFTER_COMMIT, __ -> {});
97+
var beforeCommitListener = TransactionalApplicationListener.forPayload(TransactionPhase.BEFORE_COMMIT, __ -> {});
98+
99+
var eventListeners = new PersistentApplicationEventMulticaster.TransactionalEventListeners(
100+
List.of(afterCommitListener, beforeCommitListener));
101+
102+
assertThat(eventListeners.stream())
103+
.hasSize(1)
104+
.element(0).isEqualTo(afterCommitListener);
105+
}
106+
90107
private void assertListenerSelected(SampleEvent event, boolean expected) {
91108

92109
var listeners = multicaster.getApplicationListeners(new PayloadApplicationEvent<>(this, event),

0 commit comments

Comments
 (0)