Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between registerShutdownHook() and close()

Tags:

spring

 HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
  obj.getMessage();
  context.registerShutdownHook();  

below the output:

Feb 03, 2017 11:46:12 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh  
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@799f7e29: startup date [Fri Feb 03 11:46:12 IST 2017]; root of context hierarchy
Feb 03, 2017 11:46:12 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Bean is going through init.  
Your Message : Hello World!  
Bean will destroy now.  

Whereas using context.close() gives

Feb 03, 2017 11:53:57 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh  
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@799f7e29: startup date [Fri Feb 03 11:53:57 IST 2017]; root of context hierarchy
Feb 03, 2017 11:53:57 AM   org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
INFO: Loading XML bean definitions from class path resource [Beans.xml]  
Bean is going through init.  
Your Message : Hello World!    
Feb 03, 2017 11:53:57 AM org.springframework.context.support.ClassPathXmlApplicationContext doClose  
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@799f7e29: startup date [Fri Feb 03 11:53:57 IST 2017]; root of context hierarchy  
Bean will destroy now.

Could someone explain the difference?

like image 856
user4790067 Avatar asked Feb 03 '17 06:02

user4790067


People also ask

What is registerShutdownHook?

The registerShutdownHook() is a method of Spring AbstractApplicationContext class. 2. The registerShutdownHook() method registers a shutdown hook named SpringContextShutdownHook with the JVM runtime. 3. On calling registerShutdownHook() , the Spring context is closed on JVM shutdown, if not already closed.

What is the preferred way to close an application context?

When the web container will want to stop the application, the ContextLoadListener will receive the event from the servlet. And then based on this event it will close the application context. After the undeploy of application, the @PreDestroy method will be called and the application context will be closed.

How do you handle shutdown of IoC container?

If you use Spring's IoC container in a non-web application environment (for example, in a rich client desktop environment), register a shutdown hook with the JVM. Doing so ensures a graceful shutdown and calls the relevant destroy methods on your singleton beans so that all resources are released.

What is shutdown hook in spring?

registerShutdownHook() method in Spring In Java programming language you can create shutdown hooks, where you create a new thread and provide logic that is executed when the JVM is shutting down. Then you can register your thread class instance as a shutdown hook to the VM using Runtime.


1 Answers

The ApplicationContext class doesn't define either of these methods as a part of its interface, but the ConfigurableApplicationContext does define both of these.

From the JavaDoc:

  • close() -- Close this application context, destroying all beans in its bean factory.
  • registerShutdownHook() -- Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.

Basically, AbstractApplicationContext#close() will close, or shutdown, the ApplicationContext at the time it is invoked, while AbstractApplicationContext#registerShutdownHook() will close, or shutdown, the ApplicationContext at a later time when the JVM is shutting down for whatever reason. This will be achieved by utilizing the JVM shutdown hook functionality.

In either case, the actual closing is done by the doClose() method.

If you are curious about why your outputs look so similar, it is because they are effectively doing the same thing, whether you call #close() or #registerShutdownHook() at line 3 of you example. #close will shutdown right away, and #registerShutdownHook will shutdown just before the JVM will exit, which is pretty much as soon as the method is done being invoked, because it is the last line of code!

like image 190
nicholas.hauschild Avatar answered Sep 20 '22 13:09

nicholas.hauschild