我想确定关闭挂钩运行时过程的退出状态。
我想有一个基于状态码(0或非零)的逻辑
(例如:如果零不执行其他操作,则非零则发送警报电子邮件)
您知道我如何获得此信息吗?
我试图覆盖该SecurityManager checkExit(int status)方法- 如果System.exit(status)在任何地方都显式调用此方法,则该方法有效- 但是,当应用程序“正常”退出(无活动线程)或错误导致VM退出时,它不会设置状态。
SecurityManager
checkExit(int status)
System.exit(status)
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.security.Permission; public class ExitChecker { public ExitChecker() { System.setSecurityManager(new ExitMonitorSecurityManager()); Runtime.getRuntime().addShutdownHook(new Thread(new MyShutdownHook())); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String line = ""; while (!line.equalsIgnoreCase("Q")) { try { System.out.println("Press a number to exit with that status."); System.out.println("Press 'R' to generate a RuntimeException."); System.out.println("Press 'O' to generate an OutOfMemoryError."); System.out.println("Press 'Q' to exit normally."); line = input.readLine().trim(); processInput(line); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } } } private void processInput(String line) { if (line.equalsIgnoreCase("Q")) { // continue, will exit loop and exit normally } else if (line.equalsIgnoreCase("R")) { throwRuntimeException(); } else if (line.equals("O")) { throwError(); } else { // try to parse to number try { int status = Integer.parseInt(line); callExit(status); } catch(NumberFormatException x) { // not a number.. repeat question... System.out.println("\nUnrecognized input...\n\n"); } } } public void callExit(int status) { System.exit(status); } public void throwError() { throw new OutOfMemoryError("OutOfMemoryError"); } public void throwRuntimeException() { throw new RuntimeException("Runtime Exception"); } public static void main(String[] args) { new ExitChecker(); } private static class ExitMonitorSecurityManager extends SecurityManager { @Override public void checkPermission(Permission perm) { //System.out.println(perm.getName()); //System.out.println(perm.getActions()); } @Override public void checkPermission(Permission perm, Object context) { //System.out.println(perm.getName()); //System.out.println(perm.getActions()); } @Override public void checkExit(int status) { System.out.println("Setting exit value via security manager..."); MyShutdownHook.EXIT_STATUS = status; } } private static class MyShutdownHook implements Runnable { public static Integer EXIT_STATUS; public void run() { System.out.println("In MyShutdownHook - exit status is " + EXIT_STATUS); } } }