반응형

# 스프링 부트 스케줄러 적용방법

## EnableScheduling

  • 스프링 부트 프로젝트에서 Application에 아래의 어노테이션 적용
@EnableScheduling
  • 적용 예
package com.sbs.lhh.hp;

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

@SpringBootApplication
@EnableScheduling
public class HpApplication {

	public static void main(String[] args) {
		SpringApplication.run(HpApplication.class, args);
	}

}

## Scheduled

  • 스케줄 작업을 진행할 항목에 아래의 어노테이션 적용
@Scheduled( 시간 설정 )
  • 적용 예
@Scheduled(cron = "0 0/30 * * * ?")
	public void autoGetCovid19Status() {
		System.out.println("작동확인");
		List<CovidData> covidDatas = articleService.getCovidData();
		
		List<CovidData> covidDataList;
		
		if ( covidDatas.isEmpty() ) {
			try {
				covidDataList = crawlingService.getCovidDatas();
				for (CovidData data : covidDataList) {
					articleService.setCovidData(data);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}	
		} else {
			try {
				covidDataList = crawlingService.getCovidDatas();

				for (CovidData data : covidDataList) {
					articleService.setCovidDataUpdate(data);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
반응형

+ Recent posts