@Component
public class MyScheduledJob {
private LocalDateTime runNextTime = null;
private boolean isFileFound = false;
@Scheduled(/**here comes your original cron expression: 7 am to 9 pm with an interval of 15 minutes as you say**/)
public void runMe() {
if(isFileFound && LocalDateTime.now().isBefore(runNextTime)) {
// do not run a real job processing
return;
}
isFileFound = checkWhetherFileExists();
if(isFileFound) {
runNextTime = calculateWhenDoYouWantToStartRunningTheActualJobProcessingNextTime();
}
... do actual job processing... Its not clear from the question whether it should do something if the file is not found as well, but I'm sure you've got the point ...
}
}
2条答案
按热度按时间clj7thdc1#
可能最简单的方法是在业务逻辑级别实现它。spring提供了一种运行周期性任务的方法,这是真的,但是如果满足了业务案例(找到了文件),它就不能在一段时间内停止作业。
话虽如此,您可以按如下方式实施计划作业:
因为bean是一个单例,所以您可以安全地为它创建一个状态,没有人会改变这个状态。
wvyml7n52#
你可以这样做。您可以编写逻辑来检查此方法中的文件。
请参阅此以了解更多详细信息。