【问题标题】:Close current Java app until another send a signal关闭当前 Java 应用程序,直到另一个应用程序发送信号
【发布时间】:2015-06-25 23:22:37
【问题描述】:

我想知道是否可以关闭当前的java app util另一个已经完成了一些任务,我的代码是这样的:

private static void callJar(String jardir) throws IOException, InterruptedException {
    // jardir contains the excecution command 
    Process p = Runtime.getRuntime().exec(jardir);
    synchronized (p) {
        // Here I want to wait for p for a signal but not when p has finished
        // but waitFor() do the second
        p.waitFor();
    }
    // If the other jar is correctly loaded, close this jar
    System.exit(0);
}

字符串jardir 包含将启动我将要监听的另一个进程的执行命令,如下所示:

jardir = "javaw -jar \\path\\to\\anotherjar.jar"

目前,callJar() 打开这个进程,然后关闭当前进程,直到我启动的进程终止。换句话说,关闭 A 直到关闭 B。

但我想做的是关闭A,直到B发出信号(B将继续存在)。

有没有办法从我启动的进程中监听信号?

【问题讨论】:

  • 你看过 Java 的并发 API 吗? bharatonjava.wordpress.com/2013/05/30/…
  • @Chris 如果我错了,请纠正我,但这只能在一个罐子内交流许多线程,不是吗?就我而言,我有 2 个独立的 jars 正在运行,我想同时进行通信
  • 你能定义“罐子”吗?我认为您所描述的不是大多数人在阅读“jar”时会想到的。您可能指的是“过程”。
  • 我猜他们是两个不同的过程,就像你说的那样。我叫“jar”,因为那是我打开的,但它们运行在两个不同的虚拟机中......

标签: java jar process


【解决方案1】:

在寻找答案后,我终于找到了解决方案,也许这对某人有用,所以这就是我所做的:

基于 this answerthis site,我选择使用 java.net 库在两个 Java 应用程序之间创建通信。

在进程 A 中,我有一个方法可以创建服务器通信并等待它收到来自进程 B 的消息...

private static boolean listen2ExternalProcess() {
    ServerSocket server = null;
    Socket serverSocked = null;
    String line;
    BufferedReader inputReader = null;
    try {
        server = new ServerSocket(3333);
        serverSocked = server.accept();
        inputReader = new BufferedReader(
            new InputStreamReader(serverSocked.getInputStream()));

        while (true) {
            line = inputReader.readLine();
            log.info("Client says: " + line);
            if (line.equals("Kill yourself :D")) {
                return true;
            }
        }
    } catch (UnknownHostException e) {
        log.error("Don't know about this, " + e);
        return false;
    } catch (IOException e) {
        log.error("Couldn't get IO for the connection, " + e);
        return false;
    } finally {
        try {
            if(serverSocked != null) serverSocked.close();
            if(inputReader != null) inputReader.close();
        } catch (IOException ex) {
            log.error("Couldn't get IO for the connection, " + ex);
            return false;
        }
    }
}

如果收到消息,这个方法将返回true,然后我可以继续终止进程A。

在进程 B 中,我有一个方法,可以在需要时向套接字发送消息...

public static void talk2ExternalProcess() {
    Socket socket = null;
    BufferedWriter outputWriter = null;
    try {
        socket = new Socket("localhost", 3333);
        outputWriter = new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream()));
    } catch (UnknownHostException e) {
        log.error("Don't know about host: localhost, " + e);
    } catch (IOException e) {
        log.error("Couldn't get IO for the connection to localhost, " + e);
    }

    if (socket != null && outputWriter != null) {
        try {
            outputWriter.write("Kill yourself :D");
        } catch (UnknownHostException e) {
            log.error("Trying to connect to unkown host: " + e);
        } catch (IOException e) {
            log.error("IO Exception: " + e);
        } finally {
            try {
                outputWriter.close();
                socket.close();
            } catch (IOException ex) {
                log.error("IO Exception: " + ex);
            }
        }
    } else {
        log.warn("null socket or outputwriter");
    }
}

最后,我只是将callJar 方法更改为这样的:

private static void callJar(String jardir) throws IOException {
    Runtime.getRuntime().exec(jardir);
    if (listen2ExternalProcess()) {
        System.exit(0);
    } else {
        log.warn("Something went wrong...");
    }
}

我想找到一个更简单的答案,但目前这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 2019-08-01
    • 2016-08-16
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    相关资源
    最近更新 更多