Maven 또는 Gradle 설정 파일에 다음 의존성을 추가합니다.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-batch'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
}
Spring Batch 5 버전 부터는
@EnableBatchProcessing어노테이션이 필수가 아닙니다.
package com.example.springbatchpractice;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
// @EnableBatchProcessing
public class SpringBatchPracticeApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchPracticeApplication.class, args);
}
}
Spring Batch의 기본 배치 작업을 구성합니다. 먼저, src/main/java 경로에 패키지를 생성하고, 배치 작업을 구성하는 Java 클래스를 작성합니다.
예제에선 아래 경로에 파일을 생성하였습니다.
com.example.springbatchpractice.batch.config.SampleJobConfig.java
package com.example.springbatchpractice.batch.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
public class SampleJobConfig {
@Bean("sampleJob")
public Job configureSampleJob(JobRepository jobRepository, Step sampleStep) {
return new JobBuilder("sampleJob", jobRepository)
.start(sampleStep)
.incrementer(new RunIdIncrementer())
.build();
}
}
package com.example.springbatchpractice.batch.step;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
@Slf4j
@Configuration
public class SampleStep {
@Bean
public Step configureSampleStep(JobRepository jobRepository, Tasklet sampleTasklet, PlatformTransactionManager platformTransactionManager) {
return new StepBuilder("sampleStep", jobRepository)
.tasklet(sampleTasklet, platformTransactionManager)
.build();
}
}
package com.example.springbatchpractice.batch.tasklet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class SampleTasklet implements Tasklet {
@Override
public RepeatStatus execute(org.springframework.batch.core.StepContribution contribution, org.springframework.batch.core.scope.context.ChunkContext chunkContext) throws Exception {
log.info("Run SampleTasklet");
log.info("Wait for 5 seconds");
Thread.sleep(5000);
log.info("End SampleTasklet");
return RepeatStatus.FINISHED;
}
}
c.e.s.batch.tasklet.SampleTasklet : Run SampleTasklet
c.e.s.batch.tasklet.SampleTasklet : Wait for 5 seconds
c.e.s.batch.tasklet.SampleTasklet : End SampleTasklet
o.s.batch.core.step.AbstractStep : Step: [sampleStep] executed in 5s16ms