回顾
在很多情况,当关闭应用程序的时候,需要清理某些资源。问题是用户不总是安装推荐的进程退出。例如,在一个Tomcat部署中 通过初始化一个服务器对象开启servlet容器并且调用其start方法,反过来依次调用其他组件的开启方法。通常情况下,一个服务器对象有机会停止其他的组件,通过发送shutdown命令来关闭它,就如第十四章讲述的,“Server and Service”。如果突然简单的退出会发生一些意料不到的事情,比如在程序运行的时候关闭控制台的时候。
幸运地是,Java为开发者提供了一种优雅的方式,在关闭进程中执行代码,来确保清除的代码总是被执行。这一章显示了如何使用一个ShutDown Hook来保证清除代码总被执行而不需要考虑用户如何终止这个应用程序。
在java中,针对两种事件虚拟机就会关闭:
应用程序正常退出,调用了System.exit方法或者最后的非守护线程退出。
用户突然强制虚拟机终止,例如输入CRTL+C或者在关闭一个运行中的Java程序之前从系统的退出(断线等情况)。
幸运地是,当关闭的时候,虚拟机会执行以下两步:
1 如果有的话,虚拟机启动所有注册的shutdown钩子。ShutDown钩子都是运行的时候之前注册的线程。所有的shutdown 钩子都会同时运行知道结束为止。
2 如果适合地话,虚拟机调用所有未包括的终结器。
在这一章,我们对第一步感兴趣,因为其允许开发者告诉虚拟机在程序中执行一些清除代码。一个shutdown hook实质上是一个线程子类的实例。创建一个shutdown hook是很简单的。步骤如下:
1 写一个继承Thread的类
2 提供一个run方法的实现。这个方法需要在应用程序关闭的时候运行,不管是通常地还是突然地。
3 在你的应用程序中,实例化你的shutdown 钩子
4 使用当前运行的addShutdownHook方法注册你的shutdown钩子。
你也许注意到,如果其他线程一样,你不需要启动你的shutdown钩子。当虚拟机运行它的的关闭指令的时候,其将开启和运行你的shutdown钩子。
代码清单16.1提供了一个ShutdownHookDemo类和一个线程子类ShutdownHook。注意ShutdownHook类的run方法仅仅简单地在控制台中打印字符串Shuting down。然而,你可以在打印语句前插入你所需的代码。
package ex16.pyrmont.shutdownhook;
public class ShutdownHookDemo {
public void start() {
System.out.println("Demo");
ShutdownHook ShutdownHook = new ShutdownHook();
Runtime.getRuntime().addShutdownHook(ShutdownHook);
}
public static void main(String[] args) {
ShutdownHookDemo demo = new ShutdownHookDemo();
demo.start();
try {
System.in.read();
}
catch(Exception e) {
}
}
}
class ShutdownHook extends Thread {
public void run() {
System.out.println("Shutting down");
}
}
在实例化了ShutdownHookDemo 类后,main方法调用了start方法。start方法创建了一个shutdown 钩子并在运行的时候注册。
ShutdownHook shutdownHook = new ShutdownHook();
Runtime.getRuntime().addShutdownHook(shutdownHook);
之后,程序等待用户按下enter键。
System.in.read();
当用户按下enter后,程序退出。然而,虚拟机将运行这个shutdown 钩子,结果在控制台打印出如下单词 Shutting down。
a Shutdown Hook Example
另外一个例子,使用一个简单地swing应用,叫做MySwingApp类。当运行的时候 这个应用程序产生一个临时文件。当关闭的时候,删除这个文件。
package ex16.pyrmont.shutdownhook;
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();
}
<pre class="java" name="code"> 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();
}
}
当运行地时候,应用程序调用它的的intialize方法。这个方法在user的目录中创建一个temp.txt的临时文件。
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.");
}
}
当用户关闭应用程序的时候,应用程序必须删除临时文件。我们希望用户总是点击Exit按钮,因为这样做shutdown方法总是会被调用。然而,临时文件将不被删除,如果用户点击了框架的X按钮或者通过其他方式关闭应用程序。
清单16.3的类对此给出了一种解决方案。通过提供一个shutdown钩子来修改清单16.2的代码。shutdown hook类声明为一个内部类,这样可以访问主类的方法。在清单16.3中,shutdown钩子的的run方法调用shutdown方法,保证虚拟机关闭的时候调用这个方法。
package ex16.pyrmont.shutdownhook;
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();
}
}
}
注意代码中的intilize方法。其第一件事情是创建一个MyShutdownHook类的实例,其继承了一个线程。
// add shutdown hook
MyShutdownHook shutdownHook = new MyShutdownHook();
一旦获取了一个MyShutdownHook类的实例,通过Running的addShutDownHook方法传递这个实例,代码如下:
// 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.");
}
现在,启动代码清单16.3的小程序。检查到即使突然关闭了应用程序临时文件也会删掉。
Shutdown Hook in Tomcat
你可能期望,Tomcat自己装备一个shutdown 钩子。你可以在Catalina类中找到。这个类启动一个服务器对象,管理其他组件。CatalinaShutdownHook作为一个内部类,继承了Thread,实现了run方法调用服务器对象的stop方法。
protected class CatalinaShutdownHook extends Thread {
public void run() {
if (server != null) {
try {
((Lifecycle) server).stop();
}
catch (LifecycleException e) {
System.out.println("Catalina.stop: " + e);
e.printStackTrace(System.out);
if (e.getThrowable() != null) {
System.out.println("----- Root Cause -----");
e.getThrowable().printStackTrace(System.out);
}
}
}
}
当启动Catalina实例的时候,某个阶段实例化Shutdown钩子并添加给Runtime。在后面的章节中你将了解的更多。
Summary
有时候我们想在关闭服务前运行一些清除代码。然而,依靠用户正常的退出是不可能的。此章描述的ShutdownHook提供了一种解决方案来保证不管用户怎么退出一定会运行清除代码。