Mastering Spring Event: Synchronous & Asynchronous Patterns in Java

This article explains how to use Spring's Application Event mechanism for both synchronous and asynchronous processing, covering custom event creation, listener definition, publisher usage, unit testing, and enabling async execution with @EnableAsync and @Async annotations, illustrated with code examples and diagrams.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Mastering Spring Event: Synchronous & Asynchronous Patterns in Java

Preface

In real‑world business development, logic can become extremely complex with a core business plus many sub‑tasks, leading to long, tightly coupled code that is hard to maintain. Some scenarios, such as sending emails or SMS, do not need to be completed synchronously within a single request.

Message queues can solve this problem, but they add weight and complexity; when not necessary, we prefer a lighter solution. To address these cases, we explore Spring Event.

Spring Event Synchronous Usage

Spring Event (Application Event)

is essentially an observer pattern: after a bean finishes a task, it can notify other beans, or a bean can listen to another bean's behavior.

1. Custom Event

Define an event class that extends ApplicationEvent.

2. Define Listener

Implement the ApplicationListener interface or annotate a method with @EventListener to handle the event.

3. Define Publisher

Publish the event using ApplicationEventPublisher.

4. Unit Test Execution

Run the test and observe the results.

Spring Event Asynchronous Usage

Some business scenarios, like sending emails or SMS, do not require synchronous completion.

1. Custom Event

2. Define Listener

It is recommended to use the @EventListener annotation.

3. Define Publisher

4. Unit Test Execution (Synchronous)

5. Enable Asynchronous Execution

Add the @EnableAsync annotation to the main application class and annotate asynchronous listener methods with @Async.

6. Unit Test Execution (Asynchronous)

The SMS sending thread appears as task-1; after the main thread finishes (total time: 2017 ms), the console stops printing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaAsynchronousEvent
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.