如果应用程序重新启动错过了计划启动,如何启动春季计划的作业

时间:2019-12-01 10:33:07

标签: spring kotlin spring-batch scheduling spring-scheduled

我需要创建计划的批处理作业以每晚处理一些数据。我正在尝试使用春季批处理作业,并使用cron表达式计划它。它应该每天晚上1:00 AM运行。我对如果应用程序在那时重新启动而错过启动时间会发生什么感兴趣。例如,假设应用程序关闭了30分钟,它在00:45 AM关闭并在01:15 AM开始启动,这将安排作业开始,或者我需要手动运行它。春天可以处理这个吗?我找不到有关此的任何信息。是否有可能自动管理此类案件?下面,我提供工作的示例配置:

const val CHUNK_SIZE = 20
const val JOB_NAME = "reallyAwesomeJobName"

@Configuration
class ReallyAwesomeJobConfiguration(
    private val jobLauncher: JobLauncher,
    private val jobBuilderFactory: JobBuilderFactory,
    private val stepBuilderFactory: StepBuilderFactory
) {

    @Bean
    fun reallyAwesomeJob(): Job = jobBuilderFactory
        .get(JOB_NAME)
        .incrementer(RunIdIncrementer())
        .start(step1())
        .next(step2())
        .build()

    @Bean
    fun step1(): Step = stepBuilderFactory
        .get("step1")
        .chunk<ItemDto, ItemProcessingResultDto>(CHUNK_SIZE)
        .reader(reader())
        .processor(itemProcessor1())
        .writer(writer())
        .build()

    @Bean
    fun step2(): Step = stepBuilderFactory
        .get("step2")
        .chunk<ItemDto, ItemProcessingResultDto>(CHUNK_SIZE)
        .reader(reader())
        .processor(itemProcessor2())
        .writer(writer())
        .build()

    @Bean
    fun reader() = CustomItemReader()

    @Bean
    fun itemProcessor1() = ItemProcessor1()

    @Bean
    fun itemProcessor2() = ItemProcessor2()

    @Bean
    fun writer() = CustomItemWriter()

    @Scheduled(cron = "0 0 1 * * *")
    fun perform() {
        jobLauncher.run(
            reallyAwesomeJob(),
            JobParametersBuilder()
                .addDate("executionDate", Date())
                .toJobParameters()
        )
    }
}

0 个答案:

没有答案