Search code examples
javakotlinspring-batchspring-scheduled

Spring batch job instance created each time application is started


I am trying to create simple batch job which will be run by scheduled method daily. Here is my sample code:

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

This job performs normally when it should run but it also starts every time application is restarted. For example when I added this code:

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

I see that job instance count is normal at first but time this method is invoked second time instance count is increased:

16:47:20.812 DEBUG   [    task-pool-3] uration$$EnhancerBySpringCGLIB$$2ff42873 : Job: customJobName instance count is: 23
16:47:20.819 DEBUG   [    task-pool-3] uration$$EnhancerBySpringCGLIB$$2ff42873 : Job instance: JobInstance: id=3520, version=0, Job=[customJobName]
16:47:20.829 DEBUG   [    task-pool-3] uration$$EnhancerBySpringCGLIB$$2ff42873 : 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$$2ff42873 : Job: customJobName instance count is: 24
16:48:20.833 DEBUG   [   task-pool-16] uration$$EnhancerBySpringCGLIB$$2ff42873 : Job instance: JobInstance: id=3863, version=0, Job=[customJobName]
16:48:20.840 DEBUG   [   task-pool-16] uration$$EnhancerBySpringCGLIB$$2ff42873 : 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}]

Is there anything I'm missing should batch job run this way? What can I do to fix this?


Solution

  • In application.properties need to set this configuration

    spring.batch.job.enabled=false