0. 들어가기 전에 참고

 

  @Component 어노테이션은 Spring에 해당 class가 Bean으로 등록되어야 함을 나타냄

 

 - cron 표현식

1 2 3 4 5 6  // 순서
* * * * * *  // 실행주기 문자열

// 순서별 정리
1. 초(0-59)
2. 분(0-59)
3. 시간(0-23)
4. 일(1-31)
5. 월(1-12)
6. 요일(0-7)
// 0, 7 : 일요일
// 1부터 6까지 : 월 ~ 토
"0 0 * * * *" = the top of every hour of every day.
"*/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 and 10:30 every day.
"0 0 9-17 * * MON-FRI" = 오전 9시부터 오후 5시까지 주중(월~금)에 실행한다.
"0 0 0 25 12 ?" = every Christmas Day at midnight

 

 

 

 

 

1. fixedRate

 

 - 특정 시간이 지날때마다 실행

 

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(fixedRate = 1000)
    fun executeTask() {
        // 1초마다 실행되는 작업
    }
    
}

 

 

 

 

 

2. cron

 

 - 특정 시간이 될 때마다 실행

 

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "0 * * * * *")
    fun executeTask() {
        // 매 분마다 00초에 실행되는 작업
    }
    
}

 - 리눅스 crontab 표현식과 차이가 있습니다.

https://growingsaja.tistory.com/432

 

[Linux][crontab] time, schedule, cycle Setting

[ 작업 환경 ] Centos 6, 7 프로세스 예약 데몬 리눅스용 작업 스케줄러 * * * * * 수행할 명령어 분 시 일 월 요일 분 : 0~59 시 : 0~23 일 : 1~31 월 1~12 요일 : 0~6 >>> 0 : 일요일 / 1 : 월요일 / 2 : 화요일 / 3 :

growingsaja.tistory.com

 

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "0 0 * * * *")
    fun executeTask() {
        // 매시 정각마다 실행되는 작업
    }
    
}

 

 

 

 

 

3. cron &, ?를 활용한 기본 예제

 

 * : 모든 값에 가능

 ? : 아예 무시

 

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "0 0 9 ? * MON")
    fun executeTask() {
        // 매월 월요일 9시에 실행되는 작업
    }
    
}

 

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "0 0 0 5 5 ?")
    fun executeTask() {
        // 매년 5월 5일에 실행되는 작업
    }
    
}

 

 

 

 

 

4. cron / 사용 예제

 

    */2 와 0/2 는 동일합니다.

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "0 */2 * * * *")
    fun executeTask() {
        // 매 2분마다 0초에 실행
    }
    
}

 

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "0 0/2 * * * *")
    fun executeTask() {
        // 매 2분마다 0초에 실행
    }
    
}

 

    */5 와 0/5 는 동일합니다.

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "0/5 */2 * * * *")
    fun executeTask() {
        // 매 2로 나누어 떨어지는 분에 1분간 매 5초마다 실행
    }
    
}

 

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "*/5 */2 * * * *")
    fun executeTask() {
        // 매 2로 나누어 떨어지는 분에 1분간 매 5초마다 실행
    }
    
}

 

 

 

 

 

5. - 사용 예제

 

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "55 10 3-5 * * *")
    fun executeTask() {
        // 3시부터 5시 사이에만 매 10분 55초마다 작업
    }
    
}

 

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "55 10 3-5 * * MON-FRI")
    fun executeTask() {
        // 월~금요일에 3시부터 5시 사이에만 매 10분 55초마다 작업
    }
    
}

 

 

 

 

 

6. , 사용 예제

 

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "0 */5 9,18 * * *")
    fun executeTask() {
        // 매 9시와 18시에 5분마다 작업 (5로 나뉘어 떨어지는 분에 작업)
    }
    
}

 

 

 

 

 

7. W 사용 예제 ("일" 자리에서만 사용 가능)

 

가장 가까운 평일 일자에 실행

 

예를 들어, 20W로 표기했다는 가정 하에서

 - 20일이 평일인 경우 : 20일에 실행

 - 20일이 토요일인 경우 : 월요일인 22일보다 금요일인 19일이 가까우니 19일 금요일에 실행

 - 20일이 일요일인 경우 : 월요일인 21일보다 금요일인 18일이 더 멀기 때문에 21일 월요일에 실행

 

 

 

 

 

8. L 사용 예제 ("일", "요일" 자리에서만 사용 가능)

 

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ScheduledTasks {

    @Scheduled(cron = "0 0 0 L * *")
    fun executeTask() {
        // 매달 마지막 날짜 0시 0분 0초에 작업
    }
    
}

 

 

 

 

 

9. # 사용 예제 ("요일" 자리에서만 사용 가능)

 

    3#2 : 수요일 2째주에 실행

 

 요일 (0-7)
  ㄴ 0, 7 : 일요일
  ㄴ 1부터 6까지 : 월 ~ 토

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

+ Recent posts