[Spring] ๋น„๋™๊ธฐ ์ž‘์—…๊ณผ ๋ชจ๋‹ˆํ„ฐ๋ง์„ ์œ„ํ•œ TaskExecutor, TaskScheduler ๊ธฐ๋ณธ

๋ฐ˜์‘ํ˜•

Spring Framework๋Š” TaskExecutor ์ธํ„ฐํŽ˜์ด์Šค์™€ TaskScheduler ์ธํ„ฐํŽ˜์ด์Šค๋กœ Task(์ž‘์—…)์˜ ๋น„๋™๊ธฐ ์‹คํ–‰๊ณผ ์Šค์ผ€์ค„๋ง์— ๋Œ€ํ•œ ์ถ”์ƒํ™” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ์ด ํ•„์š”ํ•œ ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

 

๊ธฐ๋ณธ์ ์œผ๋กœ Spring์€ Multi-Thread ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉฐ Singleton Pattern์„ ์ด์šฉํ•ด ๋ชจ๋“  Bean๋“ค์„ ๊ณต์œ ํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ API๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ณผ์ •์„ Blockingํ•˜์ง€ ์•Š๊ณ  ๋‹ค์ค‘ ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ์ฒ˜๋ฆฌํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๊ฐ€ API ํ˜ธ์ถœ ์ดํ›„ Bean ๋‚ด ์–ด๋–ค ํŠน์ • ์ž‘์—…์— ๋Œ€ํ•œ ๋น„๋™๊ธฐ ์‹คํ–‰์€ ๋ณด์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

 

 

TaskExecutor

TaskExecutor๋Š” Spring 2.0์—์„œ ๋“ฑ์žฅํ•œ ๋น„๋™๊ธฐ ์‹คํ–‰ ์ถ”์ƒํ™” ์ธํ„ฐํŽ˜์ด์Šค๋กœ JDK 1.5์—์„œ Thread Pool์— ๋Œ€ํ•œ ๊ฐœ๋…์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” Executor๋ฅผ ์ƒ์†ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด Pool์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์“ฐ๋ฉด ๋ ํ…๋ฐ ์™œ Executor์ผ๊นŒ?

 

๊ทธ๊ฒƒ์€ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌํ˜„์ฒด๊ฐ€ ์‹ค์ œ๋กœ ํ’€(Pool)์ด๋ผ๋Š” ๋ณด์žฅ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ Executor๋Š” ์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ(Single-Thread)๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ณ  ๋™๊ธฐํ™”๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

java.util.concurrent.Executor์™€ ๋‹ค๋ฅธ ์ ์€ ๋ฌด์—‡์ผ๊นŒ?

 

Spring์˜ TaskExecutor๊ฐ€ ๋ณ„๋„์˜ ์ถ”์ƒํ™” ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์กด์žฌํ•˜๋Š” ์ด์œ ๋Š” ์Šค๋ ˆ๋“œ ํ’€์— ๋Œ€ํ•œ ์ถ”์ƒํ™”๊ฐ€ ํ•„์š”ํ•œ ๊ณณ์— ๋‹ค๋ฅธ ์Šคํ”„๋ง ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผํ…Œ๋ฉด ApplicationEventMulticaster๋‚˜ JSM์˜ AbstractMessageListenerContainer, Quartz ํ†ตํ•ฉ ์ปดํฌ๋„ŒํŠธ๋“ค์€ ๋ชจ๋‘ ์Šค๋ ˆ๋“œ ํ’€์— TaskExecutor ์ถ”์ƒํ™”๋ฅผ ๊ตฌํ˜„ํ•œ ๊ตฌํ˜„์ฒด๋“ค์ž…๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ๋นˆ(Bean) ๋‚ด์—์„œ ์Šค๋ ˆ๋“œ ํ’€ ๋™์ž‘(๋น„๋™๊ธฐ ์ž‘์—…)์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ์ด ๋˜ํ•œ ์ž์‹ ์˜ ์š”๊ตฌ ์‚ฌํ•ญ์— ๋งž๊ฒŒ TaskExecutor๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

TaskExecutor ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ํ•˜๊ณ ์ž ํ•˜๋Š” ์ž‘์—…์„ ๋ฐ›์•„๋“ค์ด๋Š” Submitter, ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ž‘์—…์„ ์‹คํ–‰ํ•  ์‹ค์ œ ์ž‘์—…์„ Task, ๊ทธ ์ž‘์—…์„ ์‹คํ–‰ํ•  Thread๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

Java์˜ ์Šค๋ ˆ๋“œ ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์ด๋ ‡๊ฒŒ Thread Pool์„ ์‚ฌ์šฉํ•ด์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•˜๋Š” ์ด์œ ๋Š” ๋ฐ”๋กœ ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์‹œ๊ฐ„์ ์ธ ๋น„์šฉ์„ ์ค„์ด๊ณ , ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ๋Š” OOM(OutOfMemory) ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž…๋‹ˆ๋‹ค.

 

 

 

 

TaskExecutor ์ข…๋ฅ˜

Spring ๋ฐฐํฌ๋ณธ์—๋Š” ์ˆ˜๋งŽ์€ TaskExecutor ๊ตฌํ˜„์ฒด๋“ค์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘ ๊ตฌํ˜„ํ•ด์•ผ ํ•  ์ผ์€ ๊ฑฐ์˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ ์ค‘ ์ผ๋ถ€ ๋ช‡ ๊ฐ€์ง€๋งŒ ๋‹ค๋ค„๋ณด์ž๋ฉด...

 

  • SimpleAsyncTaskExecutor

    ์–ด๋–ค ์Šค๋ ˆ๋“œ๋„ ์žฌ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ํ˜ธ์ถœ ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” Executor.
    ํ•˜์ง€๋งŒ ๋™์‹œ ์ ‘๊ทผ ์ œํ•œ(concurrency limit)์„ ํ†ตํ•ด ์ œํ•œ ์ˆ˜๋ฅผ ๋„˜์–ด์„œ๋ฉด ๋นˆ ๊ณต๊ฐ„์ด ์ƒ๊ธธ ๋•Œ๊นŒ์ง€ ๋ชจ๋“  ์š”์ฒญ์„ ๋ฐ›์ง€ ์•Š์Œ.

  • SyncTaskExecutor

    ์ž‘์—…์„ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ , ์ž‘์—…์„ ํ˜ธ์ถœํ•œ ์Šค๋ ˆ๋“œ์— ์ถ”๊ฐ€๋˜๋Š” Executor
    Multi-thread ๋ฐฉ์‹์ด ์•„๋‹˜.

  • ConcurrentTaskExecutor

    ์ด ๊ตฌํ˜„์ฒด๋Š” Spring์˜ TaskExecutor๊ฐ€ ์•„๋‹Œ Java์˜ java.util.concurrent.Executor์˜ ๋ž˜ํผ๋กœ Spring๊ณผ ๊ฐ™์ด ์“ฐ๋ ค๋ฉด ๋ถ€๊ฐ€์ ์œผ๋กœ ๊ด€๋ จ๋œ ๋ฌด์–ธ๊ฐ€๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

    ์ด์˜ ๋Œ€์•ˆ์œผ๋กœ Bean Property ์„ค์ • ๊ด€๋ จ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋…ธ์ถœํ•˜๋Š” ThreadPoolExecutor๊ฐ€ ์žˆ๋Š”๋ฐ, ThreadPoolExecutor๊ฐ€ ์›ํ•˜๋Š” ๋งŒํผ ์•ˆ์ •์ ์ด์ง€ ์•Š๋‹ค๋ฉด, ConcurrentTaskExecutor๋ฅผ ๋น„์Šทํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

  • SimpleThreadPoolTaskExecutor

    Quartz SimpleThreadPool์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋กœ Spring์˜ Lifecycle callback์„ ๋ฐ›๋Š” Executor.
    Quartz์™€ Quartz๊ฐ€ ์•„๋‹Œ ์ปดํฌ๋„ŒํŠธ ๊ฐ„์˜ ๊ณต์œ ํ•ด์•ผ ํ•˜๋Š” ์Šค๋ ˆ๋“œ ํ’€์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ.

  • ThreadPoolTaskExecutor

    java.util.concurrent.ThreadPoolExecutor๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” Bean Property๋ฅผ ๋…ธ์ถœํ•˜๊ณ  TaskExecutor๋ฅผ ์ƒ์†ํ•œ ๊ตฌํ˜„์ฒด.
    ScheduledThreadPoolExecutor์˜ ๊ตฌํ˜„์ฒด์ด๊ธฐ๋„ ํ•ด์„œ ์ด ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด ConcurrentTaskExecutor ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋Š” ๊ฑธ ๊ถŒ์žฅ.

  • AsyncTaskExecutor

    Spring์—์„œ @EnableAsync๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” SimpleAsyncTaskExecutor์˜ ์ถ”์ƒํ™” ์ธํ„ฐํŽ˜์ด์Šค.

  • AsyncListenableTaskExecutor

    Submit์„ ์ถ”๊ฐ€ํ•  ๋•Œ ListenableFuture ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ์ธํ„ฐํŽ˜์ด์Šค.
    (ListenableFuture๋Š” Executor์— Task๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ๋Œ๋ ค ๋ฐ›๋Š” Future ํƒ€์ž… ์ค‘ ํ•˜๋‚˜)

 

TaskExecutor์˜ ๊ตฌํ˜„์ฒด๋Š” ์•ž์—์„œ ๋ดค๋˜ TaskExecutor์˜ ์ถ”์ƒํ™” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฑฐ์˜ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๊ทธ ๊ตฌํ˜„์ฒด์— ๋”ฐ๋ผ ๋น„๋™๊ธฐ์ ์ธ Executor๊ฐ€ ์žˆ๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ Executor๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ •๋ฆฌํ•˜๋ฉด Executor๋Š” ์Šค๋ ˆ๋“œ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฉฐ ์ด ๊ด€๋ฆฌ์—๋Š” ์œ ํœด ์ƒํƒœ ์ œ์–ด์™€ ์Šค๋ ˆ๋“œ ์ƒ์„ฑ ์ œ์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

TaskExecutor์—๋Š” ๋‹จ์ˆœํžˆ execute ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์‹คํ–‰ํ•  Task๋งŒ์„ ๋ฐ›์•„๋‚ด๋ฉด ๋‚˜๋จธ์ง€๋Š” ์ด๋ฏธ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ํ†ตํ•ด ์ž‘์—… ์œ ํœด ์ƒํƒœ์™€ ์Šค๋ ˆ๋“œ ์ œ์–ด ๋“ฑ์„ ์ง„ํ–‰ํ•ด์ค๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ, ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ๋” ์ƒ์„ธํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ExecutorService๋ฅผ ์ƒ์†ํ•˜์—ฌ AsyncListenableTaskExecutor์ฒ˜๋Ÿผ submit ์‹œ ํ•ด๋‹น ์ž‘์—…์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

TaskScheduler

TaskExecutor ์ถ”์ƒํ™”์— ์ถ”๊ฐ€๋กœ Spring 3.0์—์„œ๋Š” ๋ฏธ๋ž˜์— ์–ด๋–ค ์‹œ์ ์— ์‹คํ–‰ํ•  ํƒœ์Šคํฌ(Task)๋ฅผ ์Šค์ผ€์ค„๋งํ• ์ง€์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง„ TaskScheduler ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋„์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

Runnable๊ณผ Date๋งŒ ๋ฐ›๋Š” schedule ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ง€์ •ํ•œ ์‹œ๊ฐ„ ํ›„, ํƒœ์Šคํฌ๋ฅผ ํ•œ ๋ฒˆ ์‹คํ–‰ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. scheduleFixedRate ๋“ฑ์„ ํ†ตํ•ด ๋ฐ˜๋ณตํ•ด์„œ ํƒœ์Šคํฌ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, fixedRate, fixedDelay ๋ชจ๋‘ ๊ฐ„๋‹จํ•œ ์ฃผ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ณด๋‹ค ๋” ๋ณต์žกํ•œ ์ฃผ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๋ฉด Trigger ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

Trigger

Trigger ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ธฐ๋ณธ์€ ๊ณผ๊ฑฐ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋‚˜ ์ž„์˜์˜ ์ƒํ™ฉ์— ๊ธฐ๋ฐ˜์„ ๋‘ฌ์„œ ์‹คํ–‰ ์‹œ๊ฐ„์„ ๊ฒฐ์ •ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. ์ด์ „ ์‹คํ–‰์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ณ ๋ คํ•˜๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ๋Š” TriggerContext์— ์žˆ์œผ๋ฏ€๋กœ ์ด๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฝ”๋“œ๋ฅผ ์ž ์‹œ ์‚ดํŽด๋ณด๋ฉด nextExecutionTime ๋ฉ”์„œ๋“œ ๋ถ€๋ถ„์— TriggerContext๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด Context๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„์ฒด์— ์ฐธ๊ณ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

TriggerContext๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฉฐ SimpleTriggerContext๋ฅผ ๊ตฌํ˜„์ฒด๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ ์ด๋“ค ๋ฐ์ดํ„ฐ๋“ค์ด ๋ชจ๋‘ ์€๋‹‰ํ™” ๋˜์–ด ์žˆ์–ด ์›ํ•˜๋Š” ๋Œ€๋กœ ์ด ๋ฐ์ดํ„ฐ๋“ค์„ ํ™•์žฅํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ž์„ธํ•œ๊ฑด Trigger ๊ตฌํ˜„์ฒด๋ฅผ ๋ณด๋ฉฐ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

 

Trigger ๊ตฌํ˜„์ฒด

Spring Framework์—์„œ๋Š” CronTrigger์™€ PeriodicTrigger๋ผ๋Š” ๋‘ ๊ฐœ์˜ Trigger ๊ตฌํ˜„์ฒด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. 

 

Cron Trigger๋Š” cron ํ‘œํ˜„์‹(expression)์„ ์ด์šฉํ•ด ํƒœ์Šคํฌ๋ฅผ ์Šค์ผ€์ค„๋งํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์›”~๊ธˆ(์ฃผ์ค‘)์— 9์‹œ์— 5์‹œ ์‚ฌ์ด ๋งค 15๋ถ„๊ฐ„ ์‹คํ–‰๋˜๋Š” ์Šค์ผ€์ค„๋ง ์ž…๋‹ˆ๋‹ค.

 

PeriodicTrigger๋Š” ๊ณ ์ •๋œ ์‹œ๊ฐ„์„ ๋ฐ›๊ณ  ์ •๊ธฐ์ ์œผ๋กœ ํƒœ์Šคํฌ๋ฅผ ์Šค์ผ€์ค„๋งํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์œ„ ์ฝ”๋“œ๋Š” 1ms ๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ํƒœ์Šคํฌ๋ฅผ ์‹คํ–‰ํ•˜๊ฒ ๋‹ค๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ TimeUnit์„ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›๋Š”๋ฐ, ์ด๋ฅผ ๋ฐ›์ง€ ์•Š์œผ๋ฉด null๋กœ ๋“ค์–ด๊ฐ€๋ฉฐ null์ธ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’์ธ TimeUnit.MilliSeconds๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 

 

 

 

TaskScheduler ๊ตฌํ˜„์ฒด

Spring์˜ TaskExecutor์ฒ˜๋Ÿผ TaskScheduler ๋˜ํ•œ ๊ทธ ๊ตฌํ˜„์ฒด๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํŠน์ • ๊ตฌํ˜„์ฒด์— ์˜์กดํ•˜๊ฑฐ๋‚˜ ๊ตฌํ˜„์ฒด๋ฅผ ํž˜๋“ค๊ฒŒ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. 

 

  • ConcurrentTaskScheduler

    Spring์—์„œ @Scheduled ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์Šค์ผ€์ค„๋Ÿฌ
    ๋‹จ์ผ ์Šค๋ ˆ๋“œ๋งŒ์œผ๋กœ๋กœ ๋™์ž‘

  • DefaultManagedTaskScheduler

    J2EE์˜ ManagedScheduledExecutorService๋ฅผ ์‚ฌ์šฉํ•œ ์Šค์ผ€์ค„๋Ÿฌ.
    J2SE์™€ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๊ฑฐ์˜ ๋น„์Šทํ•˜์ง€๋งŒ ๋งค๋‹ˆ์ง• ํ˜•ํƒœ๋กœ ๋™์ž‘

  • ThreadPoolTaskExecutor

    Spring์—์„œ @Scheduled ์–ด๋…ธํ…Œ์ด์…˜์„ fixedDelay๋ฅผ ์ด์šฉํ•ด ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์Šค์ผ€์ค„๋Ÿฌ
    ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ˜•ํƒœ๋กœ ๋™์ž‘. (์Šค๋ ˆ๋“œ ๊ด€๋ฆฌ ํ˜•ํƒœ)

 

TaskScheduler์˜ ์œ ์—ฐ์„ฑ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ง์ ‘ ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š” ์„œ๋ฒ„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™˜๊ฒฝ์—์„œ ๋™์ž‘ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ์™ธ๋ถ€ ์Šค๋ ˆ๋“œ ๊ด€๋ฆฌ ํ•„์š” ์—†์ด ๊ตฌํ˜„ํ•ด์ฃผ๋Š” ThreadPoolTaskScheduler ๊ตฌํ˜„์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

 

 

 

 

๋งˆ์น˜๋ฉฐ...

์—ฌ๊ธฐ๊นŒ์ง€ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์Šคํ”„๋ง์—์„œ Bean ๋‚ด ๋น„๋™๊ธฐ ์ž‘์—… ์ฒ˜๋ฆฌ์™€ ์Šค์ผ€์ค„๋ง์— ๋Œ€ํ•ด ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ TaskExecutor์™€ TaskScheduler๋ฅผ ๋™์‹œ์— ๋‹ค๋ฃฌ ์ด์œ ๋Š” ์ด๋“ค ๋ชจ๋‘ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ๊ด€๊ณ„์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

๋ชจ๋‹ˆํ„ฐ๋ง์„ ์œ„ํ•ด ์‚ฌ๋‚ด์—์„œ ์˜ˆ์•ฝ๋œ ์ž‘์—…(Scheduled Task)์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ @Scheduled ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜์—ฌ ์šฐ๋ฆฌ ์‚ฌ๋‚ด ์‹œ์Šคํ…œ์— ๋งž์ถฐ์•ผ ํ•œ๋‹ค๋ฉด Trigger, TriggerContext ๋“ฑ์˜ ์ถ”์ƒํ™” ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋‹ค์Œ ํฌ์ŠคํŠธ์—์„œ๋Š” Executor ๊ตฌํ˜„์ฒด๋“ค ์ค‘ ๋ช‡ ๊ฐ€์ง€๋ฅผ ์ด์šฉํ•ด ์‹ค์ œ Spring์—์„œ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์‹ค์ „์„ ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

์ฐธ๊ณ : [Spring ๋ ˆํผ๋Ÿฐ์Šค] 26์žฅ ํƒœ์Šคํฌ(Task) ์‹คํ–‰๊ณผ ์Šค์ผ€์ค„๋ง
(https://blog.outsider.ne.kr/1066)

๋ฐ˜์‘ํ˜•
TAGS.

Tistory Comments