[Spring] Batch 5.0 변경 사항 & initialize-schema 에러

박성우·2023년 8월 31일
0

Spring

목록 보기
10/10

spring.batch.jdbc.initialize-schema 설정이 동작하지 않는 이슈가 발생하여 이유를 알아보니 Spring Batch 5.0부터 기존과 다른 변경이 생겼으며 간단히 정리해보면,

  • SpringBoot 3.0부터 @EnableBatchProcessing 권장되지 않음

  • DefaultBatchConfigration 클래스 추가

@EnableBatchProcessing을 사용할 경우 기존 Spring Batch가 제공하는 자동 설정들을 사용할 수 없다고 한다.

DefaultBatchConfigration를 이용하면 기존에@EnableBatchProcessing을 통해 등록된 Bean 및 다양한 메소드들을 제공하여 이를 상속받고 커스터마이징 할 수 있다.

❗문제는 @EnableBatchProcessing 이나 DefaultBatchConfigration 를 사용할 경우 BatchAutoConfiguration 클래스에 달려있는 "@ConditionalOnMissingBean(value = DefaultBatchConfiguration.class, annotation = EnableBatchProcessing.class)" 에 의해 JobLauncherApplicationRunner 및 빈이 등록되지 않는다.

결국 @EnableBatchProcessing 이나 DefaultBatchConfigration을 사용할 경우 자동 설정을 밀어내고 애플리케이션의 설정을 커스텀하는 용도로 사용된다고 한다.

위와 같은 이유로 spring.batch.jdbc.initialize-schema 같은 설정 또한 동작하지 않는 것이였다.

따라서, @EnableBatchProcessing와 DefaultBatchConfigration 를 사용하지 않고 JobLauncherApplicationRunner를 직접 구현하는 수 밖에 없었으며, 기존의 JobLauncherApplicationRunner를 그대로 가져와서 구현하니 문제가 해결되었다.

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

그 외에도 자잘한 변경 사항이 있는데,

  • StepBuilderFactory, JobBuilderFactory Deprecated
    -> StepBuilder, JobBuilder 사용
return jobBuilderFactory.get("Job") // 기존
...

return new JobBuilder("Job", jobRepository) // 변경
...
  • TransactionManager 명시
...
.chunk(1000) // 기존
...

...
.chunk(1000, platformTransactionManager) // 변경
...
  • 다중 Job 실행 불가

Job 이 여러 개일 경우 spring.batch.job.name을 통해 명시해줘야 한다.


Reference

https://www.baeldung.com/spring-boot-3-migration#spring-batch
https://songkg7.github.io/posts/Spring-Batch-Changes/

profile
Backend Developer

1개의 댓글

comment-user-thumbnail
2024년 4월 18일

좋은 내용 잘 보고 갑니다.

감사합니다!!

답글 달기