[Spring] 스프링 스케쥴러 (@Scheduled)
코드
@Component
public class ScheduleCron {
SimpleDateFormat yyyyMMddHHmmssFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Scheduled(fixedDelay = 1000)
public void timer1() {
System.out.println("[ScheduleCron timer1]" + yyyyMMddHHmmssFormat.format(new Date()));
}
}
결과
코드
@Scheduled(initialDelay = 3000, fixedDelay = 1000)
public void timer2() {
System.out.println("[ScheduleCron timer2]" + yyyyMMddHHmmssFormat.format(new Date()));
}
결과
코드
@Scheduled(cron = "5 * * * * *")
public void timer3() {
System.out.println("[ScheduleCron timer3]" + yyyyMMddHHmmssFormat.format(new Date()));
}
결과
코드
@Scheduled(fixedRate = 4000)
public void timer4() {
System.out.println("[ScheduleCron timer4]" + yyyyMMddHHmmssFormat.format(new Date()));
}
결과
설명
- fixedDelay
- 작업완료 시간 간격을 지정 fixedDelay = 1000 (milliseconds)
- fixedDelay
- 작업완료 시간 간격을 지정 fixedDelay = 1000 (milliseconds)
- fixedDelayString
- 작업완료 시간 간격을 문자열로 지정 fixedDelayString = "1000" (milliseconds)
- fixedRate
- 작업시작 시간 간격을 지정 fixedDelay = 1000 (milliseconds)
- fixedRateString
- 작업시작 시간 간격을 문자열로 지정 fixedRateString = "1000" (milliseconds)
- initialDelay
- 초기 지연시간 설정 initialDelay = 1000 (milliseconds)
- initialDelayString
- 초기 지연시간 설정을 문자열로 지정 initialDelayString = "1000" (milliseconds)
- cron
- cron 표현식
초 분 시 일 월 요일
* * * * * *
초 : 0 ~ 59 * - /
분 : 0 ~ 59 * - /
시 : 0 ~ 23 * - /
일 : 1 ~ 31 * - /? L W
월 : 1 ~ 12 JAN-DEC * - / (JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC)
요일 : 1 ~ 7 SUN-SAT * - / ? L # (SUN, MON, TUE, WED, THU, FRI, SAT)
* : 모두 (매초, 매분, 매시, 매일, 매월, 매 요일)
- : 기간 설정
(1-3이면 1시, 2시, 3시에 동작)
/ : 시작시간과 간격 반복 설정
(0/5이면 0초에 시작해서 5초 간격으로 실행)
? : 해당 항목 사용 안 함
L : 마지막 기간에 사용
(월에 사용하면 마지막 날에 동작, 요일에 사용하면 토요일 실행)
W : 가장 가까운 평일에 사용
(1W 설정하고 1일이 토요일이면 3일 월요일에 실행)
(1W 설정하고 1일이 일요일이면 2일 월요일에 실행)
LW : 해당 월 마지막 평일 동작
# : 몇 번째 주와 요일 설정
(1#2 이면 일요일 2번째 주 실행)
(2#3 이면 월요일 3번째 주 실행)
"0 0 * * * *" 매일 매시간의 최상위입니다.
"* / 10 * * * * *" 10 초마다
"0 0 8-10 * * *" 매일 8시, 9시, 10시.
"0 0 6,19 * * *" 매일 오전 6시와 오후 7시.
"0 0/30 8-10 * * *" 매일 8:00, 8:30, 9:00, 9:30, 10:00 및 10:30.
"0 0 9-17 * * MON-FRI" 평일 9시에서 5 시까 지
"00 25 12?" 매일 크리스마스 (자정)
http://www.cronmaker.com/ (cron 표현식 생성 사이트)
참고 사이트
https://spring.io/guides/gs/scheduling-tasks/
Scheduling Tasks
this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team
spring.io
https://en.wikipedia.org/wiki/Cron#CRON_expression