관리 메뉴

ふたりで

springboot @Scheduled 사용 해 보기 본문

Spring

springboot @Scheduled 사용 해 보기

graykang 2020. 12. 1. 18:03
728x90
반응형
SMALL

1. @EnableScheduling 어노테이션을 해당 프로젝트의 Application.java 클래스에 달아 준다.

예)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;

@SpringBootApplication
@EnableEncryptableProperties
@EnableScheduling//스케쥴러 사용을 위한 어노테이션
public class TestApplication {
	public static void main(String[] args) {
		SpringApplication.run(TestApplication.class, args);
	}

}

 2. @Component와 @Scheduled 스케쥴러가 동작되는 서비스 클래스에 명시해 준다.

예)

import org.joda.time.LocalDateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduleTaskService {
	
	@Scheduled(fixedDelay = 20000)//1000 = 1초 2만으로 설정 해서 20초마다 동작
	public void taskOne() {
		System.out.println("default schedule GetTime:"+LocalDateTime.now());
	}
	
	@Scheduled(cron = "0/5 * * * * *")//0초에 시작해서 이후 5초마다 실행.
	public void taskTwo() {
		System.out.println("cron schedule GetTime:"+LocalDateTime.now());
	}

}

3. cron 옵션 예시

 "0 0 12 * * ?" : 아무 요일, 매월, 매일 12:00:00
 "0 30 10 ? * *" : 모든 요일, 매월, 아무 날이나 10:30:00 
 "0 15 10 * *?" : 아무 요일, 매월, 매일 10:15:00 
 "0 10 11 * * ? *" : 모든 연도, 아무 요일, 매월, 매일 11:10 
 "0 * 14 * * ?" : 아무 요일, 매월, 매일, 14시 매분 0초 
 "0 15 10 * * ? : 2005" 2005년 아무 요일이나 매월, 매일 10:15 

 "0 0/5 14 * * ?" : 아무 요일, 매월, 매일, 14시 매 5분마다 0초 
 "0 0/5 14,18 * * ?" : 아무 요일, 매월, 매일, 14시, 18시 매 5분마다 0초 
 "0 0-5 14 * * ?" : 아무 요일, 매월, 매일, 14:00부터 매 14:05까지 매 분 0초 
 "0 10,44 14 ? 3 WED" : 3월의 매주 수요일, 아무 날짜나 14:10:00, 14:44:00 
 "0 15 10 ? * MON-FRI" : 월~금, 매월, 아무 날이나 10:15:00 
 "0 15 10 15 * ?" : 아무 요일, 매월 15일 10:15:00 
 "0 15 10 L * ?" : 아무 요일, 매월 마지막 날 10:15:00 
 "0 15 10 ? * 6L" : 매월 마지막 금요일 아무 날이나 10:15:00 
 "0 15 10 ? * 6L 2002-2005" : 2002년부터 2005년까지 매월 마지막 금요일 아무 날이나 10:15:00 
 "0 15 10 ? * 6#3" : 매월 3번째 금요일 아무 날이나 10:15:00

 

[출처] [Spring] @Scheduled 정해진 시간에 맞춰서 모듈 실행하기|작성자 행복가

728x90
반응형
LIST
Comments