我有一个 Java 网络服务器,我从中使用 Apache commons exec 启动一个外部应用程序。在高层次上,流程是用户将指定参数并使用 POST 启动应用程序,然后会有一个状态页面,他们可以从中随时取消应用程序(它可能会运行数天)。
我像这样异步启动应用程序:
Map<String, String> procEnv = EnvironmentUtils.getProcEnvironment();
CommandLine cmdLine = CommandLine.parse(command);
DefaultExecutor executor = new DefaultExecutor();
executor.execute(cmdLine, procEnv, new DefaultExecuteResultHandler());
我知道在处理第一个 POST 请求时,我可以像这样终止进程:
executor.getWatchdog().destroyProcess();
但是我需要立即响应 POST 以便用户可以转到状态页面,这意味着我将无法DefaultExecutor
从后续请求中访问该实例。鉴于我必须假设可能有多个用户因此运行的应用程序实例不止一个,我如何让用户发送单独的请求以稍后取消执行?(如有必要,我可以在响应中包含额外数据并取消请求。)