Java/Spring Boot
                
              [Spring Boot] Scheduler 설정
                Ishaya seon
                 2022. 3. 21. 09:46
              
              
                    
        반응형
    
    
    
  
1. @EnableScheduling 어노테이션 추가
- @SpringBootApplication 어노테이션이 있는 Application.java 파일에 @EnableScheduling 어노테이션 추가
@SpringBootApplication
@EnableScheduling
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}
2. Scheduler.java 파일 생성
- Scheduler 클래스에 @Component 어노테이션 추가
3. 메소드 작성 및 @ Scheduled 어노테이션 작성 (크론표현식 이용)
- @Scheduled(cron = "0 */5 * * * *") 5분주기
@Component
@Slf4j
public class Scheduler {
	
	@Scheduled(cron = "0 * * * * *")	// 1분마다
	public void test1() throws Exception {
		log.info("1분마다 수행");
	}
	@Scheduled(cron = "0 */5 * * * *")	// 5분마다
	public void test2() throws Exception {
			log.info("5분마다 수행");
	}
	@Scheduled(cron = "0 0 0 * * *")	// 매일 00시 정각
	public void test3() throws Exception {		
		log.info("매일 00시 수행");
	}
	
}