diff --git a/framework-docs/modules/ROOT/pages/core/beans/context-introduction.adoc b/framework-docs/modules/ROOT/pages/core/beans/context-introduction.adoc index 44e766609b21..16e9531eeaf5 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/context-introduction.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/context-introduction.adoc @@ -484,13 +484,14 @@ custom event (`BlockedListEvent` in the preceding example). This means that the You can register as many event listeners as you wish, but note that, by default, event listeners receive events synchronously. This means that the `publishEvent()` method blocks until all listeners have finished processing the event. One advantage of this -synchronous and single-threaded approach is that, when a listener receives an event, it -operates inside the transaction context of the publisher if a transaction context is -available. If another strategy for event publication becomes necessary, see the javadoc -for Spring's +synchronous and single-threaded approach is that, when a listener receives an event, +it operates inside the transaction context of the publisher if a transaction context +is available. If another strategy for event publication becomes necessary, e.g. +asynchronous event processing by default, see the javadoc for Spring's {api-spring-framework}/context/event/ApplicationEventMulticaster.html[`ApplicationEventMulticaster`] interface and {api-spring-framework}/context/event/SimpleApplicationEventMulticaster.html[`SimpleApplicationEventMulticaster`] -implementation for configuration options. +implementation for configuration options which can be applied to a custom +"applicationEventMulticaster" bean definition. The following example shows the bean definitions used to register and configure each of the classes above: @@ -510,6 +511,12 @@ the classes above: + + + + + + ---- Putting it all together, when the `sendEmail()` method of the `emailService` bean is @@ -849,6 +856,23 @@ Kotlin:: TIP: This works not only for `ApplicationEvent` but any arbitrary object that you send as an event. +Finally, as with classic `ApplicationListener` implementations, the actual multicasting +happens via a context-wide `ApplicationEventMulticaster` at runtime. By default, this is a +`SimpleApplicationEventMulticaster` with synchronous event publication in the caller thread. +This can be replaced/customized through an "applicationEventMulticaster" bean definition, +e.g. for processing all events asynchronously and/or for handling listener exceptions: + +[source,java,indent=0,subs="verbatim,quotes"] +---- + @Bean + ApplicationEventMulticaster applicationEventMulticaster() { + SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster(); + multicaster.setTaskExecutor(...); + multicaster.setErrorHandler(...); + return multicaster; + } +---- + [[context-functionality-resources]]