Skip to content

Event infrastructure improvements

Stéphane Nicoll edited this page Jan 28, 2015 · 9 revisions

This document describes how our application event infrastructure can be improved and harmonized with other recent message-related use cases.

Proposals

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) { ... }

Features

The following features are under consideration

Event qualifier using SpEL

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) { ... }

Reply pattern

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) { ... }

Asynchronous processing

An event may be processed asynchronously by adding an @Async annotation on the method declaration.

@EventListener @Async
public void processOrderCreatedEvent(OrderCreatedEvent event) { ... }

Event processing customizations

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.

Ordering

If multiple listeners are attached to the same event, they can be ordered. Something like:

@Order(42)
@EventListner
public void handle(OrderCreatedEvent event) { ... }

Firing events

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.

Resources

Clone this wiki locally