12

我想在关闭挂钩运行时确定进程的退出状态。

我想要一个基于状态码(0或非零)的逻辑

(例如:如果零不做任何其他非零发送警报电子邮件)

你知道我怎样才能得到这些信息吗?

4

4 回答 4

7

我试图覆盖该SecurityManager checkExit(int status)方法 - 如果System.exit(status)在任何地方显式调用此方法 - 但是,当应用程序“正常”退出(没有活动线程)或错误杀死 VM 时,它不会设置状态。

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);
        }
    }

}
于 2009-09-28T13:45:25.620 回答
3

这是一些示例代码,其中一个专用类用于System.exit通过调用doExit(int). 该类还存储退出状态并随后充当关闭挂钩。

public class ShutDownHook implements Runnable {
  private volatile Integer exitStatus;

  // Centralise all System.exit code under control of this class.
  public void doExit(int exitStatus) {
    this.exitStatus = exitStatus;
    System.exit(exitStatus); // Will invoke run.
  }

  public void run() {
    // Verify that an exit status has been supplied.
    // (Application could have called System.exit(int) directly.)
    if (this.exitStatus != null) {
      switch(exitStatus) {
        case 0: // Process based on exit status.
        // Yada yada ...
      }
    }
  }
}
于 2009-09-28T12:25:22.433 回答
1

为什么在应用程序本身中这样做?如果您的应用程序没有发送电子邮件作为正常操作的一部分,那么结合这种功能不是一个好主意,恕我直言。

我只相信从 JVM 进程设置适当的返回值,并让 shell 脚本或其他任何东西来处理电子邮件的有条件创建。

Shutdownhooks 应该只运行很短的时间,发送电子邮件可能会消耗相当长的时间。

于 2009-09-28T12:57:59.037 回答
-1

您必须将退出状态保存main到全局 ( public static) 变量中。

于 2009-09-28T12:06:30.777 回答