-
[Spring boot] @Scheduled 스케쥴 어노테이션 사용SpringBoot 2022. 4. 20. 16:08반응형
사용 이유
시간별로 주기적인 작업이 필요한 경우, ex) 5초에 한번씩 배치가 돌게 하고싶다
@Scheduled 어노테이션을 사용하여 특정 시간마다 동작해 스케쥴러를 구현 할 수 있다.
사용방법
1. xml 설정 필요 총 2가지 방법중 하나 골라서 사용
<새로운 xml을 만들어 추가하는 방법>
<!--?xml version="1.0" encoding="UTF-8"?--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven> </task:annotation-driven> </beans><기존 xml에 추가하는 방법>
xmlns:task="http://www.springframework.org/schema/task" xsi:schemalocation=" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven> </task:annotation-driven>사용예시
@Scheduled(cprn "0 15 14 * * ?") // 매일 오후 2시 15분 실행 public void testScheduler(){ System.out.println("확인"); }@Scheduled 속성
- fixedDelay : 이전 작업이 끝난 시점으로 부터 고정된 시간(ms)을 설정.
- fixedRate : 이전 작업이 수행되기 시작한 시점으로 부터 고정된 시간(ms)을 설정.-cron 속성 : UNIX계열 잡 스케쥴러 표현식으로 작성 - cron="초 분 시 일 월 요일 [년도]" - 요일 : 1(SUN) ~ 7(SAT)
* - 특수문자 * : 모든 수.
* - : 두 수 사이의 값. ex) 10-15 -> 10이상 15이하
* , : 특정 값 지정. ex) 3,4,7 -> 3,4,7 지정
* / : 값의 증가. ex) 0/5 -> 0부터 시작하여 5마다
* ? : 특별한 값이 없음. (월, 요일만 해당)
* L : 마지막. (월, 요일만 해당)@Scheduled 어노테이션은 매개변수가 없는 메소드에만 적용 가능.
@Scheduled(fixedDelay = 3000) : 3초마다
@Scheduled(cron = "0 * * * * *")//모든 년월일시분, 요일 0 초 마다: 매분 0초마다 라는 뜻(매분마다)
@Scheduled(cron ="( 0 0 * * .. == 0분 0초 == 정시 마다)
@Scheduled(cron ="( 0 0 0 * .. == 0시 0분 0초 == 자정 마다)
반응형'SpringBoot' 카테고리의 다른 글
[Spring boot] 어노테이션 정리 (0) 2022.05.17 [Spring boot] 스프링 부트 정리 (0) 2021.09.16