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
Technological progress can be expressed as layers of abstraction - higher layers are built on top of lower layers treating them as abstract black boxes with known interfaces. A serverless approach represents an inflection point that entirely separates the runtime layer from the u...
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO Silicon Valley 2019 will cover all of these tools, with the most comprehensive program and with 222 rockstar speakers throughout our industry presenting 22 Keynotes and General Sessions, 250 Breakout Sessions along 10 Tracks, as well as our ...
Your job is mostly boring. Many of the IT operations tasks you perform on a day-to-day basis are repetitive and dull. Utilizing automation can improve your work life, automating away the drudgery and embracing the passion for technology that got you started in the first place. In...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and l...
Big Switch's mission is to disrupt the status quo of networking with order of magnitude improvements in network e ciency, intelligence and agility by delivering Next-Generation Data Center Networking. We enable data center transformation and accelerate business velocity by delive...
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