Gracefully Shutdown Spring Application Container

This tutorial shows you how to register a shutdown hook with the JVM in a non-web application context. By default the ApplicationContext does not have a shutdown hook and does not have any exposed methods to configure this. These methods are all encapsulated in the ConfigurableApplicationContext. This interface exposes a couple of extra configuration and lifecycle management methods like close(); and registerShutdownHook();. When you call the registerShutdownHook() method, this registers a shutdown hook with the JVM and ensures a graceful shutdown and calls the relevant destroy methods on your singleton beans.

Adding init and destroy methods

This spring managed bean has a init and a destroy method. These methods will be called when the bean is created and destroyed.

package com.memorynotfound.spring.core.lifecycle;

public class Logger {

    public void init(){
        System.out.println("- - - initializing logger bean");
    }

    public void destroy(){
        System.out.println("- - - destroying logger bean");
    }
}

Configuring the bean

We configure the initializing and destruction methods using xml configuration. The init-method method registers an initialization method that will be called when the bean is created. The destroy-method method registers a destruction method that will be called before the JVM is shutdown. The lifecycle methods will only be invoked on a singleton bean.

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

    <bean class="com.memorynotfound.spring.core.lifecycle.Logger"
          init-method="init"
          destroy-method="destroy"/>

</beans>

Register a Shutdown hook with the JVM

The ConfigurableApplicationContext exposes the close() and registerShutdownHook() methods. You can explicitly call the close() method. Or call the registerShutdownHook() method. Both must be used in a non-web application context. These methods will call the registered destruction methods of singleton beans before the JVM is closed.

package com.memorynotfound.spring.core.lifecycle;

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

public class Run {

    public static void main(String... args) throws InterruptedException {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
        // implicit shutdown hook
        context.registerShutdownHook();

        // or explicit close method
        context.close();
    }
}

Output

As you can see, the destruction method of our singleton bean is called.

- - - initializing logger bean
- - - destroying logger bean

References

Download

You may also like...