Spring/Spring Batch

Spring Batch (2) ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ ๋ฐ ์˜์กด์„ฑ ์„ค์ •, Hello World

์ฃผ๋ฐœ2 2022. 10. 29. 14:44
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ ํ•ด๋‹น ํฌ์ŠคํŒ…์˜ ๋ชฉํ‘œ

  • Spring Batch ํ”„๋กœ์ ํŠธ ์„ค์ •
  • Spring Batch ์„ค์ • ๋‚ด๋ถ€
  • Spring Batch Hello World

 

 

๐Ÿ“Œ Spring Batch ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ

ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ์€ IntelliJ, Java 11, Gradle, MySQL(Docker), H2, Spring Boot, Batch ๋“ฑ์„ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

(ํ”„๋กœ์ ํŠธ ์„ค์ • ๊ด€๋ จ ์„ค๋ช…์€ ์—†์œผ๋‹ˆ GitHub๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.)

 

 

๐Ÿ“Œ Spring Batch ์˜์กด์„ฑ ์„ค์ •

์Šคํ”„๋ง ๋ฐฐ์น˜๋ฅผ ์ž‘๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๋ฉ”์ธ ํด๋ž˜์Šค์— @EnableBatchProcessing ์–ด๋…ธํ…Œ์ด์…˜์„ ์„ ์–ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableBatchProcessing
@SpringBootApplication
public class SpringBatchApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBatchApplication.class, args);
    }

}

@EnableBatchProcessing ์–ด๋…ธํ…Œ์ด์…˜์€ 4๊ฐœ์˜ ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ณ , ์Šคํ”„๋ง ๋ฐฐ์น˜์˜ ์ดˆ๊ธฐํ™” ๋ฐ ์‹คํ–‰ ๊ตฌ์„ฑ์ด ์ด๋ฃจ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

@EnableBatchProcessing์— ์˜ํ•œ ์‹คํ–‰ ๊ตฌ์„ฑ

 

1. @EnableBatchProcessing

์Šคํ”„๋ง ๋ฐฐ์น˜ ์ž‘๋™์„ ์œ„ํ•ด ์„ ์–ธํ•ด์•ผ ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋‚ด๋ถ€์—๋Š” BatchConfigurationSelector ํด๋ž˜์Šค๊ฐ€ Import ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

BatchConfigurationSelectors ํด๋ž˜์Šค

BatchConfigurationSelector ํด๋ž˜์Šค์—๋Š” modular ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์„ค์ •ํ•  ํด๋ž˜์Šค๋ฅผ ์„ ํƒํ•˜๋Š”๋ฐ, modular๋Š” ๋”ฐ๋กœ ์„ค์ •ํ•˜์ง€ ์•Š๊ธฐ์— SimpleBatchConfiguration ํด๋ž˜์Šค๋ฅผ ๊ฐ€์žฅ ๋จผ์ € ์„ค์ •ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

2. AbstractBatchConfiguration ์ถ”์ƒ ํด๋ž˜์Šค & SimpleBatchConfiguration ํด๋ž˜์Šค

SimpleBatchConfiguration ํด๋ž˜์Šค๋Š” AbstractBatchConfiguration ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค.

AbstractBatchConfiguration ์ถ”์ƒ ํด๋ž˜์Šค๋Š” JobRepository, JobBuilderFactory, StepBuilderFactory ๋“ฑ์„ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

JobBuilderFactory, StepBuilderFactory ๊ฐ์ฒด๋Š” Bean ์ดˆ๊ธฐํ™” ์‹œ ์„ค์ • ๋ฉ”์„œ๋“œ์ธ afterPropertiesSet() ๋ฉ”์„œ๋“œ์—์„œ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ฃผ์ž…ํ•ฉ๋‹ˆ๋‹ค.

...

@Override
public void afterPropertiesSet() throws Exception {
    this.jobBuilderFactory = new JobBuilderFactory(jobRepository());
    this.stepBuilderFactory = new StepBuilderFactory(jobRepository(), transactionManager());
}

 

SimpleBatchConfiguration๋Š” ์Šคํ”„๋ง ๋ฐฐ์น˜์˜ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ํ”„๋ก์‹œ ๊ฐ์ฒด(createLazyProxy() ๋ฉ”์„œ๋“œ)๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

(ํ”„๋ก์‹œ ํŒจํ„ด์— ๋Œ€ํ•œ ๊ฐœ๋…์€ ํ•ด๋‹น ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.)

  • JobRepository
  • JobLauncher
  • JobRegistry
  • ...

 

3. BatchConfigurerConfiguration ํด๋ž˜์Šค

์œ„ SimpleBatchConfiguration ๊ณผ์ •์ด ๋๋‚˜๋ฉด BatchConfigurerConfiguration ํด๋ž˜์Šค๊ฐ€ ์ž‘๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

BatchConfigurerConfiguration ํด๋ž˜์Šค์—๋Š” BasicBatchConfigurer, JpaBatchConfigurer ๋‘ ์„ค์ •์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

  • BasicBatchConfigurer: SipmleBatchConfiguration ํด๋ž˜์Šค์—์„œ ์ƒ์„ฑํ•œ ํ”„๋ก์‹œ ๊ฐ์ฒด์˜ ์‹ค์ œ ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์„ค์ • ํด๋ž˜์Šค
  • JpaBatchConfigurer: JPA ๊ด€๋ จ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์„ค์ • ํด๋ž˜์Šค

JpaBatchConfigurer ํด๋ž˜์Šค๋Š” BasicBatchConfigurer ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ๊ธฐ์— ํ•„๋“œ๋‚˜ ๋ฉ”์„œ๋“œ๋“ค์€ ๊ณต์œ ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 

4. BatchAutoConfiguration ํด๋ž˜์Šค

๋งˆ์ง€๋ง‰์œผ๋กœ ์‹ค์ œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ Job ์‹คํ–‰๊ณผ ๊ด€๋ จ๋œ Launcher๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์„ค์ • ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

์œ„ ์‚ฌ์ง„์˜ 76๋ผ์ธ์—์„œ ์ƒ์„ฑ๋˜๋Š” JobLauncherApplicationRunner ํด๋ž˜์Šค๊ฐ€ ์‹ค์ œ Job์„ ์‹คํ–‰ํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

JobLauncherApplicationRunner

JobLauncherApplicationRunner ํด๋ž˜์Šค๋Š” ์œ„์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ ์‹ค์ œ 195 ๋ผ์ธ์˜ execute() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์˜ ApplicationEventPublisher ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์ด๋ฒคํŠธ๋ฅผ ๋ฐœํ–‰ํ•˜์—ฌ Job์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿ“Œ Spring Batch Hello World

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:~/test
    username: sa
    password:
  h2:
    console:
      enabled: true
      path: /h2-console
  batch:
    jdbc:
      initialize-schema: always
package io.spring.springbatch;

import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
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;

@RequiredArgsConstructor
@Configuration
public class HelloJobConfiguration {

    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job helloJob() {
        return this.jobBuilderFactory.get("helloJob")
                .start(helloStep1())
                .next(helloStep2())
                .build();
    }

    @Bean
    public Step helloStep1() {
        return stepBuilderFactory.get("helloStep1")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println(" ============================");
                    System.out.println(" >> Hello Spring Batch");
                    System.out.println(" ============================");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
    public Step helloStep2() {
        return stepBuilderFactory.get("helloStep2")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println(" ============================");
                    System.out.println(" >> Step2 has executed");
                    System.out.println(" ============================");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
}

  • @Configuration : Batch Job ์ •์˜ ๋ฐ ๋นˆ ์„ค์ •
  • JobBuilderFactory : Job์„ ์ƒ์„ฑํ•˜๋Š” ๋นŒ๋” ํŒฉํ† ๋ฆฌ, AbstractBatchConfiguration ์ถ”์ƒ ํด๋ž˜์Šค์—์„œ Bean์œผ๋กœ ๋“ฑ๋ก
  • StepBuilderFactory : Step์„ ์ƒ์„ฑํ•˜๋Š” ๋นŒ๋” ํŒฉํ† ๋ฆฌ, AbstractBatchConfiguration ์ถ”์ƒ ํด๋ž˜์Šค์—์„œ Bean์œผ๋กœ ๋“ฑ๋ก
  • Job -> Step -> Tasklet(Step ๋‚ด์—์„œ ๋‹จ์ผ ํ…Œ์Šคํฌ๋กœ ์ˆ˜ํ–‰๋˜๋Š” ๋กœ์ง) ์ˆœ์œผ๋กœ ๊ตฌ์„ฑ

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EB%B0%B0%EC%B9%98/dashboard

 

 

์ฐธ๊ณ  ๋ฌธ์„œ

 

๋ฐ˜์‘ํ˜•