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
"Calligo is a cloud service provider with data privacy at the heart of what we do. We are a typical Infrastructure as a Service cloud provider but it's been designed around data privacy," explained Julian Box, CEO and co-founder of Calligo, in this SYS-CON.tv interview at 21st Cl...
DXWorldEXPO LLC announced today that the upcoming DXWorldEXPO | DevOpsSUMMIT | CloudEXPO New York will feature 10 companies from Poland to participate at the "Poland Digital Transformation Pavilion" on November 12-13, 2018. Polish Digital Transformation companies which will exhib...
Organize your corporate travel faster, at lower cost. Hotailors is a next-gen AI-powered travel platform. What is Hotailors? Hotailors is a platform for organising business travels that grants access to the best real-time offers from 2.000.000+ hotels and 700+ airlines in ...
There's no doubt that blockchain technology is a powerful tool for the enterprise, but bringing it mainstream has not been without challenges. As VP of Technology at 8base, Andrei is working to make developing a blockchain application accessible to anyone. With better tools, entr...
DXWorldEXPO LLC announced today that Nutanix has been named "Platinum Sponsor" of CloudEXPO | DevOpsSUMMIT | DXWorldEXPO New York, which will take place November 12-13, 2018 in New York City. Nutanix makes infrastructure invisible, elevating IT to focus on the applications and se...
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
Most Read This Week
ADS BY GOOGLE