java spring boot

spring boot - fixedDelay

2pie 2022. 4. 15. 15:57
반응형

Application 에 @EnableScheduling  꼭 해줘야 합니다.

@EnableScheduling //스케쥴사용시 추가
@SpringBootApplication
public class KimScheduleApplication {

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

}

------------------------------------------------------------------------------------------------------------------

1초마다 scheduleFixedDelayTask() 실행시 사용하는 방법.

fixedDelay, fixedRate 는 동일하게 지정된 시간간격으로 실행을 하지만, 시점의 차이가 있습니다.

fixedDelay=1000 설정시, scheduleFixedDelayTask() 실행이 끝나고 1초 후에  scheduleFixedDelayTask() 가 다시 실행합니다.

fixedRate=1000 설정시, scheduleFixedDelayTask() 실행이 시작하는 시점기준 1초 후에  scheduleFixedDelayTask() 가 다시 실행합니다.

시작하는 시점의 차이입니다.

@Slf4j
@Component
public class KimSchedule {

   @Scheduled (fixedDelay=1000) 
   public void scheduleFixedDelayTask() throws InterruptedException, JSONException, IOException {
      try {
         log.info("Fixed delay task - {}", System.currentTimeMillis() / 1000);
      } catch(InterruptedException e) {
         log.error("InterruptedException ERROR: {} ", e.getMessage());
      }
   }

}

반응형