Graceful shutdown of application is very important aspect to leave our application in consistent state. The primary task of shut downing application is releasing all resources back to system.
We will learn how to bring down, Spring and normal Java apps.
You would have noticed invoking registerShutdownHook() on AbstractApplicationContext in Spring apps, so that Spring gets a chance to clean up it's resources before JVM goes down.
try
{
AbstractApplicationContext context= new ClasspathXmlApplicationContext("application-config.xml")
context.registerShutdownHook();
}
finally
{
context.close();
}
Pls remember, there is no guarantee that always shutdown hook gets executed. for example, if user uses Kill -9 etc hook won't gets it's turn. in cases of, Kill/ctl+c System.exit(0), hook runs before JVM goes off.
This way Spring will cleanup container and invoke destroy methods on beans.
It's also good practice to call close() in finally block. This is because if our application thows unhandled runtime exception, we might of background threads,started by some beans still running and JVM will not terminate.
In case of normal applications, we can register hook with JVM by calling addShutdownHook(Runnable) on Runtime instance.
For example,
public class ShutdownHookExample
{
public static void main(String[] args)
{
ShutdownHookExample shutdownHook = new ShutdownHookExample();
shutdownHook.registerShutDownHook();
}
private Thread shutDownHook = new Thread()
{
public void run()
{
cleanUp();
}
};
private void registerShutDownHook()
{
//Registering hook with VM
Runtime.getRuntime().addShutdownHook(this.shutDownHook);
while(true);
}
private void cleanUp()
{
System.out.println("cleaning resources");
}
}
Compile an run above program from command prompt and press ctl+c to bring down program and we could see cleaning resources printing in console.
We can close DB connections,File handlers etc in cleanUp method.
Hope you find this post useful.
Happy Learning.
We will learn how to bring down, Spring and normal Java apps.
You would have noticed invoking registerShutdownHook() on AbstractApplicationContext in Spring apps, so that Spring gets a chance to clean up it's resources before JVM goes down.
try
{
AbstractApplicationContext context= new ClasspathXmlApplicationContext("application-config.xml")
context.registerShutdownHook();
}
finally
{
context.close();
}
Pls remember, there is no guarantee that always shutdown hook gets executed. for example, if user uses Kill -9 etc hook won't gets it's turn. in cases of, Kill/ctl+c System.exit(0), hook runs before JVM goes off.
This way Spring will cleanup container and invoke destroy methods on beans.
It's also good practice to call close() in finally block. This is because if our application thows unhandled runtime exception, we might of background threads,started by some beans still running and JVM will not terminate.
In case of normal applications, we can register hook with JVM by calling addShutdownHook(Runnable) on Runtime instance.
For example,
public class ShutdownHookExample
{
public static void main(String[] args)
{
ShutdownHookExample shutdownHook = new ShutdownHookExample();
shutdownHook.registerShutDownHook();
}
private Thread shutDownHook = new Thread()
{
public void run()
{
cleanUp();
}
};
private void registerShutDownHook()
{
//Registering hook with VM
Runtime.getRuntime().addShutdownHook(this.shutDownHook);
while(true);
}
private void cleanUp()
{
System.out.println("cleaning resources");
}
}
Compile an run above program from command prompt and press ctl+c to bring down program and we could see cleaning resources printing in console.
We can close DB connections,File handlers etc in cleanUp method.
Hope you find this post useful.
Happy Learning.