개발/Spring Batch

Spring Batch 5 버전으로 간단한 Job, Step 실행

Developer Document 2023. 2. 11. 18:33

Spring Batch 5로 버전 업 되면서 많은 것들이 변하였다.

간단하게 하나의 Job 아래 2개의 Step을 구성할 때는 다음과 같았다.

 

변경 전 (Spring Batch 4)

1. 메인 클래스에 @EnableBatchProcessing 설정

2. 원하는 Job 구성 시 BuilderFactory 사용

사진 상에 빨간 줄이 뜨는 이유는 Spring Batch 5가 설치된 프로젝트에서 코드를 작성해서 그렇다.

해당 팩토리 및 메소드가 deprecated 되었고, 삭제하라는 의미로 빨간 줄이 뜨고 있다.

 


이전 버전의 경우 위처럼 실행하면 정상적으로 실행된다.

하지만 Spring Batch 5 버전에서 실행 시 다음과 같은 에러 로그가 노출된다.

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.document.springbatch.configurations.HelloJobConfiguration required a bean of type 'org.springframework.batch.core.configuration.annotation.JobBuilderFactory' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.batch.core.configuration.annotation.JobBuilderFactory' in your configuration.

 

Spring Batch 5로 버전 업되면서 많은 부분이 변경되어서 그렇다.

기존 버전에서 Spring Batch 5로 업그레이드 하기 위해서는 Spring Batch 공식 깃헙 가이드를 참고하면 된다.

https://github.com/spring-projects/spring-batch/wiki/Spring-Batch-5.0-Migration-Guide

 

Spring Batch 5.0 Migration Guide

Spring Batch is a framework for writing offline and batch applications using Spring and Java - spring-projects/spring-batch

github.com

 

이제 위와 같은 코드가 정상 동작하도록 변경해보자.

 

변경 후 (Spring Batch 5)

1. 메인 클래스에 @EnableBatchProcessing 제거

2. Job Configuration 클래스 수정

특이한 점은 Job과 Step을 생성할 때 JobBuilder를 사용하고, JobRepository, PlatformTransactionManager 의존성을 주입받아야 한다는 것이다.

기본적인 빈들은 Spring Batch에서 생성해주니 별도의 빈을 생성하지 않아도 정상 동작한다.

2023-02-11T18:30:35.280+09:00  INFO 31264 --- [           main] c.d.springbatch.SpringBatchApplication   : Starting SpringBatchApplication using Java 17.0.6 with PID 31264 (/Users/document/Desktop/document/education/Spring Batch/code/spring-batch/target/classes started by document in /Users/document/Desktop/document/education/Spring Batch/code/spring-batch)
2023-02-11T18:30:35.282+09:00  INFO 31264 --- [           main] c.d.springbatch.SpringBatchApplication   : No active profile set, falling back to 1 default profile: "default"
2023-02-11T18:30:35.493+09:00  INFO 31264 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-02-11T18:30:35.557+09:00  INFO 31264 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:62e359e5-1898-4165-bb75-f2eebf9afbe3 user=SA
2023-02-11T18:30:35.557+09:00  INFO 31264 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-02-11T18:30:35.646+09:00  INFO 31264 --- [           main] c.d.springbatch.SpringBatchApplication   : Started SpringBatchApplication in 0.511 seconds (process running for 0.716)
2023-02-11T18:30:35.647+09:00  INFO 31264 --- [           main] o.s.b.a.b.JobLauncherApplicationRunner   : Running default command line with: []
2023-02-11T18:30:35.659+09:00  INFO 31264 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=helloJob]] launched with the following parameters: [{}]
2023-02-11T18:30:35.667+09:00  INFO 31264 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [helloStep1]
 =======================
 >> Hello Spring Batch!!
 =======================
2023-02-11T18:30:35.671+09:00  INFO 31264 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [helloStep1] executed in 3ms
2023-02-11T18:30:35.672+09:00  INFO 31264 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [helloStep2]
 =======================
 >> step2 was executed.
 =======================
2023-02-11T18:30:35.674+09:00  INFO 31264 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [helloStep2] executed in 1ms
2023-02-11T18:30:35.677+09:00  INFO 31264 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=helloJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 13ms
2023-02-11T18:30:35.679+09:00  INFO 31264 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2023-02-11T18:30:35.688+09:00  INFO 31264 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

로그를 보면 자동으로 메모리에 H2 데이터베이스를 생성하여 사용하는 것을 알 수 있다.

해당 설정으로 설정한 Job, Step 모두 정상 동작함을 확인할 수 있다.