每X秒运行一次java代码,但如果代码有效,请不要等待TimeUnit.sleep



我正在尝试每 10 分钟运行一次脚本 sh,脚本的目的是在服务器上设置一个应用程序。 因此,我定义了一个测试应用程序是否在服务器上成功部署的方法。

我的代码如下:运行在服务器上部署应用程序的脚本sh,等待10分钟,通过调用返回布尔值的方法testDeployment((在服务器上测试部署。

for (int i=0; i<=x;i++) {
Runtime.getRuntime().exec("cmd.exe /c start "+"\script.sh");
TimeUnit.SECONDS.sleep(300);
testDeploy=testDep.testDeployment();
}

但是我需要的是每 20 秒测试一次部署,如果应用程序部署成功,我就不必等待 10 分钟。

do {            
testDeploy=testDep.testDeployment();
}while(testDeploy==false&& "while time is inferior than 10 minutes");

请提供任何帮助

也许是这样的:

Instant start = Instant.now();
boolean testDeploy = false;
do {
// ...
testDeploy = testDep.testDeployment();
} while (!testDeploy && Duration.between(start, Instant.now()).toMinutes() < 10);

相关内容

最新更新