Simplify Spring Event Listeners with @EventListener and Async Configuration
This guide explains how Spring 4.2+ simplifies event handling using @EventListener, demonstrates creating and publishing events, covers annotation attributes, execution order, async execution with @Async, and shows Spring Boot async configuration based on the mica project, complete with code examples and configuration details.
Explanation
This article uses Spring 4.2+ as an example (earlier versions differ slightly). The author has been using Spring events since 2014 and created JFinal-event, which mirrors Spring's usage to simplify listener implementation.
Usage
Creating Event Listener
In Spring 4.2+ you no longer need separate listener classes; simply annotate a Spring bean method with @EventListener.
/**
* Account listener, handles post‑creation logic
*/
@Component
public class AccountListener {
/**
* 1. Send email, SMS
*/
@EventListener
public void processAccountCreatedEvent1(AccountCreatedEvent event) {
// TODO
}
/**
* 2. Add points, etc. Use @Order(100) to set execution order
*/
@EventListener
@Order(100)
public void processAccountCreatedEvent2(AccountCreatedEvent event) {
// TODO
}
/**
* 3. Create Lucene index, etc. Use @Async for asynchronous execution
*/
@EventListener
@Async
public void processAccountCreatedEvent3(AccountCreatedEvent event) {
// TODO
}
}Sending Event
Example of publishing an account‑creation event in MyBatis. Note: JPA provides its own event mechanism, so manual publishing is unnecessary when using JPA.
@Autowired
private ApplicationEventPublisher publisher;
@Override
public boolean save(Account account) {
// pseudo‑code, assume DB save succeeds
if (true) {
publisher.publishEvent(new AccountCreatedEvent(account));
}
return false;
}@EventListener Annotation Attributes
The value (or classes) attribute specifies one or more event types to listen for, allowing a single method to handle a hierarchy of events. The condition attribute accepts a Spring EL expression for additional filtering, e.g., #event.account.age > 10.
Listener Execution Order
Use @Order(100) to define the order in which listeners are invoked. In asynchronous scenarios, ordering only guarantees the order of task submission to the thread pool; actual execution depends on thread scheduling.
Async Execution
Mark a listener method with @Async to run it asynchronously. Ensure that @EnableAsync is present in the configuration to activate Spring’s async support.
Spring Boot Async Configuration
This section is based on the async configuration from the mica project.
Enable asynchronous processing with @EnableAsync and scheduled tasks with @EnableScheduling. The MicaAsyncProperties class configures the thread pool.
mica.async.core-pool-size: default 2 (core thread count)
mica.async.keep-alive-seconds: default 300 (thread keep‑alive time)
mica.async.max-pool-size: default 50 (maximum thread count)
mica.async.queue-capacity: default 10000 (task queue capacity)
References
[1] https://gitee.com/596392912/JFinal-event [2] mica: https://gitee.com/596392912/mica
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
