Spring 每次启动应用程序时创建批处理作业实例

Spring batch job instance created each time application is started

我正在尝试创建简单的批处理作业,它将每天按预定方法 运行。 这是我的示例代码:

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

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

    @Bean
    fun step1(): Step = stepBuilderFactory
        .get("step1")
        .chunk<DataDto, ProcessingResultDto>(CHUNK_SIZE)
        .reader(customStreamItemReader())
        .processor(customItemProcessor())
        .writer(customItemWriter())
        .build()

    @Bean
    fun customStreamItemReader() = CustomStreamItemReader()

    @Bean
    fun customItemProcessor() = CustomItemProcessor()

    @Bean
    fun customItemWriter() = CustomItemWriter()

    @Scheduled(cron = "0 0 18 * * *")
    fun performJob() {
        jobLauncher.run(
            job(),
            JobParametersBuilder()
                .addDate("executionDate", Date())
                .toJobParameters()
        )
    }

    companion object {
        const val CHUNK_SIZE = 20
        const val JOB_NAME = "customJobName"
    }
}

这个作业在它应该运行时正常执行,但它也会在每次重新启动应用程序时启动。例如,当我添加此代码时:

@Scheduled(fixedDelayString = "PT1M")
fun checkJobStatus() {
    val jobInstanceCount = jobExplorer.getJobInstanceCount(JOB_NAME)
    logger.debug("Job: $JOB_NAME instance count is: $jobInstanceCount")
    val jobInstance = jobExplorer.getLastJobInstance(JOB_NAME)
    logger.debug("Job instance: $jobInstance")
    val jobExecution = jobInstance?.let { jobExplorer.getLastJobExecution(it) }
    logger.debug("Job execution: $jobExecution")
}

我看到作业实例计数一开始是正常的,但是第二次调用此方法时实例计数增加了:

16:47:20.812 DEBUG   [    task-pool-3] uration$$EnhancerBySpringCGLIB$ff42873 : Job: customJobName instance count is: 23
16:47:20.819 DEBUG   [    task-pool-3] uration$$EnhancerBySpringCGLIB$ff42873 : Job instance: JobInstance: id=3520, version=0, Job=[customJobName]
16:47:20.829 DEBUG   [    task-pool-3] uration$$EnhancerBySpringCGLIB$ff42873 : Job execution: JobExecution: id=3520, version=2, startTime=2020-01-03 15:58:28.619, endTime=2020-01-03 15:58:28.713, lastUpdated=2020-01-03 15:58:28.714, status=COMPLETED, exitStatus=exitCode=COMPLETED;exitDescription=, job=[JobInstance: id=3520, version=0, Job=[customJobName]], jobParameters=[{run.id=18, executionDate=1577196000244}]
...
16:48:20.832 DEBUG   [   task-pool-16] uration$$EnhancerBySpringCGLIB$ff42873 : Job: customJobName instance count is: 24
16:48:20.833 DEBUG   [   task-pool-16] uration$$EnhancerBySpringCGLIB$ff42873 : Job instance: JobInstance: id=3863, version=0, Job=[customJobName]
16:48:20.840 DEBUG   [   task-pool-16] uration$$EnhancerBySpringCGLIB$ff42873 : Job execution: JobExecution: id=3863, version=2, startTime=2020-01-03 16:47:21.386, endTime=2020-01-03 16:47:21.496, lastUpdated=2020-01-03 16:47:21.497, status=COMPLETED, exitStatus=exitCode=COMPLETED;exitDescription=, job=[JobInstance: id=3863, version=0, Job=[customJobName]], jobParameters=[{run.id=19, executionDate=1577196000244}]

如果以这种方式批处理作业 运行,我是否遗漏了什么?我该怎么做才能解决这个问题?

application.properties需要设置这个配置

spring.batch.job.enabled=false