首页 > 解决方案 > 如何更改此 Spring Batch @Scheduled() 注释以指定执行时间和时间?

问题描述

我正在开发一个执行作业的 Spring Batch 应用程序。作业是使用 Spring CRON 表达式安排的。

在我的项目中,我有这个 SpringBatchExampleJobLauncher 类:

@Component
public class SpringBatchExampleJobLauncher {

    private static final Logger LOGGER = LoggerFactory.getLogger(SpringBatchExampleJobLauncher.class);

    private final Job job;
    private final JobLauncher jobLauncher;
    private ExecutionContext executionContext;

    @Autowired
    public SpringBatchExampleJobLauncher(@Qualifier("updateNotaryDistrictsJob") Job job, 
                                         JobLauncher jobLauncher,
                                         ExecutionContext executionContext) {
        this.job = job;
        this.jobLauncher = jobLauncher;
        this.executionContext = executionContext;
    }

    @Scheduled(cron = "0/10 * * * * *")
    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");
    }

    private JobParameters newExecution() {
        Map<String, JobParameter> parameters = new HashMap<>();

        JobParameter parameter = new JobParameter(new Date());
        parameters.put("currentTime", parameter);

        return new JobParameters(parameters);
    }
}

在此特定示例中,使用的 CRON 异常是:

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

因此该作业每 10 秒执行一次。这工作得很好,但它不适合真实的情况。我必须每天在特定的时间和分钟执行我的工作

根据这篇文章中显示的内容: Spring cron vs normal cron?

还有这个文档: https ://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html

I know that, doing in the following way, I can perform my job at 12:00 PM on the first day of every month.

"0 0 12 1 * *"

and now I have two problem:

  1. For test purposes I must perform my job something like every 18:30 of every day (basically I want do specify not only the hour but also the time of when the job must be executed. Moreover I want perform it every day and not on the first day of the month as specified by the previous expression). How can I modify this CRON expression in order to obtain the desired behavior?

  2. For a next production phase I have to change this behavior in order to perform my job at a specific day (for example Moday) of a specific week (for example every 2 weeks) in a specific hour and time. For example: the job must be performed the first Monday and the third on the month at 02:30 pm. How can I modify this CRON expression in order to obtain the desired behavior?

Thank you

标签: javaspringspring-bootcronspring-batch

解决方案


From Spring documentation, the format for Cron expressions is:

 ┌───────────── 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)
 │ │ │ │ │ │
 * * * * * *
  1. You can achieve the first case with 0 30 18 * * *. Asterisk * means every in cron expressions.
  2. For this you can use 0 30 14 ? * MON#1,MON#3. MON#X means the Xth Monday of the month. In this case we use ? for day of the month field, because it doesn't matter which day of the month the first/third Monday falls on. ? indicates no specific value.

推荐阅读