-
Notifications
You must be signed in to change notification settings - Fork 0
Event infrastructure improvements
This document describes how our application event infrastructure can be improved and harmonized with other recent message-related use cases.
This sections lists some code samples that represent what we might support.
Any public method in a spring-managed bean could react to an event by adding the @EventListener
annotation.
@EventListener
public void handle(OrderCreatedEvent event) { ... }
We should also be able to publish any arbitrary event and wrap that in some kind of GenericEvent
. In this case, the following may be used:
@EventListener
public void handle(JobExecution jobExecution) { ... }
The following features are under consideration
An event listener may be interested by only a subset of a given even type. If the content of the event is the only way to filter the candidates, we could use a SpEL expression. Something like
@EventListener(condition = "${#execution.getStatus() == Status.RUNNING}")
public void handleJobExecution(JobExecution execution) { ... }
A event listener may return an event or a simple payload in which case it’s wrapped in a generic event with the current instance as the source
@EventListener
public JobStatus handleJobExecution(JobExecution execution) { ... }
An event may be processed asynchronously by adding an @Async
annotation on the method declaration.
@EventListener @Async
public void processOrderCreatedEvent(OrderCreatedEvent event) { ... }
Certain kind of events should be processed if a certain condition is met and ideally this should be open enough for others to add their own. A typical example is SPR-12080 that requires that an event should be processed if the transaction has completed for instance.
Ideally an extra annotation should be added to the method and a handler registered in the context. This handler has a chance to determine when the event should be fired.
If multiple listeners are attached to the same event, they can be ordered. Something like:
@Order(42)
@EventListner
public void handle(OrderCreatedEvent event) { ... }
The ApplicationContext
is a central component in a typical Spring-based application so it could be used to fire new events. ApplicationEventPublisher
already exists but we probably need a different interface that does not stick to ApplicationEvent
.
-
SPR-12080: transaction bound application events
-
SPR-11622: annotation-based event listener
-
SPR-12410: event listener ordering
-
restbucks app using a tx-bound events prototype.
What's New in the Spring Framework
Building a distribution with dependencies
Migrating from earlier versions of the Spring Framework
Migrating to Spring Framework 5.x
Migrating to Spring Framework 4.x
Migrating to Spring Framework 3.x
Manually merging pull requests