Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between ContextRefreshedEvent, ContextStartedEvent, ContextStoppedEvent and ContextClosedEvent

Tags:

spring

events

In Spring 5.x, what is the difference between following events?

  1. ContextRefreshedEvent
  2. ContextStartedEvent
  3. ContextStoppedEvent
  4. ContextClosedEvent

Which event correlate with the servlet context events (as per https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html):

  • ServletContextListener.contextInitialized(ServletContextEvent); and
  • ServletContextListener.contextDestroyed(ServletContextEvent)?

I have the following situation:

  • In want to initialize a logging subsystem as soon as possible, should that be done in ContextRefreshedEvent or ContextStartedEvent?

  • I also want to destruct it as late as possible, should that be done in ContextClosedEvent or ContextStoppedEvent?

like image 997
Franz They Avatar asked Nov 06 '17 09:11

Franz They


People also ask

What is ContextClosedEvent?

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.

What is ContextStartedEvent?

public class ContextStartedEvent extends ApplicationContextEvent. Event raised when an ApplicationContext gets started.

What is spring ContextRefreshedEvent?

public class ContextRefreshedEvent extends ApplicationContextEvent. Event raised when an ApplicationContext gets initialized or refreshed.


1 Answers

The documentation for these built-in events can be found here, specifically:

ContextRefreshedEvent

Published when the ApplicationContext is initialized or refreshed (for example, by using the refresh() method on the ConfigurableApplicationContext interface). Here, “initialized” means that all beans are loaded, post-processor beans are detected and activated, singletons are pre-instantiated, and the ApplicationContext object is ready for use. As long as the context has not been closed, a refresh can be triggered multiple times, provided that the chosen ApplicationContext actually supports such “hot” refreshes. For example, XmlWebApplicationContext supports hot refreshes, but GenericApplicationContext does not.

ContextStartedEvent

Published when the ApplicationContext is started by using the start() method on the ConfigurableApplicationContext interface. Here, “started” means that all Lifecycle beans receive an explicit start signal. Typically, this signal is used to restart beans after an explicit stop, but it may also be used to start components that have not been configured for autostart (for example, components that have not already started on initialization).

ContextStoppedEvent

Published when the ApplicationContext is stopped by using the stop() method on the ConfigurableApplicationContext interface. Here, “stopped” means that all Lifecycle beans receive an explicit stop signal. A stopped context may be restarted through a start() call.

ContextClosedEvent

Published when the ApplicationContext is closed by using the close() method on the ConfigurableApplicationContext interface. Here, “closed” means that all singleton beans are destroyed. A closed context reaches its end of life. It cannot be refreshed or restarted.

RequestHandledEvent

A web-specific event telling all beans that an HTTP request has been serviced. This event is published after the request is complete. This event is only applicable to web applications that use Spring’s DispatcherServlet.

Afaik, none of these correlate directly to the ServletContext. That's a different beast than Spring's application context, and there are separate events for that.

Setting up and tearing down a logging system can be complicated and will depend on which logging system you use. In short, you may want to try with ContextRefreshedEvent and ContextClosedEvent. The other two are only dispatched when you call start() or stop() on the application context, so you wouldn't want to use those.

If you're using Spring Boot, you may want to look at Spring Boot's own abstraction of logging systems (org.springframework.boot.logging.LoggingSystem), which defines beforeInitialize, initalize, and cleanUp methods, and also a shutdownHandler that is called when the JVM exists.

And see org.springframework.boot.context.logging.LoggingApplicationListener for reference. Spring Boot comes with additional application events. The initialization of the logging system seems to be done on the ApplicationEnvironmentPreparedEvent. Cleanup is done on ContextClosedEvent and ApplicationFailedEvent.

like image 120
Dario Seidl Avatar answered Oct 26 '22 13:10

Dario Seidl