Search code examples
springspring-bootspring-batchbatch-processing

Why do subsequent jobs become NOOPs after the first successful job?


There is one job as shown in the code below.

import kr.piehealthcare.point.core.domain.user.UserRepository
import mu.KotlinLogging
import org.springframework.batch.core.Job
import org.springframework.batch.core.Step
import org.springframework.batch.core.configuration.annotation.StepScope
import org.springframework.batch.core.job.builder.JobBuilder
import org.springframework.batch.core.repository.JobRepository
import org.springframework.batch.core.step.builder.StepBuilder
import org.springframework.batch.core.step.tasklet.Tasklet
import org.springframework.batch.repeat.RepeatStatus
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.transaction.PlatformTransactionManager

private val log = KotlinLogging.logger {  }

@Configuration
class BatchConfig(
    val jobRepository: JobRepository,
    val transactionManager: PlatformTransactionManager,
    val userRepository: UserRepository

){
    @Bean
    fun myJob(): Job {
        return JobBuilder("taskletJob", jobRepository)
            .start(myStep())
            .build()
    }

    @Bean
    fun myStep(): Step {
        return StepBuilder("taskletStep", jobRepository)
            .tasklet(myTask(), transactionManager)
            .build()
    }

    @Bean
    @StepScope
    fun myTask(): Tasklet {
        return Tasklet { _, _ ->
            log.info { "test" }
            RepeatStatus.FINISHED
        }
    }
}

Through scheduling, I want to run a job at a desired time,

 @Scheduled(cron = "* * * * * *")
    fun batchGo(){
        val jobParameters = JobParameters()
        val jobExecution: JobExecution = jobLauncher.run(batchConfig.myJob(), jobParameters)
        if (jobExecution.status == BatchStatus.COMPLETED) {
            println("batch success")
        } else {
            println("error")
        }
    }

enter image description here

When the first job succeeds with 'COMPLETED', why do the next jobs not run and become 'NOOP'?

What I want is to run the same job multiple times. how can i solve it?


Solution

  • You can set this behavior with the StepBuilder like this:

    stepBuilder.allowStartIfComplete(true)