Skip to content

Commit 29a5148

Browse files
committed
GH-181 - Delay lookup of EventPublicationRegistry as much as possible.
We now avoid to initialize the EventPublicationRegistry in the ApplicationEventMulticaster unless there is a transactional event listener interested in the event to be published. This should help us avoid premature initialization of the registry for application events published early in the application context bootstrap phase.
1 parent fbd2c2e commit 29a5148

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,8 @@ public void multicastEvent(ApplicationEvent event, ResolvableType eventType) {
9494
return;
9595
}
9696

97-
var txListeners = new TransactionalEventListeners(listeners);
98-
var eventToPersist = getEventToPersist(event);
99-
100-
registry.get().store(eventToPersist, txListeners.stream() //
101-
.map(TransactionalApplicationListener::getListenerId) //
102-
.map(PublicationTargetIdentifier::of));
97+
new TransactionalEventListeners(listeners)
98+
.ifPresent(it -> storePublications(it, getEventToPersist(event)));
10399

104100
for (ApplicationListener listener : listeners) {
105101
listener.onApplicationEvent(event);
@@ -146,6 +142,15 @@ private ApplicationListener<ApplicationEvent> executeListenerWithCompletion(Even
146142
return listener;
147143
}
148144

145+
private void storePublications(Stream<TransactionalApplicationListener<ApplicationEvent>> listeners,
146+
Object eventToPersist) {
147+
148+
var identifiers = listeners.map(TransactionalApplicationListener::getListenerId) //
149+
.map(PublicationTargetIdentifier::of);
150+
151+
registry.get().store(eventToPersist, identifiers);
152+
}
153+
149154
private static Object getEventToPersist(ApplicationEvent event) {
150155

151156
return PayloadApplicationEvent.class.isInstance(event) //
@@ -257,5 +262,9 @@ public void doWithListener(String identifier,
257262
.findFirst()
258263
.ifPresent(callback);
259264
}
265+
266+
public boolean hasListeners() {
267+
return !listeners.isEmpty();
268+
}
260269
}
261270
}

0 commit comments

Comments
 (0)