这个Spring批量作业调度CRON表达式正确吗?
Is it this Spring Batch job scheduling CRON expression correct?
我正在处理 Spring 批处理申请,我必须以这种方式安排工作:我的工作必须是每个星期日晚上 01:30 上午 运行。
正在查看 Spring 文档,此处:https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions
显示此图:
┌───────────── second (0-59)
│ ┌───────────── minute (0 - 59)
│ │ ┌───────────── hour (0 - 23)
│ │ │ ┌───────────── day of the month (1 - 31)
│ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
│ │ │ │ │ ┌───────────── day of the week (0 - 7)
│ │ │ │ │ │ (or MON-SUN -- 0 or 7 is Sunday)
│ │ │ │ │ │
* * * * * *
所以我想用这种方式注释定义我的工作的方法运行:
@Scheduled(cron = "0 30 01 * * 7")
public void runSpringBatchExampleJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
LOGGER.info("Spring Batch example job was started");
List<NotaryDistrict> notaryDistrictsList = new ArrayList<NotaryDistrict>();
executionContext.put("notaryDistrictsList", notaryDistrictsList);
jobLauncher.run(job, newExecution());
LOGGER.info("Spring Batch example job was stopped");
}
是否正确?每周日(第 7 天)01:30 上午 运行?
他们给出了早上 6 点的例子,也就是 0 0 6,19 * * * 6:00 AM and 7:00 PM every day
所以我认为你应该 @Scheduled(cron = "0 30 1 * * 7")
而不是 @Scheduled(cron = "0 30 01 * * 7")
只是为了安全起见。除此之外,我觉得还不错。
另外,为了测试您的调度程序,我想您可以在您的一个环境中部署空方法,该方法仅记录一些文本,而不执行实际工作。显然,您可以将时间更改为比下周日更早的时间 1:30am。
@Scheduled(cron = "0 30 1 * * 7")
public void runSpringBatchExampleJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
LOGGER.info("Spring Batch example job was started");
}
您可以使用 spring 提供的 API 来验证您的 cron 表达式,请查看一些代码片段供您参考。
您可以将所需的日期和所需的 cronExpression 传递给以下方法,
public static void printNextTriggerTime(String cronExpression) {
CronExpression expression = CronExpression.parse(cronExpression);
LocalDateTime result = LocalDateTime.now();
for (int i = 0; i < 10; i++) {
result = expression.next(result);
System.out.println("Next Cron runs at: " + result);
}
}
类似这样的输出,
**printNextTriggerTime("0 30 01 * * 7");**
Next Cron runs at: 2021-11-14T01:30
Next Cron runs at: 2021-11-21T01:30
Next Cron runs at: 2021-11-28T01:30
Next Cron runs at: 2021-12-05T01:30
Next Cron runs at: 2021-12-12T01:30
Next Cron runs at: 2021-12-19T01:30
Next Cron runs at: 2021-12-26T01:30
Next Cron runs at: 2022-01-02T01:30
Next Cron runs at: 2022-01-09T01:30
Next Cron runs at: 2022-01-16T01:30
只是为了对您的 spring 启动 cron 表达式进行单元测试。
我正在处理 Spring 批处理申请,我必须以这种方式安排工作:我的工作必须是每个星期日晚上 01:30 上午 运行。
正在查看 Spring 文档,此处:https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions
显示此图:
┌───────────── second (0-59)
│ ┌───────────── minute (0 - 59)
│ │ ┌───────────── hour (0 - 23)
│ │ │ ┌───────────── day of the month (1 - 31)
│ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
│ │ │ │ │ ┌───────────── day of the week (0 - 7)
│ │ │ │ │ │ (or MON-SUN -- 0 or 7 is Sunday)
│ │ │ │ │ │
* * * * * *
所以我想用这种方式注释定义我的工作的方法运行:
@Scheduled(cron = "0 30 01 * * 7")
public void runSpringBatchExampleJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
LOGGER.info("Spring Batch example job was started");
List<NotaryDistrict> notaryDistrictsList = new ArrayList<NotaryDistrict>();
executionContext.put("notaryDistrictsList", notaryDistrictsList);
jobLauncher.run(job, newExecution());
LOGGER.info("Spring Batch example job was stopped");
}
是否正确?每周日(第 7 天)01:30 上午 运行?
他们给出了早上 6 点的例子,也就是 0 0 6,19 * * * 6:00 AM and 7:00 PM every day
所以我认为你应该 @Scheduled(cron = "0 30 1 * * 7")
而不是 @Scheduled(cron = "0 30 01 * * 7")
只是为了安全起见。除此之外,我觉得还不错。
另外,为了测试您的调度程序,我想您可以在您的一个环境中部署空方法,该方法仅记录一些文本,而不执行实际工作。显然,您可以将时间更改为比下周日更早的时间 1:30am。
@Scheduled(cron = "0 30 1 * * 7")
public void runSpringBatchExampleJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
LOGGER.info("Spring Batch example job was started");
}
您可以使用 spring 提供的 API 来验证您的 cron 表达式,请查看一些代码片段供您参考。
您可以将所需的日期和所需的 cronExpression 传递给以下方法,
public static void printNextTriggerTime(String cronExpression) {
CronExpression expression = CronExpression.parse(cronExpression);
LocalDateTime result = LocalDateTime.now();
for (int i = 0; i < 10; i++) {
result = expression.next(result);
System.out.println("Next Cron runs at: " + result);
}
}
类似这样的输出,
**printNextTriggerTime("0 30 01 * * 7");**
Next Cron runs at: 2021-11-14T01:30
Next Cron runs at: 2021-11-21T01:30
Next Cron runs at: 2021-11-28T01:30
Next Cron runs at: 2021-12-05T01:30
Next Cron runs at: 2021-12-12T01:30
Next Cron runs at: 2021-12-19T01:30
Next Cron runs at: 2021-12-26T01:30
Next Cron runs at: 2022-01-02T01:30
Next Cron runs at: 2022-01-09T01:30
Next Cron runs at: 2022-01-16T01:30
只是为了对您的 spring 启动 cron 表达式进行单元测试。