개발/Java,Spring

SpringBatch에서 @EnableBatchProcessing때문에 Job이 실행되지 않는 문제

호돌맨 2023. 6. 1. 10:20

개요 (Overview)

SpringBatch 3.x에서 Job이 실행되지 않는 문제가 발생했다.

We encountered an issue where the Job was not running in SpringBatch 3.x.

문제 (Problem)

SpringBoot 3.0 (정확히 v3.0.0-M5)는 부터는 @EnableBatchProcessing을 사용하면 BatchAutoConfiguration.java에서 jobLauncherApplicationRunner bean 생성을 하지 않는다. 나는 Multiple datasource를 사용하고 있기 때문에 datasource, transactionManager를 지정하기 위해 반드시 사용해야 했다.

As of SpringBoot 3.0 (precisely v3.0.0-M5), when @EnableBatchProcessing is used, the jobLauncherApplicationRunner bean is not created in BatchAutoConfiguration.java. I have to use it due to multiple datasources in use, to specify the datasource and transactionManager.

해결 (Solution)

  1. @EnableBatchProcessing 사용하지 않기
    Remove @EnableBatchProcessing. I had to use @EnableBatchProcessing because I'm using multiple datasources, and it was necessary to specify the datasource and transactionManager.
  2. DefaultBatchConfiguration 사용하기
    해당 패치의 commit 메시지에는 DefaultBatchConfiguration sub-class로서 별도 구성을 할 수 있다고 한다. 그런데 DataSource, PlatformTransactionManager를 별도로 지정하기 위해서는 bean으로 등록하는 방법밖에 없다. 예전에는 setDataSource를 통해 override할 수 있던것 같은데 지금은 안된다. 어쨌거나 bean 등록 방식을 사용하려면 애시당초 @EnableBatchProcessing 를 고민하지도 않았겠지.
    The commit message of this patch states that you can set up a separate configuration as a subclass of DefaultBatchConfiguration. However, if you want to specify DataSource and PlatformTransactionManager separately, you have to register them as beans. It seems that in the past you could override this using setDataSource, but that's not possible now. Regardless, if we were to use the bean registration method, we wouldn't have even considered @EnableBatchProcessing in the first place.
  3. jobLauncherApplicationRunner bean 등록
    아래와같이 jobLauncherApplicationRunner bean을 따로 등록한다.
    I register the jobLauncherApplicationRunner bean separately.
@Configuration
@EnableConfigurationProperties(BatchProperties.class)
public class BatchConfig {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "spring.batch.job", name = "enabled", havingValue = "true", matchIfMissing = true)
    public JobLauncherApplicationRunner jobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer jobExplorer,
                                                                     JobRepository jobRepository, BatchProperties properties) {
        JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(jobLauncher, jobExplorer, jobRepository);
        String jobNames = properties.getJob().getName();
        if (StringUtils.hasText(jobNames)) {
            runner.setJobName(jobNames);
        }
        return runner;
    }
}

관련 링크 (Related Links)