Spring Batch ๊ฐœ๋… ๋ฐ ์ฃผ์š” ๊ตฌ์„ฑ์š”์†Œ, ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ ๊ตฌํ˜„

2025. 2. 18. 17:57ใ†Spring/[2025] Spring Boot

728x90
๐Ÿ“Œ ๋ชฉ์ฐจ 
Spring Batch

- ๊ธฐ๋ณธ ๊ฐœ๋… & ํŠน์ง•, ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ, ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”
Spring Scheduler
- ๊ธฐ๋ณธ ๊ฐœ๋…, ์‚ฌ์šฉ ๋ฐฉ๋ฒ•
๊ฐ„๋‹จํ•œ ์˜ˆ์ œ ๊ตฌํ˜„
- CSV ํŒŒ์ผ ์ฝ์–ด์„œ DB์— ์ €์žฅํ•ด๋ณด๊ธฐ

 

 

Spring Batch

Spring Batch ์Šคํ”„๋ง ๋ฐฐ์น˜ : ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ž‘์—…์„ ์ž๋ฐ” ๊ธฐ๋ฐ˜์˜ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ๊ด„์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜ ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์„ ์ž๋™ํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋จ

 

Spring Batch ํŠน์ง•

  • ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„
    • ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด chunk (์ฒญํฌ) ๊ธฐ๋ฐ˜ ์ฒ˜๋ฆฌ์™€ paging (ํŽ˜์ด์ง•) ๋“ฑ์˜ ๊ธฐ์ˆ  ์ œ๊ณต
    • ๋ถ„์‚ฐ ์ฒ˜๋ฆฌ ๋ฐ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ์ง€์›ํ•ด ํ™•์žฅ์„ฑ๊ณผ ์„ฑ๋Šฅ ๊ทน๋Œ€ํ™”
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํŒŒ์ผ, API ๋“ฑ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ๋ณ€ํ™˜ํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ๊ณผ์ • ์ž๋™ํ™”
  • ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ, ์ž‘์—… ์žฌ์‹œ์ž‘ ๊ธฐ๋Šฅ ๋“ฑ ์ง€์› → ์Šคํ”„๋ง ๋ฐฐ์น˜๋Š” ๋ฐฐ์น˜ ์ž‘์—…์„ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ์™€ ํ•จ๊ป˜ ๊ด€๋ฆฌํ•˜๊ณ , ์‹คํŒจํ•œ ์ž‘์—…์„ ๋ณต๊ตฌํ•˜๊ณ  ์žฌ์‹œ์ž‘ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•จ
Spring Batch ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์€ ์˜ˆ์‹œ
- ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
- ์ •๊ธฐ์ ์ธ ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ (ex. ๋งค์ผ ์ƒˆ๋ฒฝ 1์‹œ์— ์ •์‚ฐ ์ž‘์—… ์‹คํ–‰)
- ETL(Extract, Transform, Load) ์ž‘์—…
- ์ž๋™ํ™”๋œ ๋ณด๊ณ ์„œ ์ƒ์„ฑ
- ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ๋Œ€๋Ÿ‰๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
- ๋ฐ์ดํ„ฐ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜
→ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ค„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, Spring Batch๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์Œ !

 

 

Spring Batch ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ

Job → Step → (Reader → Processor → Writer) ๊ตฌ์กฐ๋กœ ๋™์ž‘

 

Job ํ•˜๋‚˜ ์ด์ƒ์˜ Step ์„ ํฌํ•จํ•˜๋Š” ๋ฐฐ์น˜ ์ž‘์—…
Step Job์„ ๊ตฌ์„ฑํ•˜๋Š” ํ•˜๋‚˜์˜ ์‹คํ–‰ ๋‹จ์œ„
ItemReader ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๋Š” ์—ญํ•  (DB, CSV, API ๋“ฑ)
ItemProcessor ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•˜๋Š” ์—ญํ•  (๋ณ€ํ™˜, ํ•„ํ„ฐ๋ง ๋“ฑ)
ItemWriter ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์—ญํ•  (DB, ํŒŒ์ผ ๋“ฑ)
JobRepository Job ์‹คํ–‰ ์ด๋ ฅ์„ ์ €์žฅํ•˜๋Š” ์ €์žฅ์†Œ
JobLauncher Job์„ ์‹คํ–‰ํ•˜๋Š” ์—”์ง„

 

Job

Spring Batch์—์„œ ๊ฐ€์žฅ ํฐ ์‹คํ–‰ ๋‹จ์œ„, ๋ฐฐ์น˜ ์ž‘์—…์˜ ์ „์ฒด ํ๋ฆ„์„ ์ •์˜ํ•จ

  • ํ•˜๋‚˜ ์ด์ƒ์˜ Step ์œผ๋กœ ์ด๋ฃจ์–ด์ง€๊ณ , ๊ฐ Step ์€ ์ˆœ์ฐจ์ /๋ณ‘๋ ฌ์ ์œผ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Œ
  • Job ์€ ์žฌ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๊ณ , ์‹คํŒจํ•œ ์ž‘์—…์„ ์žฌ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ํ•จ
  • JobBuilder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑํ•˜๊ณ , JobLancher๋ฅผ ํ†ตํ•ด ์‹คํ–‰๋จ

JobInstance

ํŠนJob์ด ์‹คํ–‰๋  ๋•Œ ์ƒ์„ฑ๋˜๋Š” ๊ณ ์œ ํ•œ ์‹คํ–‰ ๋‹จ์œ„ Job ์ด ์‹คํ–‰๋  ๋•Œ ๋งˆ๋‹ค ์ƒ์„ฑ๋จ (ํ•˜๋‚˜์˜ Job์ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ JobInstance๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ)

  • JobInstance๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ Job ์˜ ์ด๋ฆ„์ด๋ž‘ JobParameter ์˜ ํ•ด์‹œ๊ฐ’(JobKey)์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ ์œ ํ•˜๊ฒŒ ์‹๋ณ„๋จ → ๋™์ผํ•œ JobParameter ๋กœ ์‹คํ–‰ํ•˜๋ฉด ๊ธฐ์กด์˜ JobInstance ์žฌ์‚ฌ์šฉํ•˜๊ณ , ์ƒˆ๋กœ์šด JobParamter ๋กœ ์‹คํ–‰ํ•˜๋ฉด ์ƒˆ๋กœ์šด JobInstance๊ฐ€ ์ƒ์„ฑ๋จ
  • Spring Batch๋Š” JobInstance ์ •๋ณด๋ฅผ BATCH_JOB_INSTANCE ํ…Œ์ด๋ธ”์— ์ €์žฅํ•˜์—ฌ ๊ด€๋ฆฌํ•จ → ๋™์ผํ•œ Job ์ด๋ฆ„์ด๋ž‘ ๋™์ผํ•œ JobParamter์˜ ํ•ด์‹œ๊ฐ’์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋Š” ์ค‘๋ณต ์ €์žฅ๋˜์ง€ ์•Š์Œ

JobExecution

Spring Batch์—์„œ JobInstance ์˜ ์‹คํ–‰ ๋‹จ์œ„, Job ์ด ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ์ƒ์„ฑ๋จ

  • ์‹คํ–‰ ์ƒํƒœ, ์‹œ์ž‘ ๋ฐ ์ข…๋ฃŒ ์‹œ๊ฐ„, ์‹คํŒจ ์›์ธ ๋“ฑ ์‹คํ–‰ ์ค‘ ๋ฐœ์ƒํ•œ ์ •๋ณด๋“ค์„ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•จ
    • ์‹คํ–‰ ์ƒํƒœ (BatchStatus) :  STARTING, STARTED, COMPLETED, FAILED, STOPPED
    • ์ข…๋ฃŒ ์ƒํƒœ (ExitStatus) : ์„ฑ๊ณต ๋˜๋Š” ์‹คํŒจ ์ด์œ , ๊ธฐ๋ณธ ๊ฐ’ UNKNOWN
  • JobInstance๊ฐ€ ์‹คํ–‰ ๋  ๋•Œ ๋งˆ๋‹ค ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— JobInstance๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ JobExecution ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ

 

JobParamter

Spring Batch์—์„œ ๋ฐฐ์น˜ ์ž‘์—… ์‹คํ–‰ ์‹œ ์™ธ๋ถ€ ๋˜๋Š” ๋‚ด๋ถ€์—์„œ ์ „๋‹ฌ๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ํŠน์ • Job ์‹คํ–‰์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ์‹คํ–‰ ์กฐ๊ฑด์„ ๋™์ ์œผ๋กœ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

  • JobInstance ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ตฌ๋ณ„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ
  • Key-Value ํ˜•ํƒœ
  • ์ง€์›๋˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž… : String, Long, Double, Date

 

Step

Job ์„ ๊ตฌ์„ฑํ•˜๋Š” ํ•˜๋‚˜์˜ ์‹คํ–‰ ๋‹จ์œ„, ํŠน์ • ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ์ง์„ ์บก์Šํ™” ํ•จ

 

๊ตฌ์„ฑ ์š”์†Œ

  • Tasklet ๊ธฐ๋ฐ˜ : ๋‹จ์ผ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ์‹
  • Chunk ๊ธฐ๋ฐ˜ : ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹ → ItemReader, ItemProcessor, ItemWriter ๋กœ ๊ตฌ์„ฑ๋จ

 

StepExecution

Spring Batch์—์„œ Step ์˜ ์‹คํ–‰ ๋‹จ์œ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด ํŠน์ • Step ์‹คํ–‰ ์‹œ ์ƒ์„ฑ๋˜์–ด ์‹คํ–‰ ์ƒํƒœ์™€ ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•จ

  • Step์˜ ์‹œ์ž‘ ์‹œ๊ฐ„, ์ข…๋ฃŒ ์‹œ๊ฐ„, ์ฒ˜๋ฆฌ๋œ ๋ฐ์ดํ„ฐ ์ˆ˜, ์‹คํŒจ ์—ฌ๋ถ€ ๋“ฑ ์‹คํ–‰ ์ค‘ ๋ฐœ์ƒํ•œ ๋ชจ๋“  ์ •๋ณด๋ฅผ ์ €์žฅํ•จ
  • StepExecution๋„ ํŠน์ • Step์˜ ํ•œ ๋ฒˆ์˜ ์‹คํ–‰์„ ๋‚˜ํƒ€๋ƒ„, Step ์ด ์‹คํ–‰๋  ๋•Œ ๋งˆ๋‹ค ์ƒˆ๋กœ์šด StepExecution ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋จ
  • StepExecution ๋ฐ์ดํ„ฐ๋Š” BATCH_STEP_EXECUTION ํ…Œ์ด๋ธ”์— ์ €์žฅํ•˜์—ฌ ์‹คํ–‰ ์ด๋ ฅ์„ ๊ด€๋ฆฌํ•จ
  • Job์ด ์‹คํ–‰๋˜๋ฉด JobInstacne, JobExecution ์ƒ์„ฑ, ๊ทธ ์•ˆ์—์„œ Step์ด ์‹คํ–‰๋  ๋•Œ ๋งˆ๋‹ค, ์ƒˆ๋กœ์šด StepExcecution ๊ฐ์ฒด ์ƒ์„ฑ

 

ItemReader

๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๋Š” ์—ญํ•  (DB, CSV ๋“ฑ์˜ ํŒŒ์ผ ..)

 

 

ItemProcessor

Reader์—์„œ ์ฝ์–ด์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•˜๊ฑฐ๋‚˜ ๋ณ€ํ™˜ํ•˜๋Š” ์—ญํ• 

  • ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ํ•„ํ„ฐ๋งํ•˜๊ฑฐ๋‚˜ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Œ
  • ํ•„์ˆ˜๋Š” ์•„๋‹ˆ๊ณ , ํ•„์š”์— ๋”ฐ๋ผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋จ

 

ItemWriter

Reader ๋˜๋Š” Processor ๋ฅผ ํ†ตํ•ด ์–ป์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•˜๋Š” ์—ญํ• 

  • ๋ฐ์ดํ„ฐ๋ฅผ ์ผ๊ด„์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์—ฌ ์„ฑ๋Šฅ ์ตœ์ ํ™”

 

JobRepository

Job๊ณผ Step ์˜ ์‹คํ–‰ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์—ญํ• 

  • ๋ฐฐ์น˜ ์ž‘์—…์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ (ex. ์‹คํ–‰ ์ƒํƒœ, ์‹œ์ž‘/์ข…๋ฃŒ ์‹œ๊ฐ„, ์‹คํŒจ ์—ฌ๋ถ€ ๋“ฑ)๋ฅผ ์ €์žฅํ•จ
  • ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ์™€ ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์žฅ, ์‹คํŒจ ์‹œ ๋ณต๊ตฌ ์ง€์›

 

JobLauncher

Job ์„ ์‹คํ–‰ํ•˜๋Š” ์—ญํ• 

  • ๋™๊ธฐ/๋น„๋™๊ธฐ ๋ฐฉ์‹์œผ๋กœ Job ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Œ
  • JobRepository์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜์—ฌ JobInstance ๋ฐ JobExcution ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•จ

 

๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”

Spring Batch ๋Š” ๋ฐฐ์น˜ ์ž‘์—…์˜ ์‹คํ–‰ ์ƒํƒœ์™€ ์ด๋ ฅ์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉ Spring Batch๊ฐ€ ์ œ๊ณตํ•˜๋Š” SQL ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋จ

BATCH_JOB_INSTANCE JobInstance(๋ฐฐ์น˜ ์ž‘์—…์˜ ๋…ผ๋ฆฌ์  ๋‹จ์œ„)์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ.
BATCH_JOB_EXECUTION JobExecution(ํŠน์ • JobInstance์˜ ์‹คํ–‰)์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ.
BATCH_JOB_EXECUTION_PARAMS Job ์‹คํ–‰ ์‹œ ์ „๋‹ฌ๋œ JobParameter์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ.
BATCH_JOB_EXECUTION_CONTEXT JobExecution ์ค‘ ์ƒ์„ฑ๋œ ExecutionContext(๊ณต์œ  ๋ฐ์ดํ„ฐ)์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ.
BATCH_STEP_EXECUTION StepExecution(๊ฐ Step์˜ ์‹คํ–‰)์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ.
BATCH_STEP_EXECUTION_CONTEXT StepExecution ์ค‘ ์ƒ์„ฑ๋œ ExecutionContext์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ.

 

BATCH_JOB_INSTANCE

: JobInstance์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ

  • JOB_INSTANCE_ID : JobInstance๋ฅผ ๊ณ ์œ ํ•˜๊ฒŒ ์‹๋ณ„ํ•˜๋Š” ID
  • JOB_NAME : Job์˜ ์ด๋ฆ„ (์‹คํ–‰๋œ Job์˜ ์ด๋ฆ„)
  • JOB_KEY : JobParameter ๋ฅผ ์ง๋ ฌํ™”ํ•˜์—ฌ ์ƒ์„ฑ๋œ ๊ณ ์œ  ํ‚ค

BATCH_JOB_EXECUTION

: JobInstance์˜ ์‹คํ–‰(JobExecution) ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ

  • Job ์ด ์‹คํ–‰๋  ๋•Œ ๋งˆ๋‹ค ์ƒˆ๋กœ์šด JobExecution์ด ์ƒ์„ฑ๋จ
  • ์‹คํ–‰ ์ƒํƒœ, ์‹œ์ž‘/์ข…๋ฃŒ ์‹œ๊ฐ„, ์ข…๋ฃŒ ์ฝ”๋“œ ๋“ฑ์„ ๊ธฐ๋กํ•จ
  • JOB_EXECUTION_ID : JobExecution์„ ๊ณ ์œ ํ•˜๊ฒŒ ์‹๋ณ„ํ•˜๋Š” ID
  • JOB_INSTANCE_ID : ์—ฐ๊ฒฐ๋œ JobInstance์˜ ID
  • STATUS : ์‹คํ–‰ ์ƒํƒœ (STARTED, COMPLETED, FAILED)
  • EXIT_CODE : ์ข…๋ฃŒ ์ฝ”๋“œ (์„ฑ๊ณต/์‹คํŒจ ์—ฌ๋ถ€)
  • EXIT_MESSAGE : EXIT_CODE๊ฐ€ ‘FAILED’ ์ผ ๋•Œ, ์—๋Ÿฌ ๋ฉ”์‹œ์ง€
  • START_TIME, END_TIME : ์‹คํ–‰ ์‹œ์ž‘ ๋ฐ ์ข…๋ฃŒ ์‹œ๊ฐ„

BATCH_JOB_EXECUTION_PARAMS

: ํŠน์ • JobExecution ์— ์ „๋‹ฌ๋œ JobParamter

  • ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ด๋ฆ„, ๊ฐ’, ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ธฐ๋กํ•จ

 

BATCH_JOB_EXECUTION_CONTEXT

: JobExecution ์ค‘ ๊ณต์œ  ๋ฐ์ดํ„ฐ(ExecutionContext) ๋ฅผ ์ €์žฅ

  • ์ง๋ ฌํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ , ์ž‘์—… ์ค‘ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•˜๊ฑฐ๋‚˜ ๋ณต๊ตฌํ•  ๋•Œ ์‚ฌ์šฉํ•จ

BATCH_STEP_EXECUTION

: StepExecution, ๊ฐ Step์˜ ์‹คํ–‰์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ

  • ๊ฐ Step์ด ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด StepExecution ์ด ์ƒ์„ฑ๋จ
  • ์ฝ๊ธฐ/์“ฐ๊ธฐ/์ปค๋ฐ‹/์Šคํ‚ต๋œ ๋ฐ์ดํ„ฐ ์ˆ˜ ๋“ฑ์˜ ์„ธ๋ถ€ ํ†ต๊ณ„ ๊ธฐ๋ก
  • STEP_EXECUTION_ID : StepExecution ์„ ๊ณ ์œ ํ•˜๊ฒŒ ์‹๋ณ„ํ•˜๋Š” ID
  • STEP_NAME : Step ์ด๋ฆ„
  • JOB_EXECUTION_ID : ์—ฐ๊ฒฐ๋œ JobExecution ์˜ ID
  • STATUS : ์‹คํ–‰ ์ƒํƒœ (STARTED, COMPLETED, FAILED)
  • READ_COUNT, WRITE_COUNT, COMMIT_COUNT, SKIP_COUNT : ์ฒ˜๋ฆฌ๋œ ๋ฐ์ดํ„ฐ ์ˆ˜์™€ ํŠธ๋žœ์žญ์…˜ ํ†ต๊ณ„

BATCH_STEP_EXECUTION_CONTEXT

: StepExecution ์ค‘ ๊ณต์œ  ๋ฐ์ดํ„ฐ(StepExecutionContext) ์ €์žฅ

  • BATCH_JOB_EXECUTION_CONTEXT์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, Step ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ

 

ํ…Œ์ด๋ธ” ์ž๋™ ์ƒ์„ฑ

Spring Batch ์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Œ

* ์ง์ ‘ ํ…Œ์ด๋ธ” ์ •์˜ํ•˜๋ ค๋ฉด Spring Batch์—์„œ ์ œ๊ณตํ•˜๋Š” SQL ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ์ˆ˜๋™์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์Œ

 

โž• Spring Batch์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฐฉ์‹

: Tasklet, Chunk

 

Tasklet

  • ํ•˜๋‚˜์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ์‹, ๋‹จ์ˆœํžˆ ํŠน์ • ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ณ  ์ข…๋ฃŒํ•จ
  • ๋Œ€์ฒด๋กœ ๋‹จ์ˆœํ•˜๊ฑฐ๋‚˜ ๋ณต์žกํ•˜์ง€ ์•Š์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ ์ ํ•ฉํ•˜๋ฉฐ, ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋‚˜ ๋‹จ์ผ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์ฃผ๋กœ ์‚ฌ์šฉ๋จ
  • Tasklet ์ธํ„ฐํŽ˜์ด์Šค์˜ execute() ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ , ์ด ๋ฉ”์„œ๋“œ๋Š” ํ•˜๋‚˜์˜ ํŠธ๋žœ์žญ์…˜ ๋ฒ”์œ„์—์„œ ์‹คํ–‰๋จ
  • ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ RepeatStatus.FINISHED ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด Step ์ข…๋ฃŒ๋จ
  • ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ RepeatStatus.CONTINUABLE ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด Tasklet์ด ๊ณ„์† ์‹คํ–‰๋˜์–ด์•ผ ํ•จ
  • ex) ํŒŒ์ผ ์‚ญ์ œ, ๋ฐ์ดํ„ฐ ์ดˆ๊ธฐํ™”, ๋กœ๊ทธ ๊ธฐ๋ก ๋“ฑ ๋ฐ˜๋ณต๋˜์ง€ ์•Š๋Š” ์ž‘์—…

Chunk

  • ๋ฐ์ดํ„ฐ๋ฅผ ์ผ์ • ํฌ๊ธฐ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹, ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
  • ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ , ๊ฐ€๊ณตํ•˜๋ฉฐ, ์ €์žฅํ•˜๋Š” ์ž‘์—…์„ ์ฒญํฌ ๋‹จ์œ„๋กœ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•จ
  • ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ์— ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์ฒญํฌ ํฌ๊ธฐ๋งŒํผ ๋‚˜๋ˆ„์–ด ํŠธ๋žœ์žญ์…˜ ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌํ•จ
    • ํ•˜๋‚˜์˜ ์ฒญํฌ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋ฉด ์ปค๋ฐ‹๋˜๊ณ , ์‹คํŒจํ•˜๋ฉด ํ•ด๋‹น ์ฒญํฌ๋งŒ ๋กค๋ฐฑ๋จ
์ฒ˜๋ฆฌ ๋ฐฉ์‹ ๋‹จ์ผ ์ž‘์—…์„ ์ˆ˜ํ–‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒญํฌ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„์–ด ์ฒ˜๋ฆฌ
ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ํŠธ๋žœ์žญ์…˜ ํ•„์š” ์‹œ ์ˆ˜๋™ ์„ค์ • ์ฒญํฌ ๋‹จ์œ„๋กœ ์ž๋™ ๊ด€๋ฆฌ
๊ตฌ์„ฑ ์š”์†Œ Tasklet ์ธํ„ฐํŽ˜์ด์Šค Reader → Processor → Writer
์ ํ•ฉํ•œ ์ž‘์—… ์œ ํ˜• ๊ฐ„๋‹จํ•œ ๋ฐ˜๋ณต ์—†๋Š” ์ž‘์—… (์˜ˆ: ๋กœ๊ทธ ๊ธฐ๋ก) ๋Œ€๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ (์˜ˆ: ETL ํ”„๋กœ์„ธ์Šค)
์‹คํŒจ ์‹œ ๋กค๋ฐฑ ๋ฒ”์œ„ ์ „์ฒด ์ž‘์—… ์‹คํŒจํ•œ ์ฒญํฌ๋งŒ ๋กค๋ฐฑ
๋ณต์žก๋„ ๋‚ฎ์Œ ์ƒ๋Œ€์ ์œผ๋กœ ๋†’์Œ
์‚ฌ์šฉ ์‚ฌ๋ก€ ํŒŒ์ผ ์‚ญ์ œ, ์ดˆ๊ธฐํ™” ์ž‘์—… ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋Œ€๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ ๋ฐ ์ €์žฅ

 

 

Spring Scheduler

Spring Scheduler ์Šคํ”„๋ง ์Šค์ผ€์ฅด๋Ÿฌ : Spring Framework์—์„œ ์ œ๊ณตํ•˜๋Š” ์ž‘์—… ์Šค์ผ€์ค„๋ง ๊ธฐ๋Šฅ, ํŠน์ • ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์ด๋‚˜ ํŠน์ • ์‹œ์ ์— ์ž‘์—…์„ ์ž๋™์œผ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•จ

 

Spring Scheduler ํŠน์ง•

  • ํŠน์ • ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ/ํŠน์ • ์‹œ์ ์— ์ž‘์—…์„ ์‹คํ–‰ํ•จ
  • ๋ณ„๋„์˜ ์Šค๋ ˆ๋“œ์—์„œ ๋น„๋™๊ธฐ ์‹คํ–‰
    • ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ๋‹จ์ผ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰์ง€๋งŒ, ์Šค๋ ˆ๋“œ ํ’€์„ ์„ค์ •ํ•˜์—ฌ ๋ณ‘๋ ฌ ์‹คํ–‰๋„ ๊ฐ€๋Šฅํ•จ
  • @EnableScheduling, @Scheduled ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์ž‘์—… ๊ตฌํ˜„ ๊ฐ€๋Šฅ

 

์„ค์ • ๋ฐฉ๋ฒ•

0๏ธโƒฃ Spring Scheduler ๋Š” Spring Framework์— ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌํ•จ๋œ ๊ธฐ๋Šฅ์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜์กด์„ฑ ์ถ”๊ฐ€ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

 

1๏ธโƒฃ ์Šค์ผ€์ค„๋Ÿฌ ํ™œ์„ฑํ™”

@EnableScheduling // SpringBoot Schedule ์‚ฌ์šฉ
@SpringBootApplication
public class BatchApplication {

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

}

@EnableScheduling ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์Šค์ผ€์ค„๋ง ๊ธฐ๋Šฅ ํ™œ์„ฑํ™”ํ•œ๋‹ค.

 

2๏ธโƒฃ ์Šค์ผ€์ค„๋ง ์ž‘์—… ์ •์˜

@Slf4j
@Component
public class TestScheduler
{

	@Scheduled(fixedRate = 5000) // 5์ดˆ ๋งˆ๋‹ค ์ž‘์—… ์‹คํ–‰ 
	public void fixedRateTask()
	{
		log.info("Fixed Rate Task Executed !!");
	}

	@Scheduled(fixedDelay = 5000) // ์ด์ „ ์ž‘์—… ์ข…๋ฃŒ ํ›„ 5์ดˆ ๋’ค ์‹คํ–‰ 
	public void fixedDelayTask()
	{
		log.info("Fixed Delay Task Executed !! ");
	}

	@Scheduled(cron = "0 0 12 * * ?") // ๋งค์ผ ์ •์˜ค 12์‹œ์— ์‹คํ–‰ 
	public void cronTask()
	{
		log.info("Cron Task Executed !! ");
	}
}
  • fixedRateTask : ์ด์ „ ์ž‘์—…์˜ ์‹œ์ž‘ ์‹œ์ ๋ถ€ํ„ฐ ์ง€์ •๋œ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ(ms) ๋งˆ๋‹ค ์ž‘์—…์„ ์‹คํ–‰
  • fixedDelayTask : ์ด์ „ ์ž‘์—…์ด ์ข…๋ฃŒ๋œ ์‹œ์ ๋ถ€ํ„ฐ ์ง€์ •๋œ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ(ms) ๋งˆ๋‹ค ์ž‘์—…์„ ์‹คํ–‰
  • cron : Cron ํ‘œํ˜„์‹ ์‚ฌ์šฉํ•ด์„œ ์Šค์ผ€์ค„๋ง
    • “0 0 12 * * ?”→ ๋งค์ผ ์ •์˜ค์— ์‹คํ–‰
    • “0 0/15 * * * ?”→ ๋งค 15๋ถ„ ๋งˆ๋‹ค ์‹คํ–‰
    • “0 0 9-17 * * MON-FRI”→ ์›” ~ ๊ธˆ ์˜ค์ „ 9์‹œ๋ถ€ํ„ฐ 17์‹œ๊นŒ์ง€ ๋งค์‹œ๊ฐ„ ์ •๊ฐ์— ์‹คํ–‰

โ— @Scheduled ์–ด๋…ธํ…Œ์ด์…˜์€ ๋ฐ˜ํ™˜ ํƒ€์ž…์„ ๋ฐ˜๋“œ์‹œ void์—ฌ์•ผ ํ•˜๊ณ , ๋งค๊ฐœ๋ณ€์ˆ˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ

 

๊ฐ„๋‹จํ•œ ์˜ˆ์ œ ๊ตฌํ˜„

Spring Batch + Spring Scheduler๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ ๊ตฌํ˜„ํ•˜๊ธฐ

  • ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ , ๊ฐ„๋‹จํ•œ ๋ณ€ํ™˜(๊ฐ€๊ณต) ์ž‘์—… ํ›„ DB์— ์ €์žฅํ•ด๋ณด๊ธฐ !

1๏ธโƒฃ ์˜์กด์„ฑ ์ถ”๊ฐ€

build.gradle

	implementation 'org.springframework.boot:spring-boot-starter-batch'
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	runtimeOnly 'com.h2database:h2'

2๏ธโƒฃ CSV ๋ฐ์ดํ„ฐ

batch/src/main/resources/people.csv

name,age,phoneNo
Hyejin,28,010-1234-5678
JungHa,28,010-0000-0000
Name1,29,010-1111-1111
Name2,29,010-2222-2222
Name3,30,010-3333-3333
Name4,30,010-4444-4444

 

 

3๏ธโƒฃ application.yml ์„ค์ •

spring:
  datasource:
    url: jdbc:h2:mem:testdb
    driver-class-name: org.h2.Driver
    username: sa
    password:
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: create
    show-sql: true
  batch:
    jdbc:
      initialize-schema: always # DB ์Šคํ‚ค๋งˆ ์ดˆ๊ธฐํ™”.
    job:
      enabled: false # ์‹œ์ž‘๊ณผ ๋™์‹œ์— ์‹คํ–‰ ๋ฐฉ์ง€.
  h2:
    console:
      enabled: true
      path: /h2-console

 

 

4๏ธโƒฃ์Šคํ”„๋ง ๋ฐฐ์น˜ ์„ค์ •

BatchConfig

: Job, Step ์„ค์ •

@Configuration
//@EnableBatchProcessing // Spring Batch 5.x ๋Š” ์„ค์ •ํ•˜์ง€ ๋ง๊ธฐ 
@RequiredArgsConstructor
public class BatchConfig
{

	private final JobRepository jobRepository;
	private final PlatformTransactionManager transactionManager; // ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ๋‹ด๋‹น 
	private final PersonRepository personRepository;

	/**
	 * csv ํŒŒ์ผ ๋ฐ์ดํ„ฐ ์ฝ์–ด์˜ค๋Š” Reader
	 * @return
	 */
	@Bean
	public FlatFileItemReader<Person> reader()
	{
		return new FlatFileItemReaderBuilder<Person>()
				.name("personItemReader")
				.resource(new ClassPathResource("people.csv"))
				.delimited() // CSV ํŒŒ์ผ์˜ ๊ตฌ๋ถ„์ž๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ์ดํ„ฐ ์ฝ๋„๋ก ์„ค์ • 
				.names("name", "age", "phoneNo") // CSV ํŒŒ์ผ์˜ ๊ฐ ์—ด ์ด๋ฆ„ ์ง€์ • 
				.linesToSkip(1) // ์ฒซ๋ฒˆ์งธ ์ค„ ํ—ค๋” ๋ฌด์‹œ 
				.fieldSetMapper(new BeanWrapperFieldSetMapper<>(){ 
					{
						setTargetType(Person.class); // CSV ํŒŒ์ผ์—์„œ ์ฝ์€ ๋ฐ์ดํ„ฐ๋ฅผ Person ๊ฐ์ฒด๋กœ ๋งคํ•‘ 
					}
				})
//				.fieldSetMapper(fieldSet -> new Person(fieldSet.readString("name"), fieldSet.readInt("age"), fieldSet.readString("phoneNo")))
				.build();
	}

	/**
	 * Person phoneNo ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ processor
	 * @return
	 */
	@Bean
	public ItemProcessor<Person, Person> processor()
	{
		return person -> {
			person.setPhoneNo(person.getPhoneNo().replaceAll("-", ""));
			return person;
		};
	}

	/**
	 * ๋ณ€ํ™˜ํ•œ ๋ฐ์ดํ„ฐ DB์— ์ €์žฅ
	 * @return
	 */
	@Bean
	public ItemWriter<Person> writer()
	{
		return personRepository::saveAll;
	}

	/**
	 * Step ์„ค์ • : Reader -> Processor -> Writer
	 * @return
	 */
	@Bean
	public Step step()
	{
		return new StepBuilder("step1", jobRepository)
				.<Person, Person>chunk(10, transactionManager) // ํ•œ ๋ฒˆ์— 10๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์ฝ๊ณ , ์ฒ˜๋ฆฌํ•œ ๋’ค ์ปค๋ฐ‹ 
				.reader(reader())
				.processor(processor())
				.writer(writer())
				.build();
	}

	/**
	 * Job ์„ค์ • : Step ์‹คํ–‰ ์„ค์ •
	 * @return
	 */
	@Bean
	public Job importPersonJob()
	{
		return new JobBuilder("importPersonJob", jobRepository)
				.start(step())
				.build();
	}
}
  • @EnableBatchProcessing : Spring Boot 3.0 ์ด์ƒ๋ถ€ํ„ฐ๋Š” ์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ ! → ์‚ฌ์šฉํ•˜๋ฉด Spring Batch ๊ธฐ๋ณธ ์„ค์ •์ด ๋ฐฑ์˜คํ”„ ๋จ
    • @EnableBatchProcessing ์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ์ฒ˜์Œ์—๋Š” ๋ถ™์—ฌ์„œ ํ…Œ์ŠคํŠธ ํ–ˆ๋”๋‹ˆ, ์ด ์„ค์ •์ด ์ ์šฉ๋˜์ง€ ์•Š์•„์„œ ๋ฐฐ์น˜ ๊ด€๋ จ ํ…Œ์ด๋ธ”๋“ค์ด ์ž๋™ ์ƒ์„ฑ๋˜์ง€ ์•Š์•˜์Œ!!
  • Chunk ๊ธฐ๋ฐ˜ : ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹ → ItemReader, ItemProcessor, ItemWriter ๋กœ ๊ตฌ์„ฑ

 

6๏ธโƒฃ ๋ฐฐ์น˜ Job ์‹คํ–‰ 

- BatchRunner ๋กœ ์‹คํ–‰ํ•ด๋ณด๊ธฐ

: CommandLineRunner๋ฅผ ์‚ฌ์šฉํ•ด์„œ Spring Boot ์‹คํ–‰ํ•  ๋•Œ ๋ฐฐ์น˜ Job ์ž๋™์œผ๋กœ ์‹คํ–‰ํ•˜๋„๋ก !

BatchRunner

 @Component
@RequiredArgsConstructor
public class BatchRunner implements CommandLineRunner
{
	private final JobLauncher jobLauncher;
	private final Job importPersonJob;

	@Override
	public void run(String... args) throws Exception
	{
		JobParameters jobParameters = new JobParametersBuilder()
				.addLong("time", System.currentTimeMillis())
				.toJobParameters();

		jobLauncher.run(importPersonJob, jobParameters);
	}
}

 

- Spring Scheduler ์ด์šฉํ•ด์„œ ํŠน์ • ๊ฐ„๊ฒฉ์œผ๋กœ ์‹คํ–‰ํ•ด๋ณด๊ธฐ

PersonSchedule

@Slf4j
@Component
@RequiredArgsConstructor
public class PersonSchedule
{
	private final JobLauncher jobLauncher;
	private final Job importPersonJob;

	@Scheduled(cron = "0 */3 15-16 * * ?")
	public void startJob()
	{
		JobParameters jobParameters = new JobParametersBuilder()
				.addLong("time", System.currentTimeMillis())
				.toJobParameters();

		try
		{
			jobLauncher.run(importPersonJob, jobParameters);
		}
		catch (JobExecutionAlreadyRunningException
				| JobRestartException
				| JobInstanceAlreadyCompleteException
				| JobParametersInvalidException
				e)
		{
			log.error("startJob run error :{}", e.getMessage(), e);
		}
	}
}
  • @Scheduled(cron = "0 */3 15-16 * * ?")
    • 0 : (ํ•œ ๋ฒˆ) → ๋งŒ์•ฝ 3
    • */3 : 3๋ถ„๋งˆ๋‹ค ์‹คํ–‰
    • 15-16 : ์˜คํ›„ 15์‹œ๋ถ€ํ„ฐ 16์‹œ ๋™์•ˆ๋งŒ ์‹คํ–‰
    • * : ๋งค์ผ ์‹คํ–‰
    • * : ๋งค์›” ์‹คํ–‰
    • ? : ํŠน์ • ์š”์ผ ๋ฌด๊ด€

 

 

 

 

 

 

 

728x90