我知道 scheduleProcess
在开始时执行一次。
然后它安排任务每 3 分钟使用 cron 执行一次。
第一次执行 cron 后一切正常。
但每次都会有两倍的 otherMethod
调用。
我使代码更简单。
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
public void scheduleProcess() {
Runnable otherMethod = () -> orderSending();
Runnable task = () -> runAs("status", otherMethod);
scheduledTasks.add(taskScheduler.schedule(task, new CronTrigger("* */3 * * * *")));
}
public void runAs(String processName, Runnable runnable) {
System.out.println(processName);
try {
runnable.run();
} finally {
System.out.println("test2");
}
}
public void orderSending() { // this executes times two every cron tick
System.out.println("...");
}
Java 8,SpringBoot 2.0.3,在 tomcat 8 中作为 war 运行。
最佳答案
计划:
0 0/3 * * * *
使代码按预期运行。
计划:
* */3 * * * *
做出这种奇怪的行为。
关于java - Runnable 执行时间为每个 cron tick 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58397950/