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.
This commit is contained in:
Oliver Drotbohm
2023-04-23 23:56:23 +02:00
parent fbd2c2e66f
commit 29a5148dcb

View File

@@ -94,12 +94,8 @@ public class PersistentApplicationEventMulticaster extends AbstractApplicationEv
return;
}
var txListeners = new TransactionalEventListeners(listeners);
var eventToPersist = getEventToPersist(event);
registry.get().store(eventToPersist, txListeners.stream() //
.map(TransactionalApplicationListener::getListenerId) //
.map(PublicationTargetIdentifier::of));
new TransactionalEventListeners(listeners)
.ifPresent(it -> storePublications(it, getEventToPersist(event)));
for (ApplicationListener listener : listeners) {
listener.onApplicationEvent(event);
@@ -146,6 +142,15 @@ public class PersistentApplicationEventMulticaster extends AbstractApplicationEv
return listener;
}
private void storePublications(Stream<TransactionalApplicationListener<ApplicationEvent>> listeners,
Object eventToPersist) {
var identifiers = listeners.map(TransactionalApplicationListener::getListenerId) //
.map(PublicationTargetIdentifier::of);
registry.get().store(eventToPersist, identifiers);
}
private static Object getEventToPersist(ApplicationEvent event) {
return PayloadApplicationEvent.class.isInstance(event) //
@@ -257,5 +262,9 @@ public class PersistentApplicationEventMulticaster extends AbstractApplicationEv
.findFirst()
.ifPresent(callback);
}
public boolean hasListeners() {
return !listeners.isEmpty();
}
}
}