Java 如何杀死在我的测试套件下创建的所有子进程?

Java 如何杀死在我的测试套件下创建的所有子进程?,java,aspectj,apache-commons-exec,aspectj-maven-plugin,Java,Aspectj,Apache Commons Exec,Aspectj Maven Plugin,我正在使用maven(插件版本1.7)和Aspectj-1.8.3 我的场景如下:我有一个要测试的安装程序jar。 安装程序正在使用另一个jar,my-common.jar库,它包装了Apache的公用程序commons-exec-1.3,并使用它来执行命令,我编写的方法如下所示: public static int execCommand(String command) throws ExecuteException, IOException, InterruptedException {

我正在使用maven(插件版本1.7)和Aspectj-1.8.3

我的场景如下:我有一个要测试的安装程序jar。 安装程序正在使用另一个jar,my-common.jar库,它包装了Apache的公用程序commons-exec-1.3,并使用它来执行命令,我编写的方法如下所示:

public static int execCommand(String command) throws ExecuteException, IOException, InterruptedException {
    logger.debug("About to execute command: ", command);
    CommandLine commandLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    executor.setWatchdog(watchdog);
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    executor.execute(commandLine, resultHandler);
    return 0;
}
static Collection<Integer> PidsToKill = new LinkedList<Integer>();

@After("!cflow(within(IntegrationTestsAspects)) && call(* java.lang.UNIXProcess.destroyProcess(int))&&args(pid)")
public void foo3(int pid) {
    PidsToKill.add(new Integer(pid));
}
问题是,由于我的测试执行另一个jar,我的意思是,安装程序,安装程序jar执行另一个jar(让我们命名为app.jar),然后,安装程序终止,app.jar继续运行(首先,安装程序正在进行安装并准备环境,然后,他执行app.jar),当测试套件完成时,应用程序jar不会被终止(这是我的意图,也是在生产环境中应该如何终止的)

全局目标是杀死在集成测试套件下创建的所有进程

我的解决方案: 由于进程id只暴露于
java.lang.UNIXProcess
中,我想收集所有进程id,然后在测试套件结束时手动终止它们

我想把这样一个方面:

public static int execCommand(String command) throws ExecuteException, IOException, InterruptedException {
    logger.debug("About to execute command: ", command);
    CommandLine commandLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    executor.setWatchdog(watchdog);
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    executor.execute(commandLine, resultHandler);
    return 0;
}
static Collection<Integer> PidsToKill = new LinkedList<Integer>();

@After("!cflow(within(IntegrationTestsAspects)) && call(* java.lang.UNIXProcess.destroyProcess(int))&&args(pid)")
public void foo3(int pid) {
    PidsToKill.add(new Integer(pid));
}
静态集合PidsToKill=newlinkedlist();
@在(“!cflow(in(IntegrationTestsAspects))&&call(*java.lang.UNIXProcess.destroyProcess(int))&&args(pid)”之后
公共空间3(内部pid){
add(新整数(pid));
}
这就是我解决问题的想法,不需要重新设计代码的某些部分。 因此,在某种程度上,我正在寻找一种方法来确保在集成测试套件下创建的所有子进程都被终止


欢迎任何解决方案。

我知道如何编织rt.jar,我已经手动完成了。但请更具体一些。为什么要编织rt.jar?也许有另一种方法可以实现你想要的。你能展示你的外貌吗?结果应该是什么?如果您有一个woven Java运行时,您能保证每个人都在生产中使用它吗?请详细说明这样一个复杂的问题,如果你只写一行,不要指望有人免费提供完整的解决方案。附言:你是否曾经成功地手工编织过rt.jar,并且知道如何编织以及以后如何使用它?@kriegaex我更新了我的问题,谢谢。