How to trigger a scheduled Spring Batch Job?

后端 未结 4 1389
日久生厌
日久生厌 2021-02-06 00:51

I want to be able to start my job with a REST controller, then when the job is started, it should run on a scheduled basis, until i stop it again with REST.

So this is m

4条回答
  •  梦如初夏
    2021-02-06 01:38

    @Scheduled is defined on a method and not on a Bean. So create a new Class which will be a Bean

    public class BatchConfiguration {
    ...
    @Bean
    public Job job() {
        return new Job();
    }
    

    new Class:

    public class Job {
    
    @Scheduled(cron = "0/5 * * * * ?")
    public Job job() {
        return jobBuilderFactory.get("job")
                .incrementer(new RunIdIncrementer())
                .flow(step1())
                .end()
                .build();
    }
    

提交回复
热议问题