【问题标题】:Is it this Spring Batch job scheduling CRON expression correct?这个 Spring Batch 作业调度 CRON 表达式是否正确?
【发布时间】:2021-12-24 18:43:28
【问题描述】:

我正在处理 Spring Batch 应用程序,我必须以这种方式安排工作:我的工作必须在每个星期日晚上 01:30 am 运行。

查看 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 天)上午 1:30 运行?

【问题讨论】:

  • 根据文档看起来没问题...
  • 唯一我不确定的是小时数应该是 01 还是 1
  • 他们确实举了早上 6 点的例子,也就是 0 0 6,19 * * * 6:00 AM and 7:00 PM every day,所以我认为你应该这样做 @Scheduled(cron = "0 30 1 * * 7")
  • 你可以使用bradymholt.github.io/cron-expression-descriptor这样的工具

标签: java spring spring-boot spring-batch


【解决方案1】:

他们确实举了早上 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:30 更早的时间。

@Scheduled(cron = "0 30 1 * * 7")
public void runSpringBatchExampleJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        LOGGER.info("Spring Batch example job was started");
}

【讨论】:

    【解决方案2】:

    您可以使用 spring 提供的 API 来验证您的 cron 表达式,请参阅一些代码 sn-p 供您参考。

    您可以将所需的日期和所需的 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 boot cron 表达式进行单元测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      相关资源
      最近更新 更多