스프링/스프링 프레임워크
스프링 배치 - Hello World
coffee.
2025. 4. 20. 21:19
- Job
- 배치 처리 과정을 하나의 단위로 만들어 표현한 객체
- 하나의 Job 객체는 여러 Step 인스턴스를 포함하는 컨테이너
- JobRepository
- 배치 처리 정보를 담고 있는 객체
- Job이 실행되었으면 배치 처리에 대한 모든 메타 데이터가 담겨있다.
- JobLauncher
- 배치를 실행시키는 인터페이스
- 클라이언트의 요청을 받아 Job을 실행
- Job,파라미터를 받아 실행하며 JobExecution을 반환한다.
- Step
- Job 내부에 구성되어 실제 배치 작업 수행을 정의하고 제어한다. Job을 처리하는 단위
- chunk 또는 tasklet이 포함되어 있다
Hello World 예제
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.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
public class HelloJobConfig {
@Bean
public Job helloJob(JobRepository jobRepository, Step helloStep1) {
return new JobBuilder("helloJob", jobRepository)
.start(helloStep1)
.build();
}
@Bean
public Step helloStep1(JobRepository jobRepository, Tasklet helloStep1Tasklet1, PlatformTransactionManager platformTransactionManager) {
return new StepBuilder("helloStep1Tasklet1", jobRepository)
.tasklet(helloStep1Tasklet1, platformTransactionManager)
.build();
}
@Bean
public Tasklet helloStep1Tasklet1() {
return ((contribution, chunkContext) -> {
//log.info("Hello World");
System.out.println("Hello World");
return RepeatStatus.FINISHED;
});
}
}