개요 (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)
@EnableBatchProcessing
사용하지 않기
Remove @EnableBatchProcessing. I had to use @EnableBatchProcessing because I'm using multiple datasources, and it was necessary to specify the datasource and transactionManager.- 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. - 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)
'개발 > Java,Spring' 카테고리의 다른 글
Spring Boot 2.x에서 3.x로 업데이트 (0) | 2023.04.04 |
---|---|
Spring boot 2.4.5 @AuthenticationPrincipal occurs null (1) | 2021.04.30 |
우아한 테크세미나 - 스프링 배치 (3) | 2019.09.27 |
SpringSecurity에서 anonymous() (2) | 2019.08.15 |
Spring Boot 2.x, 3.x Security에서 hasPermission 사용 (0) | 2019.08.11 |