Search code examples
springspring-bootkotlinspring-batchbatch-processing

Why doesn't batch run in kotlin springBoot?


I coded as below in 'job/JobFile'

import mu.KotlinLogging
import org.springframework.batch.core.Job
import org.springframework.batch.core.Step
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing
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.core.io.FileSystemResource
import org.springframework.transaction.PlatformTransactionManager


private val log = KotlinLogging.logger {  }

@EnableBatchProcessing
@Configuration
class BatchConfig(
    val jobRepository: JobRepository,
    val transactionManager: PlatformTransactionManager
){
    @Bean
    fun taskletJob(): Job {
        return JobBuilder("taskletJob", jobRepository)
            .start(Step())
            .build()
    }

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

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

And, after setting the ' application.yml ' as follows, when the server is started, 'test batch' log is expected, but it does not work, how to fix it, so that the batch works?

spring:
  profiles:
    group:
    ........
  batch:
    jdbc:
      initialize-schema: always
    job:
      enabled: true

I'm a student studying kotlin springBoot, it may be an easy question, but it's difficult for me, I'm new to it, so please give me some advice.


Solution

  • You are using @EnableBatchProcessing with Spring Boot 3. You should remove that annotation, otherwise the Spring Batch auto-configuration from Spring Boot (meta-data table creation, automatic job execution at startup, etc) will back off.

    This is mentioned in the migration guide of Spring Boot 3: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#spring-batch-changes