본문 바로가기
Spring

[Spring Boot] Scheduler 사용법

by 두유_ 2024. 6. 12.

Spring Scheduler


Dependency

- Spring Boot starter 에 기본적으로 의존 

org.springframework.scheduling

 


Enable Scheduling

  • Project Application Class에 @EnableScheduling 추가
@EnableScheduling // 추가
@SpringBootApplication
public class SchedulerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SchedulerApplication.class, args);
    }
}
 

A Time Zones

A Time Zones The following table contains a list of time zones supported by Oracle Real-Time Collaboration. See "Property to Configure Time Zones" for details about setting the default time zone for a Web Conferencing system. Table A-1 Real-Time Collaborat

docs.oracle.com

 

Example


fixedDelay

  • 해당 메서드가 끝나는 시간 기준, milliseconds 간격으로 실행
  • 하나의 인스턴스만 항상 실행되도록 해야 할 상황에서 유용
@Scheduled(fixedDelay = 1000)
// @Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds}") // 문자열 milliseconds 사용 시
public void scheduleFixedDelayTask() throws InterruptedException {
    log.info("Fixed delay task - {}", System.currentTimeMillis() / 1000);
    Thread.sleep(5000);
}

fixedRate

  • 해당 메서드가 시작하는 시간 기준, milliseconds 간격으로 실행
  • 병렬로 Scheduler 를 사용할 경우, Class에 @EnableAsync, Method에 @Async 추가
  • 모든 실행이 독립적인 경우에 유용
@Async
@Scheduled(fixedRate = 1000)
// @Scheduled(fixedRateString = "${fixedRate.in.milliseconds}")  // 문자열 milliseconds 사용 시
public void scheduleFixedRateTask() throws InterruptedException {
    log.info("Fixed rate task - {}", System.currentTimeMillis() / 1000);
    Thread.sleep(5000);
}

Cron

  • 작업 예약으로 실행
@Scheduled(cron = "0 15 10 15 * ?") // 매월 15일 오전 10시 15분에 실행
// @Scheduled(cron = "0 15 10 15 11 ?") // 11월 15일 오전 10시 15분에 실행
// @Scheduled(cron = "${cron.expression}")
// @Scheduled(cron = "0 15 10 15 * ?", zone = "Europe/Paris") // timezone 설정
public void scheduleTaskUsingCronExpression() {
    long now = System.currentTimeMillis() / 1000;
    log.info("schedule tasks using cron jobs - {}", now);
}
  • Cron 주기 참고
@Scheduled(fixedDelay = 3000) // 3초마다 실행
@Scheduled(fixedRate = 3000) // 3초마다 실행
@Scheduled(cron = "*/5 * * * * *") // 5초마다 실행
@Scheduled(cron = "0/15 * * * * *") // 0초에 시작해서 15초 간격으로 동작
@Scheduled(cron = "* 5/10 * * * *") // 5분에 시작해서 10분 간격으로 동작
@Scheduled(cron = "0 0 12 * * *") // 매일 12시에 실행
@Scheduled(cron = "0 15 10 * * *") // 매일 10시 15분에 실행
@Scheduled(cron = "0 * 14 * * *") // 매일 14시에 0~59분까지 매분 실행
@Scheduled(cron = "0 0/5 14 * * *") // 매일 14시에 0~59분까지 5분 간격으로 실행
@Scheduled(cron = "0 0/5 14,16 * * *") // 매일 14시, 16시 0~59분까지 5분 간격으로 실행
@Scheduled(cron = "0 0-3 14 * * *") // 매일 14시 0분, 1분, 2분, 3분에 실행
@Scheduled(cron = "0 0 20 ? * MON-FRI") // 월~금일 20시 0분 0초에 실행
@Scheduled(cron = "0 0/5 14 * * ?") // 아무요일, 매월, 매일 14:00부터 14:05분까지 매분 0초 실행 (6번 실행)
@Scheduled(cron = " 0 15 10 ? * 6L") // 매월 마지막 금요일 아무날이나 10:15:00에 실행

 

Spring boot Scheduled Async


Async pool size 변경

Async의 기본 pool size는 8이고, 수정하려면 다음과 같이 수정한다.

spring:
  task:
    execution:
      pool:
        core-size: 10

 

 

Reference

https://data-make.tistory.com/699
https://m.blog.naver.com/hj_kim97/222328217338
https://jeong-pro.tistory.com/186#google_vignette

'Spring' 카테고리의 다른 글

[Spring Boot] HikariCP log 출력하기  (0) 2024.09.11
[Spring boot] Swagger 설정하기 (gradle)  (0) 2024.09.10
[Java] 멀티스레드 사용하기  (0) 2024.06.14