RSS Feed for This PostCurrent Article

Do a graceful shutdown of your Java Application when Ctr-C, Kill…

Download Source Code

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.


Trackback URL


RSS Feed for This Post15 Comment(s)

  1. Lincoln | Jan 7, 2009 | Reply

    Clear, concise and helpful.

    Many thanks.

  2. WW | Jun 5, 2009 | Reply

    But this doesn’t work if the user hits CTRL-C

  3. Dragon | May 8, 2010 | Reply

    So useful information!

  4. E.N | Aug 30, 2011 | Reply

    Hi admin,
    is there any way I can contact you with regards to backend of an etl

  5. 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.

  6. auto insurance | Jan 10, 2014 | Reply

    Slightly late, but thank you ;D

  7. طبیعت گردی | Jan 21, 2015 | Reply

    Keep on working, great job!

    interesting website. cheers

  8. ماشین ظرفشویی | Feb 3, 2015 | Reply

    Very good information. Lucky me I came across your website by chance (stumbleupon).
    I’ve book marked it for later!

  9. چمدان دلسی | Feb 15, 2015 | Reply

    i really enjoyed reading your article. it’s pleasant to read

  10. برند | Feb 15, 2015 | Reply

    Good day! Would you mind if I share your blog with my twitter group?
    Thanks For Great posts.

  11. تیشرت | 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.

  12. دیزل ژنراتور | Apr 26, 2015 | Reply

    Excellent article! We are linking to this great article on our website.

  13. irandelsey | May 12, 2015 | Reply

    Thanks everyone for the comments! I really appreciate it!

  14. yasminseir | May 13, 2015 | Reply

    Really instructive as well as superb structure of articles or blog posts, now that’s fantastic.

  15. charoghcharm | May 17, 2015 | Reply

    Good article. I absolutely love this site. Continue the good work!

3 Trackback(s)

  1. From Add Multithreading Function to Java Application using ExecutorService | twit88.com | Sep 28, 2007
  2. From Java - Signal Handling | twit88.com | Feb 6, 2008
  3. From Java: Understanding System.exit | Programming Resources | Aug 20, 2008

RSS Feed for This PostPost a Comment

*