- ์ผ์ ํ ์๊ฐ ๊ฐ๊ฒฉ์ผ๋ก ๋๋ ํน์ ์ผ์ ์ ๋ฉ์๋๋ฅผ ์คํ๋๋๋ก ํ๋ ๊ธฐ๋ฅ
- Spring Boot starter ์ ๊ธฐ๋ณธ์ ์ผ๋ก ์์กด
org.springframework.scheduling
- ๋ฉ์๋๋ void ํ์
์ด์ด์ผ ํ๋ค.
- ๋ฉ์๋๋ ๋งค๊ฐ๋ณ์ ์ฌ์ฉ ๋ถ๊ฐ
- fixedDelay
- ์ด์ ์์
์ด ์ข
๋ฃ๋ ํ ์ค์ ์๊ฐ (milliseconds) ์ดํ ๋ค์ ์์
- ์ด์ ์์
์ด ์๋ฃ๋ ๋๊น์ง ๋๊ธฐ
- fixedRate
- ๊ณ ์ ์๊ฐ ๊ฐ๊ฒฉ์ผ๋ก ์์
- ์ด์ ์์
์ด ์๋ฃ๋ ๋๊น์ง ๋ค์ ์์
์ด ์งํ๋์ง ์์
- ๋ณ๋ ฌ ๋์์ ์ฌ์ฉํ ๊ฒฝ์ฐ
@Async ์ถ๊ฐ
- ํด๋์ค์
@EnableAsync ์ถ๊ฐ
- ์์ ์ฝ๋
@Async
@Scheduled(fixedRate = 1000)
public void scheduleFixedRate() throws InterruptedException {
log.info("Fixed rate scheduled - {}", System.currentTimeMillis() / 1000);
Thread.sleep(1000);
}
- fixedDelay + initialDelay
@initialDelay ๊ฐ ์ดํ ์ฒ์ ์คํ๋๊ณ , @fixedDelay ๊ฐ์ ๋ฐ๋ผ ๊ณ์ ์ํ
- ์์ ์ฝ๋
@Scheduled(fixedDelay = 1000, initialDelay = 3000)
public void scheduleFixedDelayAndInitialDelayTask() {
log.info("FixedDelay and InitialDelay scheduled - {}", System.currentTimeMillis() / 1000);
}
- Cron
- ์ง์ ์์ฝ์ผ๋ก ์ํ
- ์์ ์ฝ๋
@Scheduled(cron = "0 15 11 20 * ?") // ๋งค์ 15์ผ ์ค์ 11์ 20๋ถ์ ์คํ
public void scheduledCronTask() {
log.info("Cron scheduled - {}", System.currentTimeMillis() / 1000);
}