Saturday 21 July 2018

Spring boot: Application Events and Listeners

In this post, I am going to explain about the listeners offered by spring. Listeners are used to get notification about events like application startup events, task finished events etc.,

For example, assume there is a big file uploading task is happening. You can create a listener to the upload task and get notified once the upload is finished.

How to add listeners to spring application?
You can add listeners with the SpringApplication.addListeners(…) method or the SpringApplicationBuilder.listeners() method.

SpringApplication application = new SpringApplication();
application.addListeners(new MyApplicationListener());
ConfigurableApplicationContext configAppContext = application.run(args);

                  (OR)
                 
ConfigurableApplicationContext applicationContent = new SpringApplicationBuilder(Application.class).listeners(new MyApplicationListener()).child(Application.class).web(WebApplicationType.NONE).bannerMode(Banner.Mode.CONSOLE).logStartupInfo(false).run(args);

When your application runs, Application events are sent in the following order.

a.   An ApplicationStartingEvent is sent at the start of a run but before any processing, except for the registration of listeners and initializers.
b.   An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known but before the context is created.
c.   An ApplicationPreparedEvent is sent just before the refresh is started but after bean definitions have been loaded.
d.   An ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.
e.   An ApplicationReadyEvent is sent after any application and command-line runners have been called. It indicates that the application is ready to service requests.
f.    An ApplicationFailedEvent is sent if there is an exception on startup.

Find the below working example.

MyApplicationListener.java

package com.sample.myApp.listeners;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {

 @Override
 public void onApplicationEvent(ApplicationEvent event) {
  System.out.println("MyApplicationListener : " + event);
 }

}

Application.java

package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.sample.myApp.listeners.MyApplicationListener;

@SpringBootApplication
public class Application {

 public static void main(String args[]) {

  SpringApplication application = new SpringApplication(Application.class);
  application.addListeners(new MyApplicationListener());

  ConfigurableApplicationContext configurableApplicationContext = application.run(args);

  configurableApplicationContext.close();
 }
}


When you ran Application class, you can able to see below events in the console.

MyApplicationListener : org.springframework.boot.context.event.ApplicationStartingEvent[source=org.springframework.boot.SpringApplication@2595ed0b]
MyApplicationListener : org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent[source=org.springframework.boot.SpringApplication@2595ed0b]
MyApplicationListener : org.springframework.boot.context.event.ApplicationPreparedEvent[source=org.springframework.boot.SpringApplication@2595ed0b]
MyApplicationListener : org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7be2e8f0: startup date [Wed May 09 10:13:42 IST 2018]; root of context hierarchy]
MyApplicationListener : org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent[source=org.springframework.boot.web.embedded.tomcat.TomcatWebServer@7e217dd7]
MyApplicationListener : org.springframework.boot.context.event.ApplicationStartedEvent[source=org.springframework.boot.SpringApplication@2595ed0b]
MyApplicationListener : org.springframework.boot.context.event.ApplicationReadyEvent[source=org.springframework.boot.SpringApplication@2595ed0b]
MyApplicationListener : org.springframework.context.event.ContextClosedEvent[source=org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7be2e8f0: startup date [Wed May 09 10:13:42 IST 2018]; root of context hierarchy]


Project structure looks like below.





Previous                                                 Next                                                 Home

No comments:

Post a Comment