Spring - Application Events

What are the Different Types of Events in Spring?

Spring’s ApplicationContext provides the functionality to support events and listeners in code. We can create beans that listen for events that are published through our ApplicationContext.

Event handling in the ApplicationContext is provided through the ApplicationEvent class and ApplicationListener interface. So if a bean implements the ApplicationListener, then every time an ApplicationEvent gets published to the ApplicationContext, that bean is notified.

public class AllApplicationEventListener implements ApplicationListener <ApplicationEvent>
{
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent)
        {
        //process event
    }
}

Spring provides the following five standard events:

ContextRefreshedEvent : This event is published when the ApplicationContext is either initialized or refreshed. This can also be raised using the refresh() method on the ConfigurableApplicationContext interface. ContextStartedEvent : This event is published when the ApplicationContext is started using the start() method on the ConfigurableApplicationContext interface. You can poll your database or you can re/start any stopped application after receiving this event. ContextStoppedEvent : This event is published when the ApplicationContext is stopped using the stop() method on the ConfigurableApplicationContext interface. You can do required house keeping work after receiving this event. ContextClosedEvent : This event is published when the ApplicationContext is closed using the close() method on the ConfigurableApplicationContext interface. A closed context reaches its end of life; it cannot be refreshed or restarted. RequestHandledEvent : This is a web-specific event telling all beans that an HTTP request has been serviced. Apart from the above, you can create your own custom events by extending ApplicationEvent class. e.g.

public class CustomApplicationEvent extends ApplicationEvent
{
        public CustomApplicationEvent ( Object source, final String msg )
        {
                super(source);
                System.out.println("Created a Custom event");
        }
}

To listen to this event, create a listener like this:

public class CustomEventListener implements ApplicationListener <CustomApplicationEvent>
{
        @Override
    public void onApplicationEvent(CustomApplicationEvent applicationEvent) {
                //handle event
        }
}

And to publish this event, you will need the help of the applicationContext instance.

CustomApplicationEvent customEvent
        = new CustomApplicationEvent( applicationContext, "Test message");
applicationContext.publishEvent ( customEvent )

Reading material

  1. https://docs.spring.io/spring-framework/reference/testing/testcontext-framework/application-events.html
  2. https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/event/ApplicationReadyEvent.html

TODO

  1. https://stackoverflow.com/questions/58967934/eventlistenerapplicationreadyevent-class-starts-only-one-method
  2. TODO (ApplicationReadyEvent) example : https://spinscale.de/posts/2020-08-06-introduction-into-spring-data-elasticsearch.html?source=post_page-----41daeda3e6b1

Links to this note