-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document custom SimpleApplicationEventMulticaster setup
Closes gh-29996
- Loading branch information
Showing
1 changed file
with
29 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | |
<bean id="blockedListNotifier" class="example.BlockedListNotifier"> | ||
<property name="notificationAddress" value="[email protected]"/> | ||
</bean> | ||
<!-- optional: a custom ApplicationEventMulticaster definition --> | ||
<bean id="applicationEventMulticaster" class="org.springframework.context.event.SimpleApplicationEventMulticaster"> | ||
<property name="taskExecutor" ref="..."/> | ||
<property name="errorHandler" ref="..."/> | ||
</bean> | ||
---- | ||
|
||
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]] | ||
|