如何正确调度这个5分钟的春季批处理作业?为什么要立即开始,而不是等待设定的时间?



我正在开发一个Spring批处理应用程序,我发现在正确调度作业时遇到了一些困难。

我有这个班,我的工作安排:

/**
* This bean schedules and runs our Spring Batch job.
*/
@Component
@Profile("!test")
public class SpringBatchExampleJobLauncher {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringBatchExampleJobLauncher.class);
@Autowired
@Qualifier("launcher")
private JobLauncher jobLauncher;

@Autowired
@Qualifier("updateNotaryDistrictsJob")
private Job updateNotaryDistrictsJob;
@Autowired
@Qualifier("updateNotaryListInfoJob")
private Job updateNotaryListInfoJob;

@Scheduled(cron = "0 */5 * * * *")
public void runUpdateNotaryDistrictsJob() {
LOGGER.info("SCHEDULED run of updateNotaryDistrictsJob STARTED");
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try {
jobLauncher.run(updateNotaryDistrictsJob, jobParameters);
}catch (Exception ex){
LOGGER.error(ex.getMessage());
}
}

}

你可以看到在我的runUpdateNotaryDistrictsJob()方法:

@Scheduled(cron = "0 */5 * * * *")

为了启动我的updateNotaryDistrictsJob每5分钟一次

问题是,当我在调试模式下运行应用程序时,我可以看到作业立即执行(它在第一个断点上停止)。它似乎没有等待由cron表达式设置的5分钟。

怎么了?我该如何解决这个问题?

cron表达式0 */5 * * * *不像您期望的那样读取。它似乎不是等待由cron表达式设置的5分钟,这不是cron表达式所定义的。从每小时的第0分钟开始,cron表达式将在第0秒每5分钟运行一次,这意味着它不会在服务启动后等待5分钟。例如,如果您在10:22启动它,它将在10:25运行。

如果您确实需要在服务启动后等待5分钟,您应该考虑使用@Scheduled(fixedRate = 5000, initialDelay = 5000)

最新更新