? Spring Boot에서 @Scheduled
사용하기
Spring Boot의 @Scheduled
어노테이션을 활용하면 정해진 시간 간격으로 특정 작업을 자동 실행할 수 있습니다.
예를 들어, 주기적으로 뉴스 RSS를 가져와 데이터베이스에 저장하거나 크롤링하는 작업을 자동화할 수 있습니다.
✅ 1. @EnableScheduling
활성화
Spring Boot에서 @Scheduled
을 사용하려면 스케줄링 기능을 활성화해야 합니다.@EnableScheduling
을 메인 클래스 또는 설정 클래스에 추가하면 됩니다.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling // 스케줄링 활성화
public class ScheduledApp {
public static void main(String[] args) {
SpringApplication.run(ScheduledApp.class, args);
}
}
✅ 2. @Scheduled
을 활용한 자동 실행 작업
? 주기적으로 RSS에서 뉴스 가져오기
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Component
public class YahooTechNewsScheduler {
private static final String YAHOO_RSS_URL = "https://www.yahoo.com/news/rss/tech";
// 10초마다 실행 (초, 분, 시간, 일, 월, 요일)
@Scheduled(fixedRate = 10000) // 10초마다 실행
public void fetchNews() {
System.out.println("Fetching Yahoo Tech News...");
try {
// 1. Yahoo Tech RSS 가져오기
Document doc = Jsoup.connect(YAHOO_RSS_URL).get();
Elements items = doc.select("item");
List<String> newsList = new ArrayList<>();
// 2. 기사 5개 가져오기
for (Element item : items.subList(0, Math.min(5, items.size()))) {
String title = item.selectFirst("title").text();
String link = item.selectFirst("link").text();
newsList.add("Title: " + title + "\nLink: " + link);
}
// 3. 출력
newsList.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
✅ 3. @Scheduled
속성 상세 설명
속성 | 설명 | 예시 |
---|---|---|
fixedRate | 이전 실행이 끝난 후, 지정된 간격(밀리초)마다 실행 | @Scheduled(fixedRate = 5000) (5초마다 실행) |
fixedDelay | 이전 실행이 끝난 후, 지정된 시간 후에 실행 | @Scheduled(fixedDelay = 5000) (5초 후 실행) |
initialDelay | 처음 실행될 때까지 대기하는 시간(밀리초) | @Scheduled(initialDelay = 10000, fixedRate = 5000) |
cron | 크론 표현식을 사용해 특정 시간에 실행 | @Scheduled(cron = "0 0 * * * *") (매 정각 실행) |
✅ 4. cron
표현식 활용 예제
cron
표현식을 사용하면 특정 시간대에 맞춰 실행할 수 있습니다.
// 매일 오전 6시에 실행
@Scheduled(cron = "0 0 6 * * *")
// 매시간 정각 실행
@Scheduled(cron = "0 0 * * * *")
// 매일 10초마다 실행
@Scheduled(cron = "*/10 * * * * *")
// 매월 1일 오전 3시에 실행
@Scheduled(cron = "0 0 3 1 * *")
✅ 5. 실행 결과 예시
? Fetching Yahoo Tech News...
? AI Breakthrough in Quantum Computing
? https://www.yahoo.com/news/tech/ai-quantum-computing
? Apple Announces New iPhone 16
? https://www.yahoo.com/news/tech/apple-iphone-16
-------------------------------------------------
? Fetching Yahoo Tech News...
? AI Breakthrough in Quantum Computing
? https://www.yahoo.com/news/tech/ai-quantum-computing
? Apple Announces New iPhone 16
? https://www.yahoo.com/news/tech/apple-iphone-16
-------------------------------------------------
✅ 6. 정리
@EnableScheduling
→ 스케줄링 기능 활성화@Scheduled
→ 자동 실행 주기를 설정 (fixedRate
,fixedDelay
,cron
)Jsoup
을 활용해 RSS에서 뉴스 가져오기- 주기적으로 야후 뉴스 RSS를 수집하는 서비스 구현 가능