简体   繁体   中英

Is it this Spring Batch job scheduling CRON expression correct?

I am working on a Spring Batch application and I have to schedule a job in this way: my job have to be run every Sunday night at 01:30 am.

Checking on Spring documentation, here: https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions

It show this diagram:

 ┌───────────── 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)
 │ │ │ │ │ │
 * * * * * *

So I was thinking to annotate the method that will define my job run in this way:

@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");
    }

Is it correct? It will be runned every Sunday (day 7) at 01:30 am?

You can verify your cron expression by using the spring provided APIs, please see some code snippet for your reference.

You can pass desired date and desired cronExpression to the below method,

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);
    }
}

The output similar to this,

**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

Just to unit test your spring boot cron expression.

They do give example with 6am, which is 0 0 6,19 * * * 6:00 AM and 7:00 PM every day so I assume you should do @Scheduled(cron = "0 30 1 * * 7") instead of @Scheduled(cron = "0 30 01 * * 7") just to be on the safe side. Other than that it looks good to me.

Also to test your scheduler, I suppose you can deploy empty method in one of your environments, which is only logging some text, without performing actual work. Obviously you can change the time to something sooner than the next Sunday 1:30am.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM