Skip to content

Exploring the Power of Spring Events πŸ¦ΈπŸΌβ€β™‚οΈ

Lyes S edited this page Jul 17, 2022 · 5 revisions

Table Of Contents

Exploring the Power of Spring Events

Spring Events vs Direct Method Calls

Spring events are one of the most used functionalities in the framework but also one of the more useful. It provides a way for different components to communicate and have more flexibility with each other than the traditional method calls.

Event System

Leads to Flexible and loosely coupled architecture design.

Ref.:[1]

Benefits

  • The loose coupling architecture design can handle changes because it :
    • Reduces the dependency between multiple components.
    • Reduces the risk of unanticipated impact on the other component.
    • Simplifies testing, maintenance and troubleshooting problems.
  • Provides pub/sub capability, which means subscribers can be added or removed at any time.
    • Event publishers and event subscribers (listeners) are not tied up and can be used independently of each other.
    • Changes in the publisher or subscribers will not affect each other as both are independent.
  • Allows sending data to other components effectively.
  • Allows invoking logic on multiple components at the same time

Highlights of Spring Events

Core components of Spring Events are : Event, Publisher and Listener.

Event

An Application Event is a simple POJO class that holds data and exchanges between publisher and listener.

public class CustomEvent {
    private String name;
}

Publisher

Constructs event objects and publishes it to anyone who is listening. It uses ApplicationEventPublisher and its publishEvent method.

@Autowired
private final ApplicationEventPublisher publisher;

public void placeOrder(Order order) {
   publisher.publishEvent(new CustomEvent());
}

Listener

There are different flavors of implementing listeners to an event. It can be implemented with annotations or by implementing ApplicationListener interface.

public class MyListener implements ApplicationListener<CustomEvent> {

  @EventListener
  public void onEvent(CustomEvent customEvent){}

  @Override
  public void onApplicationEvent(CustomEvent customEvent){}
}

Spring does all the heavy lifting for us and register all listeners, so there is no need for manual registration.

Spring events are by default synchronous, meaning that the publisher thread blocks until all listeners have finish processing the event.

Ref.:[1]

However, Spring also supports Asynchronous mode, meaning that the event is published in a new thread and release execution of the publisher independently from the listener.

Ref.:[1]

Transaction Bound Events

Spring allows us to bind an event listener to a phase of the current transaction. This allows events to be used with more flexibility when the outcome of the current transaction matters to the listener.

Ref.:[1]

When we annotate our method with @TransactionalEventListener, we get an extended event listener that is aware of the transaction. [2]

public class MyListener {

  @TransactionalEventListener(phase=TransactionPhase.AFTER_COMPLETION)
  public void onEvent(CustomEvent customEvent) {
     // Will only be invoked when the current transaction completes.
  }

}

Predefined Events

By default, Spring provides several predefined application events that are tied to the life cycle of Spring Application Context.

Ref.:[1]

Some events are triggered before the ApplicationContext is created, so we cannot register a listener on those as a @Bean. We can register listeners for these events by adding the listener manually.

  public static void main(String[] args) {
    SpringApplication springApplication = new SpringApplication(Application.class);
    springApplication.addListeners(new MyListener());
    springApplication.run(args);
  }
Clone this wiki locally