spring(boot)
[boot]Scheduling: method를 주기적으로 실행시키려면?
수학소년
2022. 10. 26. 00:36
특정 method를 주기적으로 실행시키려면?
Spring batch가 필요할줄 알고 회사에서 spring batch관련 책을 사달래서 읽어봤다.. 읽어봤는데..
quartz를 이용해서 서버 시작하면서 n초 간격으로 m번 반복하는 내용이 있었다.
나는 특정 시간(ex. 매일 새벽 2시)마다 실행시켜야 했다.
구글링 해보니 @Scheduled 이라는 annotation이 있었다.
@Scheduled
특정 method위에 @Scheduled 과 함께,
cron, fixedDelay, fixedRate로 실행 주기를 설정 할 수 있다.
ex. 매일 새벽 2시는 @Scheduled(cron = "0 2 * * *")
@Scheduled가 적용되려면 다음과 같은 조건이 필요함.
1. @EnableScheduling: @Scheduled를 포함하는 class 또는 스프링 부트 실행하는 class 위에 @EnableScheduling를 달아놔야함.
2. Bean등록: @Scheduled를 포함하는 class가 bean에 등록되어 있어야 한다.(@Service, @Repository, @Component 등등..)
..............
@Component // <- bean에 등록할 수 있는 annotation
@EnableScheduling
public class BatchService {
@Scheduled(fixedDelay = 10000)
public ....................
Spring Batch 없어도 됌. 걍 완전 spring 기본 기능