我有一个批处理过程,可以将WAV顺序转换为MP3。问题在于,几千个文件之后,仍有太多文件处于打开状态,并且该文件的运行超出了文件限制。
这样做的原因是由于SystemCommandTasklet中的代码:
FutureTask<Integer> systemCommandTask = new FutureTask<Integer>(new Callable<Integer>() {
public Integer call() throws Exception {
Process process = Runtime.getRuntime().exec(command, environmentParams, workingDirectory);
return process.waitFor();
}
});
这使我不得不依靠JVM清理进程,使文件保持打开状态等令人讨厌的副作用。
我将其重写为:
FutureTask<Integer> systemCommandTask = new FutureTask<Integer>(new Callable<Integer>() {
public Integer call() throws Exception {
Process process = Runtime.getRuntime().exec(command, environmentParams, workingDirectory);
int status = process.waitFor();
process.getErrorStream().close();
process.getInputStream().close();
process.getOutputStream().flush();
process.getOutputStream().close();
process.destroy();
return status;
}
});
我有95%的把握可以在我的Mac上运行(感谢lsof),但是我如何进行一个可以在任何系统上运行的适当测试,以证明我正在尝试的工作实际上是可行的?
最佳答案
证明将是困难的。但是...
创建一个(虚拟)命令,该命令不会做很多事,但会像真实的东西一样锁定文件。这可以确保您的测试不依赖于实际使用的命令。
创建一个测试,该测试使用旧版本启动系统命令任务,但使用DummyCommand。使它经常启动任务,直到获得预期的异常为止。调用所需的任务数N
更改测试以启动100xN任务。
将任务更改为新版本。如果测试变为绿色,则应合理确定代码可以正常工作。