
순수 Spring Batch 예제와 동일한 시나리오를 사용한다.
Job 이름: simpleHelloJob
Step 구성:
Tasklet 기반 Step 사용
데이터 처리 로직은 포함하지 않음
Spring Initializer를 사용해 프로젝트를 생성한다.
Project: Gradle
Language: Java
Spring Boot: 3.4.x
Java: 17
Dependencies:
Spring Boot 프로젝트 생성 시 spring-boot-starter-batch가 자동으로 포함되며, 이는 Spring Batch 실행에 필요한 대부분의 의존성과 자동 설정을 함께 제공한다.
Spring Boot 기반 프로젝트의 build.gradle은 다음과 같은 형태를 가진다.
plugins {
id 'java'
id 'org.springframework.boot' version '3.4.7'
id 'io.spring.dependency-management' version '1.1.7'
}
group = 'com.example.batch'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-batch'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.batch:spring-batch-test'
}
tasks.named('test') {
useJUnitPlatform()
}
순수 Spring Batch 방식과 비교했을 때 가장 큰 차이는 spring-batch-core를 직접 추가하지 않는다는 점이다. Spring Boot가 Batch 실행에 필요한 의존성과 핵심 Bean 구성을 자동으로 처리한다.
Spring Boot 기반 배치 애플리케이션의 진입점은 일반적인 Spring Boot 애플리케이션과 동일하다.
@SpringBootApplication
public class SimpleBatchApplication {
public static void main(String[] args) {
System.exit(
SpringApplication.exit(
SpringApplication.run(SimpleBatchApplication.class, args)
)
);
}
}
배치 애플리케이션에서는 실행 결과를 exit code로 외부 시스템에 전달하는 것이 중요하므로, System.exit를 함께 사용하는 것이 권장된다.
이제 실제 배치 Job을 구성한다. 이 부분은 순수 Spring Batch 예제와 거의 동일하다.
@Configuration
public class SimpleHelloJobConfig {
private final JobRepository jobRepository;
private final PlatformTransactionManager transactionManager;
public SimpleHelloJobConfig(JobRepository jobRepository,
PlatformTransactionManager transactionManager) {
this.jobRepository = jobRepository;
this.transactionManager = transactionManager;
}
@Bean
public Job simpleHelloJob() {
return new JobBuilder("simpleHelloJob", jobRepository)
.start(startStep())
.next(endStep())
.build();
}
@Bean
public Step startStep() {
return new StepBuilder("startStep", jobRepository)
.tasklet((contribution, chunkContext) -> {
System.out.println("배치 Job이 시작되었습니다.");
return RepeatStatus.FINISHED;
}, transactionManager)
.build();
}
@Bean
public Step endStep() {
return new StepBuilder("endStep", jobRepository)
.tasklet((contribution, chunkContext) -> {
System.out.println("배치 Job이 정상적으로 종료되었습니다.");
return RepeatStatus.FINISHED;
}, transactionManager)
.build();
}
}
순수 Spring Batch 방식과 비교해보면 다음 차이가 있다.
DefaultBatchConfiguration을 상속한 설정 클래스가 없다.Spring Boot 환경에서는 JobLauncherApplicationRunner가 자동으로 배치 실행을 담당한다. 실행할 Job은 프로퍼티로 지정한다.
./gradlew bootRun --args='--spring.batch.job.name=simpleHelloJob'
실행 결과는 다음과 같다.
배치 Job이 시작되었습니다.
배치 Job이 정상적으로 종료되었습니다.
동시에 Spring Batch 메타데이터 테이블이 H2 데이터베이스에 자동으로 생성되고, Job과 Step 실행 이력이 저장된다.
| 구분 | 순수 Spring Batch | Spring Boot 기반 |
|---|---|---|
| 인프라 설정 | 직접 구성 | 자동 구성 |
| DataSource | 수동 정의 | 자동 정의 |
| TransactionManager | 수동 정의 | 자동 정의 |
| JobRepository | DefaultBatchConfiguration | 자동 구성 |
| 실행 진입점 | CommandLineJobRunner | JobLauncherApplicationRunner |
| 배치 로직 | 동일 | 동일 |