Comments
yourfanat wrote: I am using another tool for Oracle developers - dbForge Studio for Oracle. This IDE has lots of usefull features, among them: oracle designer, code competion and formatter, query builder, debugger, profiler, erxport/import, reports and many others. The latest version supports Oracle 12C. More information here.
Cloud Expo on Google News
SYS-CON.TV
Cloud Expo & Virtualization 2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Cloud Computing & Enterprise IT: Cost & Operational Benefits
How and Why is a Flexible IT Infrastructure the Key To the Future?
Click For 2008 West
Event Webcasts
Java Code Stack #9 JVM Shutdown Hooks
Java Code Stack #9 JVM Shutdown Hooks

The other day I was trying to terminate this unprecedented application server from my console window by using CTRL-C. The application did get the idea that I didn't want to see any more of it, but I guess it was trying to save the settings, flush streams, close database connection, update log files, write preferences, check data files - all when I was waiting patiently. It was in a dangled state and didn't respond even when I killed the PID. I never felt paranoid with JVM Shutdown hooks until that moment. Do not use Runtime.runFinalizersOnExit, for performing a cleanup operation during improper JVM shutdown, because this will result in finalizing of objects, which is being manipulated by different concurrent threads. This is unsafe and will lead to deadlock.

Here is the proper implementation of shutdown hook. We create a shutdown hook, which performs the last minute panic operation, and monitor the cleanup period using a hook timer. Whether our application performs cleanup or not, the hook timer forcibly shuts down the application by invoking Runtime.halt. This is because when the JVM is about to be shut down all the hooks are started randomly without any order.

public class Jcs9{

public static class panicHook extends Thread{
panicHook(){
/** Note the empty constructor
* Do not start this thread with this.start()
* as the hook is already started
*/
}

public void run(){

/** Add your clean-up operation here **/
System.out.println("Shutdown Hook started......");
while(true){}

}
}


public static class hookTimer extends Thread{
hookTimer(){
/** Do not start this thread with this.start()
* as the hook is already started
*/
}

public void run(){

/** Monitor the Hook Period **/
try{
this.sleep(3000);
}catch(Exception e){}

System.out.println("Hook Period exceeded....Forcing Shutdown!!");

/** Do not use Runtime.removeShutdownHook(Thread) here! as the
* shutdown process is already started.
**/

/** Forcing all hooks to exit **/
Runtime.getRuntime().halt(0);
/** System.exit(0) has no effect while the
* hooks are active! so dont even try.
*/
}
}

public static void main(String ar[]){
Runtime rtime=Runtime.getRuntime();

/** Register the hook with the JVM **/
Thread phook=new panicHook();
rtime.addShutdownHook(phook);

/** Register the Timer with the JVM **/
Thread thook=new hookTimer();
rtime.addShutdownHook(thook);

System.out.println("Try interrupting me dude.....");
while(true){}
}

}

Improper implementation of shutdown-hook turns me off and I, in turn, turn off the JVM's interaction with the OS signals with the java -Xrs switch. So the application can't perform its cleanup during improper shutdown. Some revenge!

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Hey Tony Thanx for your suggestion. Will pass it on to the Web Admin.

i guess i forgot that when posted the browser will render my tags invisible.

better said ...
use html <pre> ... <pre\> tags before and after your code examples.

Your code has provided me with a start on how we can control our environment a little better when things get out of control. About your code looking strange ... how about embedding your java code in " ... your good code examples go here ... " tags so your code retains its spaces when posted on the site.
Always a learning experience while reading your articles :)

Thanks Brij, Sure the code looks strange....maybe the Web Admin can find out a way to retain the indent spaces to some extent.

This code is really excellent.
Although working for 2 years with java, I did'nt come across these methods.
Thanks for pointing that. Although the code looked strange at first look, but after reading the java doc of Runtime.addShutdownHook method it is clear.

Keep up the good work Frank
and thanks for all your code stacks.
Brijesh


Your Feedback
Frank Jennings wrote: Hey Tony Thanx for your suggestion. Will pass it on to the Web Admin.
Tony Blanco wrote: i guess i forgot that when posted the browser will render my tags invisible. better said ... use html <pre> ... <pre\> tags before and after your code examples.
Tony Blanco wrote: Your code has provided me with a start on how we can control our environment a little better when things get out of control. About your code looking strange ... how about embedding your java code in " ... your good code examples go here ... " tags so your code retains its spaces when posted on the site. Always a learning experience while reading your articles :)
Frank Jennings wrote: Thanks Brij, Sure the code looks strange....maybe the Web Admin can find out a way to retain the indent spaces to some extent.
Brijesh wrote: This code is really excellent. Although working for 2 years with java, I did'nt come across these methods. Thanks for pointing that. Although the code looked strange at first look, but after reading the java doc of Runtime.addShutdownHook method it is clear. Keep up the good work Frank and thanks for all your code stacks. Brijesh
Latest Cloud Developer Stories
In his session at 20th Cloud Expo, Scott Davis, CTO of Embotics, discussed how automation can provide the dynamic management required to cost-effectively deliver microservices and container solutions at scale. He also discussed how flexible automation is the key to effectively br...
Modern software design has fundamentally changed how we manage applications, causing many to turn to containers as the new virtual machine for resource management. As container adoption grows beyond stateless applications to stateful workloads, the need for persistent storage is ...
We are seeing a major migration of enterprises applications to the cloud. As cloud and business use of real time applications accelerate, legacy networks are no longer able to architecturally support cloud adoption and deliver the performance and security required by highly distr...
In this presentation, you will learn first hand what works and what doesn't while architecting and deploying OpenStack. Some of the topics will include:- best practices for creating repeatable deployments of OpenStack- multi-site considerations- how to customize OpenStack to inte...
Security, data privacy, reliability and regulatory compliance are critical factors when evaluating whether to move business applications from in-house client hosted environments to a cloud platform. In her session at 18th Cloud Expo, Vandana Viswanathan, Associate Director at Co...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021



SYS-CON Featured Whitepapers
ADS BY GOOGLE