Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How correctly close the ApplicationContext in Spring?

I am studying for the Spring Core certification and I have some dount about on this question finded into the provided study material:

What is the preferred way to close an application context?

I know that if I have something like this:

ConfigurableApplicationContext context = …
// Destroy the application
context.close();

by the use of the close() method on the context objet the ApplicationContext is closed and the application is destroyed.

But I think that this is not the best way that I have to do it.

Reading the official documentation I find that I can also do something like this:

context.registerShutdownHook();

that register a Shutdown Hook with the JVM so it is the JVM that will trigger Spring's close phase before JVM exits. So on JVM exit, Spring's close phase will execute.

On the documentation I can read that: usually not possible to call context.close() because many applications (web applications) run indefinitely But what exactly means this last assertion? why web application run indefinitely?

So my questions are:

  • Can I use this second way to close an application context also with not web application?
  • Is it prefered respect the context.close()?

Tnx

like image 419
AndreaNobili Avatar asked Mar 20 '15 15:03

AndreaNobili


People also ask

How do I turn off Spring applications?

Another option to shutdown the Spring Boot application is to close Spring ApplicationContext using SpringApplication . SpringApplication. run(String…) method returns ApplicationContext as a ConfigurableApplicationContext We can use close() method to close ApplicationContext programmatically.

Why do we need to close application context?

It is important to close the context properly because different lifecycle methods must have the chance to run. Consequently, the application can release the resources and do some clean-up.

How do you gracefully shutdown a Spring boot application?

Close ApplicationContext Another option to shutdown Spring Boot application is to close Spring ApplicationContext using SpringApplication . SpringApplication#run(String…) method returns ApplicationContext as a < ConfigurableApplicationContext. We can use close() method to close ApplicationContext programmatically.


1 Answers

As you are aware that ContextLoaderListener is the one that takes care of initializing and destroying your ApplicationContext, when you shutdown your server, that ContextLoaderListener's contextDestroyed method is invoked.

  public void contextDestroyed(ServletContextEvent event){
    closeWebApplicationContext(event.getServletContext());
    ContextCleanupListener.cleanupAttributes(event.getServletContext());
  }

In that closeWebApplicationContext, they actually call the close method on ApplicationContext like this

  if ((this.context instanceof ConfigurableWebApplicationContext)) {
    ((ConfigurableWebApplicationContext)this.context).close();
  }

This is straight from spring-web-4.1.5.jar. As it is evident from here, they use close to destroy ApplicationContext in web applications.

But registerShutdownHook is used to explicitly shut down IoC container in non-web applications something like a standalone desktop application, specially when you're creating the ApplicationContext manually from ClassPathXmlApplicationContext (or) FileSystemXmlApplicationContext (or) some other types.

This is done to release all resources used by your spring application and to call destroy method on your spring beans if any.

like image 68
Arkantos Avatar answered Oct 12 '22 09:10

Arkantos