转载自:http://www.cnblogs.com/nexiyi/p/java_add_ShutdownHook.html
在线上Java程序中经常遇到进程程挂掉,一些状态没有正确的保存下来,这时候就需要在JVM关掉的时候执行一些清理现场的代码。Java中得ShutdownHook提供了比较好的方案。
JDK在1.3之后提供了Java Runtime.addShutdownHook(Thread hook)方法,可以注册一个JVM关闭的钩子,这个钩子可以在以下几种场景被调用:
1)程序正常退出
2)使用System.exit()
3)终端使用Ctrl+C触发的中断
4)系统关闭
5)使用Kill pid命令干掉进程
注:在使用kill -9 pid是不会JVM注册的钩子不会被调用。
在JDK中方法的声明:
public void addShutdownHook(Thread hook)
hook -- 一个初始化但尚未启动的线程对象,注册到JVM钩子的运行代码。
代码示例:
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;
public class ShutdownHookTest {
// 简单模拟干活的
static Timer timer = new Timer("job-timer");
// 计数干活次数
static AtomicInteger count = new AtomicInteger(0);
/**
* hook线程
*/
static class CleanWorkThread extends Thread {
@Override
public void run() {
System.out.println("clean some work.");
timer.cancel();
try {
Thread.sleep(2 * 1000);// sleep 2s
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
// 将hook线程添加到运行时环境中去
Runtime.getRuntime().addShutdownHook(new CleanWorkThread());
System.out.println("main class start ..... ");
// 简单模拟
timer.schedule(new TimerTask() {
@Override
public void run() {
count.getAndIncrement();
System.out.println("doing job " + count);
if (count.get() == 3) { // 干了3次退出
System.exit(0);
}
}
}, 0, 2 * 1000);
}
}
kill processId
kill -2 processId
kill -15 processId
通过上述命令进行终止应用的时候,ShutdownHook函数会被调用。
使用关闭钩子注意事项:
hook线程会延迟JVM的关闭时间,所以尽可能减少执行时间。
关闭钩子中不要调用system.exit(),会卡主JVM的关闭过程。但是可以调用Runtime.halt()
不能在钩子中进行钩子的添加和删除,会抛IllegalStateException
在system.exit()后添加的钩子无效,因为此时JVM已经关闭了。
当JVM收到SIGTERM命令(比如操作系统在关闭时)后,如果钩子线程在一定时间没有完成,那么Hook线程可能在执行过程中被终止。
Hook线程也会抛错,若未捕获,则钩子的执行序列会被停止。
404

被折叠的 条评论
为什么被折叠?



