Do a graceful shutdown of your Java Application when Ctr-C, Kill…
By admin on Sep 27, 2007 in Java, Programming
When I am developing Java application for backend processes to do ETL, I often need to ensure that resources are properly cleaned up when the application is shutdown. However, user behavior is quite unpredictable. In either Windows or Unix, process can be terminated by force by the user, by sending the kill signal.
In Java, in order to do graceful shutdown, I normally add a shutdown hook to the Java runtime, using the Runtime.getRunTime().addShutdownHook method.
As an example, I have an interface for my application which has the start and shutdown method
public interface IApp {
void start();
void shutDown();
}
I wrote a ShutdownInterceptor class which extends the Thread class.
public class ShutdownInterceptor extends Thread {
private IApp app;
public ShutdownInterceptor(IApp app) {
this.app = app;
}public void run() {
System.out.println(“Call the shutdown routine”);
app.shutDown();
}
}
My sample application will implement the IApp interface. Have a look at the main method.
public class SampleApp extends BaseApp implements IApp {
public void start() {
try {
System.out.println(“Sleeping for 5 seconds before shutting down”);
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}public void shutDown() {
// Do a graceful shutdown here
System.out.println(“Shutdown is called”);
}public static void main(String args[]) {
IApp app = new SampleApp();
ShutdownInterceptor shutdownInterceptor = new ShutdownInterceptor(app);
Runtime.getRuntime().addShutdownHook(shutdownInterceptor);
app.start();
}
}
At startup, I created the SampleApp class, and then the ShutdownInterceptor class with the application instance. Then I add the interceptor to the Java runtime.
The application sleeps for 5 seconds. When it exits, ShutdownInterceptor will call application shutdown method automatically.
Lincoln | Jan 7, 2009 | Reply
Clear, concise and helpful.
Many thanks.
WW | Jun 5, 2009 | Reply
But this doesn’t work if the user hits CTRL-C
Dragon | May 8, 2010 | Reply
So useful information!
E.N | Aug 30, 2011 | Reply
Hi admin,
is there any way I can contact you with regards to backend of an etl
download kik messenger | Jul 9, 2013 | Reply
Simply want to say your article is as astounding. The clarity in your post is simply spectacular and i
can assume you are an expert on this subject. Fine with your permission allow me to grab
your RSS feed to keep up to date with forthcoming post.
Thanks a million and please carry on the enjoyable work.
auto insurance | Jan 10, 2014 | Reply
Slightly late, but thank you ;D
طبیعت گردی | Jan 21, 2015 | Reply
Keep on working, great job!
interesting website. cheers
ماشین ظرفشویی | Feb 3, 2015 | Reply
Very good information. Lucky me I came across your website by chance (stumbleupon).
I’ve book marked it for later!
چمدان دلسی | Feb 15, 2015 | Reply
i really enjoyed reading your article. it’s pleasant to read
برند | Feb 15, 2015 | Reply
Good day! Would you mind if I share your blog with my twitter group?
Thanks For Great posts.
تیشرت | Mar 9, 2015 | Reply
Good Day! I’m at work surfing around your blog from my new iphone! Just Excellent article! We are linking to this great article on our website.
دیزل ژنراتور | Apr 26, 2015 | Reply
Excellent article! We are linking to this great article on our website.
irandelsey | May 12, 2015 | Reply
Thanks everyone for the comments! I really appreciate it!
yasminseir | May 13, 2015 | Reply
Really instructive as well as superb structure of articles or blog posts, now that’s fantastic.
charoghcharm | May 17, 2015 | Reply
Good article. I absolutely love this site. Continue the good work!
bodog博狗 | Jun 18, 2015 | Reply
Any suggestions on sites to start a blog where I can BS sports and another happenings?