The registerShutdownHook in spring


 

The registerShutdownHook in spring

In this tutorial you will learn about Register Shutdown Hook of Spring Framework

In this tutorial you will learn about Register Shutdown Hook of Spring Framework

The registerShutdownHook in spring

If you are using Spring IoC in non web application and want to gracefully downdown all resources we use registerShutdownHook(). This method calls the relevant destroy methods. In order to register a shutdown hook, you have to call the the registerShutdownHook() method which found in the AbstractApplicationContext class.

AppMain.java

package com.roseindia.common;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppMain {
	public static void main(String[] args) {
		AbstractApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "context.xml" });
		System.out.println("Add a shutdown hook for the above context");
		context.registerShutdownHook();
	}
}

context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>

When you run this application it will display message as shown below:


Add a shutdown hook for the above context
Sep 3, 2010 3:04:24 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@18a47e0: startup date [Fri Sep 03 15:04:24 GMT+05:30
2010]; root of context hierarchy

Download this example code

Ads