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 Post6 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

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

*