본문 바로가기

개발/Spring

[batch] spring boot3에서 배치 실행이 안됨

 

하루에 한번 open api에서 데이터를 불러와 db에 넣는 배치가 잘 돌아가지 않아 수정이 필요하다. 아직 배치를 작성해보지 않아서 공부를 하고 있다. 먼저 간단한 로그를 출력하는 job을 생성해서 테스트하는데 log가 찍히지 않았다.

@Slf4j
@Configuration
public class SimpleJobConfig {

    @Bean
    public Job simpleJob(JobRepository jobRepository, Step simpleStep1) {
        return new JobBuilder("simpleJob", jobRepository)
                .start(simpleStep1)
                .build();
    }

    @Bean
    public Step simpleStep1(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilder("simpleStep1", jobRepository)
                .tasklet(tasklet(), transactionManager)
                .build();
    }

    private Tasklet tasklet() {
        return (contribution, chunkContext) -> {
            log.info(">>>>>> This is Step1");
            return RepeatStatus.FINISHED;
        };
    }
}
  • spring boot 2에서는 JobBuilderFactory와 StepBuilderFactory를 이용하여 Job과 Step을 생성했는데 deprecated 되어 코드를 맞게 수정하였다.
  • config 파일에는 반드시 @Configuration을 설정해야한다.

 

이렇게 작성하고 테스트를 위해 main을 실행했는데 로그가 찍히지 않았다. 로그 레벨도 수정해보고 해도 그저 애플리케이션이 실행됐다가 종료되기만 함

 

INFO 28784 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : No TaskExecutor has been set, defaulting to synchronous executor.

 

  • 설정해놓은 step의 tasklet이 실행되지 않고 있다.

 

문제는 @EnableBatchProcessing 이 어노테이션이었다.

 

spring boot3에서 Application class에 저 어노테이션을 작성하면 auto-configutation이 적용되어 실행이 된다.

그래서 해당 어노테이션을 삭제했더니 배치가 실행되면서 log가 잘 찍혔다.

 

INFO 6668 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=simpleJob]] launched with the following parameters: [{}]
INFO 6668 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [simpleStep1]
INFO 6668 --- [ main] com.batch.job.SimpleJobConfig : >>>>>> This is Step1
INFO 6668 --- [ main] o.s.batch.core.step.AbstractStep : Step: [simpleStep1] executed in 4ms
INFO 6668 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=simpleJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 16ms

 

 

 

참고

https://stackoverflow.com/questions/75287102/spring-batch-5-0-with-spring-boot-tasklet-job-not-starting-automatically

https://jojoldu.tistory.com/325?category=902551