From 1d24ed86704bf3a05444bb63a8d4eb853e25a964 Mon Sep 17 00:00:00 2001 From: Andreas Ahlenstorf Date: Sat, 9 Nov 2024 19:09:41 +0100 Subject: [PATCH] Clarify event delivery behavior without active TX --- .../antora/modules/ROOT/pages/events.adoc | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/docs/antora/modules/ROOT/pages/events.adoc b/src/docs/antora/modules/ROOT/pages/events.adoc index 0398af97..02c06247 100644 --- a/src/docs/antora/modules/ROOT/pages/events.adoc +++ b/src/docs/antora/modules/ROOT/pages/events.adoc @@ -195,6 +195,41 @@ class InventoryManagement { ---- ====== +[CAUTION] +==== +Spring Modulith writes events published without an ongoing transaction to the event publication log, but Spring Framework does not deliver them. They will only be delivered when incomplete event publications are retried (see <>). To receive the events immediately, annotate the event listener with `@TransactionalEventListener(fallbackExecution = true)`: + +[tabs] +====== +Java:: ++ +[source, java, role="primary"] +---- +@Component +class InventoryManagement { + + @TransactionalEventListener(fallbackExecution = true) + @ApplicationModuleListener + void on(OrderCompleted event) { /* … */ } +} +---- +Kotlin:: ++ +[source, kotlin, role="secondary"] +---- +@Component +class InventoryManagement { + + @TransactionalEventListener(fallbackExecution = true) + @ApplicationModuleListener + fun on(event: OrderCompleted) { /* … */ } +} +---- +====== + +Please see Spring Framework's documentation on https://docs.spring.io/spring-framework/reference/data-access/transaction/event.html#page-title[Transaction-bound Events] for further information. +==== + [[publication-registry]] == The Event Publication Registry @@ -204,6 +239,8 @@ On event publication, it finds out about the transactional event listeners that .The transactional event listener arrangement before execution image::event-publication-registry-start.png[] +IMPORTANT: While Spring Framework continues to deliver events to listener solely annotated with `@EventListener`, Modulith's event publication registry ignores them and does not write them to the event publication log. Modulith only considers methods directly or indirectly annotated with `@TransactionalEventListener`. That includes `@ApplicationModuleListener`, which is annotated with `@TransactionalEventListener`. + Each transactional event listener is wrapped into an aspect that marks that log entry as completed if the execution of the listener succeeds. In case the listener fails, the log entry stays untouched so that retry mechanisms can be deployed depending on the application's needs. Automatic re-publication of the events can be enabled via the xref:appendix.adoc#configuration-properties[`spring.modulith.events.republish-outstanding-events-on-restart`] property.