Manage asynchronous EventListener with replies
This commit makes sure to reject an `@EventListener` annotated method that also uses `@Async`. In such scenario, the method is invoked in a separate thread and the infrastructure has no handle on the actual reply, if any. The documentation has been improved to refer to that scenario. Issue: SPR-14113
This commit is contained in:
@@ -8208,11 +8208,42 @@ method signature to return the event that should be published, something like:
|
||||
}
|
||||
----
|
||||
|
||||
NOTE: This feature is not supported for <<context-functionality-events-async,asynchronous
|
||||
listeners>>.
|
||||
|
||||
This new method will publish a new `ListUpdateEvent` for every `BlackListEvent` handled
|
||||
by the method above. If you need to publish several events, just return a `Collection` of
|
||||
events instead.
|
||||
|
||||
Finally if you need the listener to be invoked before another one, just add the `@Order`
|
||||
[[context-functionality-events-async]]
|
||||
==== Asynchronous Listeners
|
||||
|
||||
If you want a particular listener to process events asynchronously, simply reuse the
|
||||
<<scheduling-annotation-support-async,regular `@Async` support>>:
|
||||
|
||||
[source,java,indent=0]
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
@EventListener
|
||||
@Async
|
||||
public void processBlackListEvent(BlackListEvent event) {
|
||||
// BlackListEvent is processed in a separate thread
|
||||
}
|
||||
----
|
||||
|
||||
Be aware of the following limitations when using asynchronous events:
|
||||
|
||||
. If the event listener throws an `Exception` it will not be propagated to the caller,
|
||||
check `AsyncUncaughtExceptionHandler` for more details.
|
||||
. Such event listener cannot send replies. If you need to send another event as the
|
||||
result of the processing, inject `ApplicationEventPublisher` to send the event
|
||||
manually.
|
||||
|
||||
|
||||
[[context-functionality-events-order]
|
||||
==== Ordering Listeners
|
||||
|
||||
If you need the listener to be invoked before another one, just add the `@Order`
|
||||
annotation to the method declaration:
|
||||
|
||||
[source,java,indent=0]
|
||||
|
||||
Reference in New Issue
Block a user