๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring/Spring Batch

Spring Batch (11) Repeat

by ์ฃผ๋ฐœ2 2023. 1. 15.
๋ฐ˜์‘ํ˜•

๐Ÿ”—  Spring Batch Repeat

  • Batch Processing๋Š” ๋‹จ์ˆœ ์ตœ์ ํ™” ๋˜๋Š” ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์— ๊ด€ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • ๋ฐ˜๋ณต์„ ์ „๋žตํ™”ํ•˜๊ณ  ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ์œ„ํ•ด Spring Batch์—์„œ๋Š” ๋ฐ˜๋ณต ์ž‘์—…์„ ์ œ์–ดํ•˜๋Š” RepeatOperation ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด๋Š” RepeatTemplate ํด๋ž˜์Šค ์ž…๋‹ˆ๋‹ค

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

 

 

๐Ÿ”—  ๋ฐ˜๋ณต ์ข…๋ฃŒ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์„ธ ๊ฐ€์ง€ ํ•ญ๋ชฉ

Spring Batch์—์„œ์˜ ๋ฐ˜๋ณต ์ž‘์—…์— ๋Œ€ํ•œ ์ข…๋ฃŒ ์ฒ˜๋ฆฌ๋Š” RepeatTemplate ํด๋ž˜์Šค์—์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1. RepeatStatus

  • ์Šคํ”„๋ง ๋ฐฐ์น˜์˜ ์ž‘์—…์ด ์ข…๋ฃŒ๋˜์—ˆ๋Š”์ง€ ํŒ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ Enum

 

2. CompletionPolicy

  • RepeatTemplate ๋‚ด๋ถ€์—์„œ ๋ฐ˜๋ณต ๋ฉ”์„œ๋“œ์˜ ์ข…๋ฃŒ๋Š” CompletionPolicy์— ์˜ํ•ด ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค.
  • ์‹คํ–‰ ํšŸ์ˆ˜ ๋˜๋Š” ์™„๋ฃŒ์‹œ๊ธฐ, ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์ˆ˜ํ–‰ํ•  ์ž‘์—…์— ๋Œ€ํ•œ ๋ฐ˜๋ณต์—ฌ๋ถ€ ๊ฒฐ์ •
  • SimpleCompletionPolicy์—์„œ๋Š” RepeatStatus๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ๋Œ€ ๊ณ ์ • ํšŸ์ˆ˜(ChunkSize)๋งŒํผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • TimeoutTerminationPolicy: ๋ฐ˜๋ณต ์‹œ์ ๋ถ€ํ„ฐ ํ˜„์žฌ ์‹œ์ ๊นŒ์ง€ ์†Œ์š”๋œ ์‹œ๊ฐ„์ด ์„ค์ •๋œ ์‹œ๊ฐ„๋ณด๋‹ค ํฌ๋ฉด ๋ฐ˜๋ณต์ข…๋ฃŒ(๊ธฐ๋ณธ ํƒ€์ž„์•„์›ƒ: 30์ดˆ)
  • SimpleCompletionPolicy: ํ˜„์žฌ ๋ฐ˜๋ณต ํšŸ์ˆ˜๊ฐ€ ChunkSize ๊ฐฏ์ˆ˜๋ณด๋‹ค ํฌ๋ฉด ๋ฐ˜๋ณต์ข…๋ฃŒ(Default Chunk Size: 5)
  • CountingCompletionPolicy: ์ผ์ • ์นด์šดํŠธ๋ฅผ ๊ณ„์‚ฐ ๋ฐ ์ง‘๊ณ„ํ•ด์„œ ์นด์šดํŠธ ์ œํ•œ ์กฐ๊ฑด์ด ๋งŒ์กฑํ•˜๋ฉด ๋ฐ˜๋ณต ์ข…๋ฃŒ

 

3. ExceptionHandler

  • RepeatCallback ๋‚ด๋ถ€์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด RepeatTemplate๊ฐ€ ExceptionHandler๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋˜์งˆ์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ๋น„์ •์ƒ ์ข…๋ฃŒ๋ฅผ ์•Œ๋ฆฌ๋Š”๋ฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

ExceptionHandler์˜ ๊ฒฝ์šฐ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ ์ค‘ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์„ค์ •ํ•ด๋†“์€ ๊ตฌํ˜„์ฒด์— ์ง„์ž…ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ ํ›„ ์˜ˆ์™ธ ์ •๋ณด๋ฅผ ํ† ๋Œ€๋กœ ๊ฐ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์กฐ๊ฑด์ด ์žˆ๋Š”๋ฐ ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋˜์งˆ์ง€ ํ˜น์€ ๋ฌด์‹œํ• ์ง€ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • LogOrRethrowExceptionHandler: ์˜ˆ์™ธ๋ฅผ ๋กœ๊ทธ๋กœ ๊ธฐ๋กํ• ์ง€ ์•„๋‹ˆ๋ฉด ๋‹ค์‹œ ๋˜์งˆ์ง€ ๊ฒฐ์ •
  • RethrowOnThresholdExceptionHandler: ์ง€์ •๋œ ์œ ํ˜•์˜ ์˜ˆ์™ธ๊ฐ€ ์ž„๊ณ„๊ฐ’(Threshold)์— ๋„๋‹ฌํ•˜๋ฉด ๋‹ค์‹œ ๋ฐœ์ƒ
  • SimpleLimitExceptionHandler: ExceptionHandler ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ธฐ๋ณธ ๊ตฌํ˜„์ฒด๋กœ, ์‚ฌ์šฉ ์‚ฌ๋ก€๋Š” ์ง€์ •๋œ ์œ ํ˜•์˜ ์˜ˆ์™ธ ํƒ€์ž… ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฐœ๊ฒฌ๋˜๋ฉด ์นด์šดํ„ฐ๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ณ , ์ œํ•œ์— ๋„๋‹ฌํ•˜๋ฉด ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชฉ์ ์œผ๋กœ Spring Batch์—์„œ๋Š” SimpleLimitExceptionHandler์™€ ๋” ์œ ์—ฐํ•œ RethrowOnThresholdExceptionHandler๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
    handleException() ๋ฉ”์„œ๋“œ๋Š” RethrowOnThresholdExceptionHandler ํด๋ž˜์Šค์— ์œ„์ž„ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 

 

RepeatStatus, CompletionPolicy, ExceptionHandler์˜ ๋ฐ˜๋ณต ์ฒ˜๋ฆฌ

  • RepeatOperation ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด์ธ RepeatTemplate์—์„œ ๋ฐ˜๋ณต์„ ์ข…๋ฃŒํ•  ๋•Œ๊นŒ์ง€ ์‹ค์ œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๊ตฌํ˜„ํ•œ RepeatCallback์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
  • RepeatCallback์—๋Š” doInIteration() ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆ˜ํ–‰๋˜๋ฉฐ, ์ƒํƒœ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” RepeatContext๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๊ณ , ๋ฆฌํ„ด ๊ฐ’์œผ๋กœ๋Š” ๋ฐ˜๋ณต ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” RepeatStatus ์ž…๋‹ˆ๋‹ค.

 

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

Spring Batch์˜ Step ๋‚ด์— ์กด์žฌํ•˜๋Š” RepeatOperations(RepeatTemplate)์„ ํ†ตํ•ด RepeatCallback์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

๊ทธ ํ›„ RepeatCallback์˜ ๋ฉ”์„œ๋“œ์ธ doInIteration() ๋ฉ”์„œ๋“œ์—์„œ Tasklet์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋‚œ ํ›„ ์•ž์„œ ์‚ดํŽด๋ดค์—ˆ๋˜ ๋ฐ˜๋ณต ์ข…๋ฃŒ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ํ•ญ๋ชฉ๋“ค(ExceptionHandler, CompletionPolicy, RepeatStatus)์˜ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿ”—  ์ฝ”๋“œ ์‚ดํŽด๋ณด๊ธฐ

์˜ˆ์ œ ์ฝ”๋“œ๋Š” CompletionPolicy ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด ์ค‘ ํ•˜๋‚˜์ธ SimpleCompletionPolicy ํด๋ž˜์Šค์—์„œ ChunkSize๋ฅผ 3์œผ๋กœ ์„ค์ •ํ•˜์—ฌ 3๋ฒˆ์˜ ์ž‘์—… ํ›„ ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

(์ฝ”๋“œ์—์„œ ์ฃผ์„์œผ๋กœ ์ฒ˜๋ฆฌํ•ด๋†“์€ ๊ณณ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ CompletionPolicy ๊ตฌํ˜„์ฒด๋ฅผ ์„ค์ •ํ•˜๊ฑฐ๋‚˜, ExcceptionHandler๋ฅผ ์„ค์ •ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.)

 

Job

58๋ผ์ธ์—์„œ RepeatTemplate์˜ setCompletionPolicy() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด CompletionPolicy๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

RepeatTemplate

 

 

Job์˜ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•œ ํ›„ RepeatTemplate ํด๋ž˜์Šค์—์„œ ์ž‘์—…์ด ์™„๋ฃŒ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

count์˜ ๊ฐ’์ด ChunkSize์˜ ๊ฐ’์ธ 3๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ ์ข…๋ฃŒ๋˜์ง€ ์•Š๊ณ  ๋ฐฐ์น˜ ์ž‘์—…์ด ๋ฐ˜๋ณต ์ฒ˜๋ฆฌ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 

 

RepeatTemplate

์œ„ ์ฝ”๋“œ๋Š” RepeatTemplate ํด๋ž˜์Šค์˜ executeInternal() ๋ฉ”์„œ๋“œ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค.

215๋ผ์ธ์˜ getNextResult() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ž‘์—…์ด ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๊ณ , count๊ฐ€ 1์”ฉ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

Job

ChunkSize๊ฐ€ 3์ด๋ฏ€๋กœ ์„ธ ๋ฒˆ ๋ฐ˜๋ณต ์‹คํ–‰ ํ›„ ์ข…๋ฃŒํ•˜๊ณ  Job์˜ process() ๋ฉ”์„œ๋“œ๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿ“„  ์ฐธ๊ณ  ๋ฌธ์„œ

 

๋ฐ˜์‘ํ˜•

'Spring > Spring Batch' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Spring Batch (12) Retry  (2) 2023.03.04
Spring Batch (10) @JobScope, @StepScope  (0) 2022.12.08
Spring Batch (9) JobParametersValidator  (0) 2022.11.13
Spring Batch (8) JobLauncher  (0) 2022.11.11
Spring Batch (7) JobRepository  (0) 2022.11.08

๋Œ“๊ธ€