๐ŸคฉSpring Batch ์‹œ์ž‘ํ•˜๊ธฐ

Chaostocosmosยท2022๋…„ 11์›” 22์ผ
1

Spring Batch

๋ชฉ๋ก ๋ณด๊ธฐ
2/3

์Šคํ”„๋ง ๋ฐฐ์น˜ ์‹œ์ž‘

ํ™˜๊ฒฝ ์„ค์ • ๋งˆ์น˜๊ณ  ๋ฐฐ์น˜ ์‹œ์ž‘ํ•œ๋‹ค ใ…œใ…œ

1. ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ ๋ฐ ์˜์กด์„ฑ ์„ค์ •

ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ

Spring Boot Project ์ƒ์„ฑ

Spring Boot 2.7.5 , Maven , Java 11

Library : Spring Boot DevTools , Lombok , Spring Batch ( ์ถ”ํ›„ ์ถ”๊ฐ€ ์˜ˆ์ • )

Spring Batch Library

2. ์Šคํ”„๋ง ๋ฐฐ์น˜ ๋™์ž‘ ๋ฐ ์ดˆ๊ธฐ ์„ค์ •

@EnableBatchProcessing
  • ์Šคํ”„๋ง ๋ฐฐ์น˜๊ฐ€ ์ž‘๋™ํ•˜๊ธฐ ์œ„ํ•ด ์„ ์–ธํ•ด์•ผ ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜ ( ๊ธฐ๋Šฅ๋“ค์„ ํ™œ์„ฑํ™” ํ•˜๋„๋ก ์ดˆ๊ธฐํ™” ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜ )
    • 4 ๊ฐœ์˜ ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์‹คํ–‰ ๋ฐฐ์น˜ ๋ชจ๋“  ์ดˆ๊ธฐํ™” ๋ฐ ๊ตฌ์„ฑ ์‹œ์ž‘
    • ์Šคํ”„๋ง ๋ถ€ํŠธ ๋ฐฐ์น˜ ์ž๋™ ์„ค์ • ํด๋ž˜์Šค ์‹คํ–‰ ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋œ ๋ชจ๋“  Job ๊ฒ€์ƒ‰ ํ›„ ์ดˆ๊ธฐํ™”
์Šคํ”„๋ง ๋ฐฐ์น˜ ์ดˆ๊ธฐํ™” ์„ค์ • Class๊ธฐ๋Šฅ ๋ฐ ์„ค๋ช…
BatchAutoConfiguration1. ์Šคํ”„๋ง ๋ฐฐ์น˜๊ฐ€ ์ดˆ๊ธฐํ™” ๋  ๋•Œ ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์„ค์ • ํด๋ž˜์Šค
2. Job์„ ์ˆ˜ํ–‰ํ•˜๋Š” JobLauncherApplicationRunner Bean์„ ์ƒ์„ฑ
SimpleBatchConfiguration1. JobBuilderFactory ์™€ StepBuilderFactory ์ƒ์„ฑ
2. ์Šคํ”„๋ง ๋ฐฐ์น˜์˜ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ ์ƒ์„ฑ - ํ”„๋ก์‹œ ๊ฐ์ฒด๋กœ ์ƒ์„ฑ๋จ
BatchConfigurerConfiguration- BasicBatchConfigure
1. SimpleBatchConfiguration ์—์„œ ์ƒ์„ฑํ•œ ํ”„๋ก์‹œ ๊ฐ์ฒด์˜ ์‹ค์ œ ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์„ค์ • ํด๋ž˜์Šค
2. ๋นˆ์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž… ๋ฐ›์•„์„œ ์ฃผ์š” ๊ฐ์ฒด๋“ค์„ ์ฐธ์กฐํ•ด์„œ ์‚ฌ์šฉ
- JpaBatchConfigurer
JPA ๊ด€๋ จ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์„ค์ • ํด๋ž˜์Šค
์‚ฌ์šฉ์ž ์ •์˜ BatchConfigurer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Œ !

proxy ๊ฐ์ฒด : ์‹ค์ œ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ, ์‹ค์ œ ๊ฐ์ฒด์˜ ๋Œ€๋ฆฌ ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค

์‹คํ–‰ ์ˆœ์„œ

@EnableBatchProcessing

SimpleBatchConfiguration

BatchConfigurerConfiguration

BasicBatchConfigurer
JPABatchConfigurer

BatchAutoConfiguration

๋ฐฐ์น˜ ์ดˆ๊ธฐํ™” Test

ํ…Œ์ŠคํŠธ ์ „ h2database ( ๋ฉ”๋ชจ๋ฆฌ DB ) ๋ฅผ ์˜์กด์„ฑ์— ์ถ”๊ฐ€ํ•œ๋‹ค.
Spring Batch ์˜ ๊ฒฝ์šฐ ์Šคํ‚ค๋งˆ๊ฐ€ ์ƒ์„ฑ ๋˜์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋–„๋ฌธ์— DB๋ฅผ ์ƒ์„ฑํ•ด ๋‘์–ด์•ผ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ง€ ์•Š์Œ

์ด ํ›„ ์ƒ๋‹จ์— ์ง€์ •๋œ Class๋“ค์— ์ค‘๋‹จ์ ์„ ๊ฑธ์–ด Class์˜ Flow๋ฅผ ํ™•์ธํ•ด๋ณธ๋‹ค !

3. Hello Spring Batch ์‹œ์ž‘ํ•˜๊ธฐ

Sample Code

@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(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                        System.out.println(" ============================");
                        System.out.println(" >> Hello Spring Batch");
                        System.out.println(" ============================");
                        return RepeatStatus.FINISHED;
                    }
                })
                .build();
    }
    
}

Batch Job ์ƒ์„ฑ ์ฃผ์š” Keywords

  1. @Configuration : ํ•˜๋‚˜์˜ ๋ฐฐ์น˜ Job์„ ์ •์˜ํ•˜๊ณ  Bean ์„ค์ •
  2. JobBuilderFactory : Job์„ ์ƒ์„ฑํ•˜๋Š” ๋นŒ๋” ํŒฉํ† ๋ฆฌ ( ํŽธ์˜ ์ œ๊ณต ์œ ํ‹ธ ํด๋ž˜์Šค )
    @Bean
        public Job helloJob() {
            return this.jobBuilderFactory.get("helloJob") //Job ์ƒ์„ฑ
                    .start(helloStep1()) // Step์€ Job์˜ Start์˜ ์ธ์ž๋กœ ์‚ฌ์šฉ๋จ
                    .next(helloStep2()) // ๋‹ค์Œ Step์„ ์ง„ํ–‰ 
                    .build();
        }
  3. StepBuilderFactory : Step์„ ์ƒ์„ฑํ•˜๋Š” ๋นŒ๋” ํŒฉํ† ๋ฆฌ ( ํŽธ์˜ ์ œ๊ณต ์œ ํ‹ธ ํด๋ž˜์Šค )
    @Bean
        public Step helloStep1() {
            return stepBuilderFactory.get("helloStep1") //Step ์ƒ์„ฑ
                    .tasklet(new Tasklet() {
                        @Override
                        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                            System.out.println(" ============================");
                            System.out.println(" >> Hello Spring Batch");
                            System.out.println(" ============================");
                            return RepeatStatus.FINISHED;
                        }
                    })
                    .build();
        }
  1. Job : ์‚ฌ์šฉ์ž ์ •์˜์˜ ์ž‘์—…
  2. Step : ์ž‘์—…์˜ Step
  3. tasklet : step ์•ˆ์—์„œ ๋‹จ์ผ ํƒœ์Šคํฌ๋กœ ์ˆ˜ํ–‰๋˜๋Š” ๋กœ์ง
    return stepBuilderFactory.get("helloStep1") //Step ์ƒ์„ฑ
                    .tasklet(new Tasklet() {
                        @Override
                        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                            System.out.println(" ============================");
                            System.out.println(" >> Hello Spring Batch");
                            System.out.println(" ============================");
                            return RepeatStatus.FINISHED;
                        }
                    })
                    .build();
                 

tasklet์˜ ์ต๋ช… Class RepeatStatus์€ step์˜ ์ž‘์—…์„ ๋ฐ˜๋ณตํ•˜๋„๋ก ๋˜์–ด์žˆ์œผ๋ฉฐ Return์ด null ์ธ ๊ฒฝ์šฐ 1ํšŒ ์‹œ์ž‘ ํ›„ ์ข…๋ฃŒํ•œ๋‹ค.
์ด์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์œผ๋กœ์„œ RepeatStatus.FINISHED ๊ฐ€ ์žˆ๋‹ค.
RepeatStatus execute ์˜ ๋ฉ”์„œ๋“œ ์•ˆ์—๋Š” Step์„ ์‹คํ–‰ ์‹œํ‚ฌ ๋กœ์ง๊ตฌํ˜„๋ถ€๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ•œ๋‹ค.

Job ๊ตฌ๋™ -> Job์˜ Step ์ˆ˜ํ–‰ -> Tasklet ์ˆ˜ํ–‰

์—ฐ์Šตํ•˜๊ธฐ )
์˜์กด์„ฑ ์ฃผ์ž… : ์†Œํ”„ํŠธ์›จ์–ด ์—”์ง€๋‹ˆ์–ด๋ง์—์„œ ์˜์กด์„ฑ ์ฃผ์ž…(dependency injection)์€ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ์˜์กด์„ฑ์„ ์ œ๊ณตํ•˜๋Š” ํ…Œํฌ๋‹‰์ด๋‹ค. "์˜์กด์„ฑ"์€ ์˜ˆ๋ฅผ ๋“ค์–ด ์„œ๋น„์Šค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด์ด๋‹ค. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์–ด๋–ค ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ์ง€์ •ํ•˜๋Š” ๋Œ€์‹ , ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฌด์Šจ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๋งํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. "์ฃผ์ž…"์€ ์˜์กด์„ฑ(์„œ๋น„์Šค)์„ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฐ์ฒด(ํด๋ผ์ด์–ธํŠธ)๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ์„œ๋น„์Šค๋Š” ํด๋ผ์ด์–ธํŠธ ์ƒํƒœ์˜ ์ผ๋ถ€์ด๋‹ค. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋น„์Šค๋ฅผ ๊ตฌ์ถ•ํ•˜๊ฑฐ๋‚˜ ์ฐพ๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜๋Š” ๋Œ€์‹  ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์„œ๋น„์Šค๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ํŒจํ„ด์˜ ๊ธฐ๋ณธ ์š”๊ฑด์ด๋‹ค.

์˜์กด์„ฑ ์ฃผ์ž…์˜ ์˜๋„๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์‚ฌ์šฉ์˜ ๊ด€์‹ฌ์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Š” ๊ฐ€๋…์„ฑ๊ณผ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ ๋†’ํ˜€์ค€๋‹ค.

์˜์กด์„ฑ ์ฃผ์ž…์€ ๊ด‘๋ฒ”์œ„ํ•œ ์—ญ์ œ์–ด ํ…Œํฌ๋‹‰์˜ ํ•œ ํ˜•ํƒœ์ด๋‹ค. ์–ด๋–ค ์„œ๋น„์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๋Š” ํด๋ผ์ด์–ธํŠธ๋Š” ๊ทธ ์„œ๋น„์Šค๊ฐ€ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋˜์—ˆ๋Š”์ง€ ์•Œ์ง€ ๋ชปํ•ด์•ผ ํ•œ๋‹ค. ํด๋ผ์ด์–ธํŠธ๋Š” ๋Œ€์‹  ์„œ๋น„์Šค ์ œ๊ณต์— ๋Œ€ํ•œ ์ฑ…์ž„์„ ์™ธ๋ถ€ ์ฝ”๋“œ(์ฃผ์ž…์ž)๋กœ ์œ„์ž„ํ•œ๋‹ค. ํด๋ผ์ด์–ธํŠธ๋Š” ์ฃผ์ž…์ž ์ฝ”๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ ๋‹ค์Œ, ์ฃผ์ž…์ž๋Š” ์ด๋ฏธ ์กด์žฌํ•˜๊ฑฐ๋‚˜ ์ฃผ์ž…์ž์— ์˜ํ•ด ๊ตฌ์„ฑ๋˜์—ˆ์„ ์„œ๋น„์Šค๋ฅผ ํด๋ผ์ด์–ธํŠธ๋กœ ์ฃผ์ž…(์ „๋‹ฌ)ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ ํด๋ผ์ด์–ธํŠธ๋Š” ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ฃผ์ž…์ž์™€ ์„œ๋น„์Šค ๊ตฌ์„ฑ ๋ฐฉ์‹ ๋˜๋Š” ์‚ฌ์šฉ์ค‘์ธ ์‹ค์ œ ์„œ๋น„์Šค์— ๋Œ€ํ•ด ์•Œ ํ•„์š”๊ฐ€ ์—†์Œ์„ ์˜๋ฏธํ•œ๋‹ค. ํด๋ผ์ด์–ธํŠธ๋Š” ์„œ๋น„์Šค์˜ ์‚ฌ์šฉ ๋ฐฉ์‹์„ ์ •์˜ํ•˜๊ณ  ์žˆ๋Š” ์„œ๋น„์Šค์˜ ๊ณ ์œ ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•ด์„œ๋งŒ ์•Œ๋ฉด ๋œ๋‹ค. ์ด๊ฒƒ์€ "๊ตฌ์„ฑ"์˜ ์ฑ…์ž„์œผ๋กœ๋ถ€ํ„ฐ "์‚ฌ์šฉ"์˜ ์ฑ…์ž„์„ ๊ตฌ๋ถ„ํ•œ๋‹ค.

  • ์œ„ํ‚ค๋ฐฑ๊ณผ

์˜์กด์„ฑ ์ฃผ์ž… ์ฐธ์กฐ ( DI ๊ด€๋ จ POST ) https://mangkyu.tistory.com/150

@RequiredArgsConstructor : ์ƒ์„ฑ์ž๋ฅผ ๋ณ„๋„๋กœ ์ž‘์„ฑํ•˜์—ฌ ๋งŒ๋“ค์ง€ ์•Š๊ณ , ๊ฐ์ฒด ์ธ์ž๋ฅผ ์„ ์–ธํ•˜์—ฌ ์˜์กด์„ฑ ์ฃผ์ž…์ด ๋˜๋„๋ก ํ•˜๋Š” Lombok์˜ Annotation

@RequiredArgsConstructor ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” final ์ด๋ผ๋Š” keyword๋ฅผ ์‚ฌ์šฉ

final ์‚ฌ์šฉ ์ด์œ 

final - ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์ด ํ•„๋“œ์— ์ฃผ์ž…๋˜๋ฉด ๊ณ„์† ๊ทธ๊ฒƒ๋งŒ ์“ฐ์ด๋ฉด ๋˜๊ณ , ๊ตณ์ด ๋‹ค์‹œ ์žฌํ• ๋‹น ๋  ์ด์œ /ํ•„์š”๋„ ์—†๊ณ  ๊ทธ๋Ÿด ์ผ๋„ ์—†์Œ -> ๊ตณ์ด (์‹œ์Šคํ…œ์ ์œผ๋กœ) ์žฌํ• ๋‹น ์ž์ฒด๋Š” ๊ฐ€๋Šฅํ•จ์„ ์—ด์–ด๋‘˜ ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ final ์„ค์ •

Batch Job ์‹คํ–‰ ์„ฑ๊ณต ~

profile
๊ฐœ๋ฐœ์ƒˆ๋ฐœ

0๊ฐœ์˜ ๋Œ“๊ธ€