ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Surviving Abrupt Shutdown
Pages: 1, 2

Listing 2: A simple Swing application

package test;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

public class MySwingApp extends JFrame {
    JButton exitButton   = new JButton();
    JTextArea jTextArea1 = new JTextArea();

    String dir      = System.getProperty("user.dir");
    String filename = "temp.txt";

    public MySwingApp() {
        exitButton.setText("Exit");
        exitButton.setBounds(new Rectangle(304, 248, 76, 37));
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exitButton_actionPerformed(e);
            }
        });

        this.getContentPane().setLayout(null);
        jTextArea1.setText("Click the Exit button to quit");
        jTextArea1.setBounds(new Rectangle(9, 7, 371, 235));
        this.getContentPane().add(exitButton, null);
        this.getContentPane().add(jTextArea1, null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setBounds(0,0, 400, 330);
        this.setVisible(true);
        initialize();
    }

    private void initialize() {
        // create a temp file
        File file = new File(dir, filename);

        try {
            System.out.println("Creating temporary file");
            file.createNewFile();
        }
        catch (IOException e) {
            System.out.println("Failed creating temporary file.");
        }
    }

    private void shutdown() {
        // delete the temp file
        File file = new File(dir, filename);

        if (file.exists()) {
            System.out.println("Deleting temporary file.");
            file.delete();
        }
    }

    void exitButton_actionPerformed(ActionEvent e) {
        shutdown();
        System.exit(0);
    }

    public static void main(String[] args) {
        MySwingApp mySwingApp = new MySwingApp();
    }
}

When run, the application calls its initialize method. The initialize method, in turn, creates a temporary file called temp.txt in the user's directory:

private void initialize() {
    // create a temp file
    File file = new File(dir, filename);

    try {
        System.out.println("Creating temporary file");
        file.createNewFile();
    }
    catch (IOException e) {
        System.out.println("Failed creating temporary file.");
    }
}

When the user closes the application, the application must delete the temporary file. We hope that the user will always click the Exit button--by doing so, the shutdown method (which deletes the temporary file) will always be called. However, the temporary file will not be deleted if the user closes the application, by clicking the X button of the frame or by some other means.

Listing 3 offers a solution to this. It modifies the code in Listing 2 by providing a shutdown hook. The shutdown hook class is declared as an inner class so that it has access to all of the methods of the main class. In Listing 3, the shutdown hook's run method calls the shutdown method, guaranteeing that this method will be invoked when the virtual machine shuts down.

Listing 3: Using a shutdown hook in the Swing application

package test;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

public class MySwingAppWithShutdownHook extends JFrame {
    JButton exitButton   = new JButton();
    JTextArea jTextArea1 = new JTextArea();

    String dir      = System.getProperty("user.dir");
    String filename = "temp.txt";

    public MySwingAppWithShutdownHook() {
        exitButton.setText("Exit");
        exitButton.setBounds(new Rectangle(304, 248, 76, 37));
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exitButton_actionPerformed(e);
            }
        });

        this.getContentPane().setLayout(null);
        jTextArea1.setText("Click the Exit button to quit");
        jTextArea1.setBounds(new Rectangle(9, 7, 371, 235));
        this.getContentPane().add(exitButton, null);
        this.getContentPane().add(jTextArea1, null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setBounds(0,0, 400, 330);
        this.setVisible(true);
        initialize();
    }

    private void initialize() {
        // add shutdown hook
        MyShutdownHook shutdownHook = new MyShutdownHook();
        Runtime.getRuntime().addShutdownHook(shutdownHook);

        // create a temp file
        File file = new File(dir, filename);

        try {
            System.out.println("Creating temporary file");
            file.createNewFile();
        }
        catch (IOException e) {
            System.out.println("Failed creating temporary file.");
        }
    }

    private void shutdown() {
        // delete the temp file
        File file = new File(dir, filename);

        if (file.exists()) {
            System.out.println("Deleting temporary file.");
            file.delete();
        }
    }

    void exitButton_actionPerformed(ActionEvent e) {
        shutdown();
        System.exit(0);
    }

    public static void main(String[] args) {
        MySwingAppWithShutdownHook mySwingApp = new MySwingAppWithShutdownHook();
    }

    private class MyShutdownHook extends Thread {
        public void run() {
            shutdown();
        }
    }
}

Pay special attention to the initialize method in the class shown in Listing 3. The first thing it does is to create an instance of the inner class MyShutdownHook, which extends a Thread:

// add shutdown hook
MyShutdownHook shutdownHook = new MyShutdownHook();

Once you get an instance of the MyShutdownHook class, pass it to the addShutDownHook method of the Runtime, as in the following line:

Runtime.getRuntime().addShutdownHook(shutdownHook);

The rest of the initialize method is similar to the initialize method in the class given in Listing 2. It creates a temporary file and prints a string "Creating temporary file":

// create a temp file
File file = new File(dir, filename);

try { 
    System.out.println("Creating temporary file");
    file.createNewFile();
}
catch (IOException e) {
    System.out.println("Failed creating temporary file.");
}

Now, start the small application given in Listing 3. Check that the temporary file is always deleted, even if you abruptly shut down the application.

Summary

Sometimes we want our application to run some clean-up code prior to shutting down. However, it is impossible to rely on the user to always quit properly. The shutdown hook described in this article offers a solution that guarantees that the clean-up code is run, regardless of how the user closes the application.

Budi Kurniawan is a senior J2EE architect and author.


Return to ONJava.com.