Determines the exit status in the Java close hook thread
•
Java
I want to determine the exit status of the process when shutdown is pending
I want a logic based on status codes (0 or non-zero)
(for example, if it is zero, no other non-zero alert email will be sent)
Do you know I can get this information?
Solution
I tried to override the securitymanager checkexit (int status) method – if system If exit (status) is called arbitrarily, this method is valid - however, when the application exits "normal" (no active thread), it will not set the state, or an error will kill the virtual machine
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); } } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码