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

2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 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 AJAXWorld RIA Stories
Founded in 2000, Chetu Inc. is a global provider of customized software development solutions and IT staff augmentation services for software technology providers. By providing clients with unparalleled niche technology expertise and industry experience, Chetu has become the prem...
DXWorldEXPO LLC announced today that Dez Blanchfield joined the faculty of CloudEXPO's "10-Year Anniversary Event" which will take place on November 11-13, 2018 in New York City. Dez is a strategic leader in business and digital transformation with 25 years of experience in the I...
"DivvyCloud as a company set out to help customers automate solutions to the most common cloud problems," noted Jeremy Snyder, VP of Business Development at DivvyCloud, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY...
In his session at 21st Cloud Expo, James Henry, Co-CEO/CTO of Calgary Scientific Inc., introduced you to the challenges, solutions and benefits of training AI systems to solve visual problems with an emphasis on improving AIs with continuous training in the field. He explored app...
Data is the fuel that drives the machine learning algorithmic engines and ultimately provides the business value. In his session at Cloud Expo, Ed Featherston, a director and senior enterprise architect at Collaborative Consulting, discussed the key considerations around qualit...
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