Logging
This commit is contained in:
1 parent
e3381e9748
commit
a4db897659
1 file changed
+29
-5
@@ -14,31 +14,55 @@ open class EventDispatcher(val eventStore: EventStore) {
|
|||||||
open fun dispatch(referenceId: UUID, events: List<Event>) {
|
open fun dispatch(referenceId: UUID, events: List<Event>) {
|
||||||
val derivedFromIds = events.mapNotNull { it.metadata.derivedFromId }.flatten().toSet()
|
val derivedFromIds = events.mapNotNull { it.metadata.derivedFromId }.flatten().toSet()
|
||||||
val deletedEventIds = events.filterIsInstance<DeleteEvent>().map { it.deletedEventId }
|
val deletedEventIds = events.filterIsInstance<DeleteEvent>().map { it.deletedEventId }
|
||||||
|
|
||||||
val candidates = events
|
val candidates = events
|
||||||
.filterNot { it is SignalEvent }
|
.filterNot { it is SignalEvent }
|
||||||
.filter { it.eventId !in derivedFromIds }
|
.filter { it.eventId !in derivedFromIds }
|
||||||
.filter { it.eventId !in deletedEventIds }
|
.filter { it.eventId !in deletedEventIds }
|
||||||
|
|
||||||
val effectiveHistory = events
|
val effectiveHistory = events
|
||||||
.filter { it.eventId !in deletedEventIds } // fjern slettede events
|
.filter { it.eventId !in deletedEventIds }
|
||||||
.filterNot { it is DeleteEvent } // fjern selve delete-eventet
|
.filterNot { it is DeleteEvent }
|
||||||
|
|
||||||
|
|
||||||
EventListenerRegistry.getListeners().forEach { listener ->
|
EventListenerRegistry.getListeners().forEach { listener ->
|
||||||
for (candidate in candidates) {
|
for (candidate in candidates) {
|
||||||
try {
|
try {
|
||||||
val result = listener.onEvent(candidate, effectiveHistory)
|
val result = listener.onEvent(candidate, effectiveHistory)
|
||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
|
validateReferenceId(result, listener)
|
||||||
eventStore.persist(result)
|
eventStore.persist(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e: SoftDispatchException.UnqualifiedEntryEventException) {
|
} catch (e: SoftDispatchException.UnqualifiedEntryEventException) {
|
||||||
log.debug("Soft-dispatch skip (unqualified entry): ${e.message}")
|
log.debug("Soft-dispatch skip (unqualified entry): ${e.message}")
|
||||||
|
|
||||||
} catch (e: SoftDispatchException.SkipListenerException) {
|
} catch (e: SoftDispatchException.SkipListenerException) {
|
||||||
log.debug("Soft-dispatch skip: ${e.message}")
|
log.debug("Soft-dispatch skip: ${e.message}")
|
||||||
|
|
||||||
} catch (e: SoftDispatchException) {
|
} catch (e: SoftDispatchException) {
|
||||||
log.warn("Soft-dispatch in ${listener::class.simpleName} for event ${e.eventType?.simpleName}: ${e.message}")
|
log.warn(
|
||||||
|
"Soft-dispatch in ${listener::class.simpleName} " +
|
||||||
|
"for event ${e.eventType?.simpleName}: ${e.message}"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* Validates that referenceId (a lateinit) is initialized.
|
||||||
|
* If not, throws a clear and actionable exception.
|
||||||
|
*/
|
||||||
|
private fun validateReferenceId(event: Event, listener: Any) {
|
||||||
|
try {
|
||||||
|
// Accessing lateinit will throw if not initialized
|
||||||
|
event.referenceId
|
||||||
|
} catch (e: UninitializedPropertyAccessException) {
|
||||||
|
throw IllegalStateException(
|
||||||
|
"Listener ${listener::class.simpleName} attempted to persist " +
|
||||||
|
"${event::class.simpleName} (${event.eventId}) without initializing referenceId"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user