본문 바로가기
개발/Web

[Spring] Scheduler(스프링 스케줄러) 설정하기

by zuzuu 2021. 11. 30.
반응형

스프링에서 특정 시간 혹은 몇 분, 몇 시간 마다 기능을 동작 시키려면 Spring에서 제공하는 scheduler를 이용하면 된다.

(Spring 3.1부터는 Quartz없이 간단하게 스케줄러 구현이 가능하다.)

먼저 applicationContext에 스케줄러를 등록한다. (Component Scan이 선언되어 있는 파일)

  • applicationContext.xml (요약)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:task="http://www.springframework.org/schema/task"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

<!-- =================================================================== -->
<!-- Component Scan -->
<!-- =================================================================== -->
<context:component-scan base-package="com.company.test" >
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- =================================================================== -->
<!-- Scheduler -->
<!-- =================================================================== -->

<task:scheduler id="scheduler" pool-size="2" />
<task:scheduled-tasks scheduler="scheduler" >
 <task:scheduled ref="testJob" method= "doJob" cron="0 0/30 * * * *" />
</task:scheduled-tasks>
</beans>

 => testJob의 doJob 메소드를 30분마다 실행하는 스케줄러를 등록했다. testJob은 아래와 같다.

 

  • TestJob.java
@Service("testJob")
public class TestJob {
 public synchronized void doJob() {
  System.out.println("###################################################");
  System.out.println("Scheduler");
  System.out.println("###################################################");
 }
}

 

Cron Expression (출처 : https://blog.naver.com/cyon24/203674742)

왼쪽부터 오른쪽순으로 다음과 같은 의미가 있다

Seconds
0 ~ 59
Minutes
0 ~ 59
Hours
0 ~ 23
Day of Month
1 ~ 31
Month
1 ~ 12
Day of Week
1 ~ 7 (1 => 일요일, 7=> 토요일 / MON,SUN...)
Years(optional)
1970 ~ 2099

특수문자의 사용은 아래와 같은 의미가 있다.

*
모든수를 의미, Minutes 위치에 사용될 경우 매분마다 라는 뜻
?
Day of Month, Day of Week에만 사용 가능, 특별한 값이 없다는 뜻
-
기간을 설정, Hour 위치에 10 - 12 라고 쓰면 10, 11, 12dp 동작하라는 뜻
,
특정 시간을 설정. Day of Week 위치에 2, 4, 6 이라고 쓰면 월, 수, 금에만 동작하라는 뜻
/
증가를 표현, Seconds 위치에 0/15로 설정되어 있으면, 0초에 시작해서 15초 간격으로 동작 하라는 뜻
L
Day Of Month 에서만 사용하며, 마지막 날의 의미 Day of Month 에 L로 설정되어 있으면 그달의 마지막날에 실행하라는 뜻
W
Day of Month 에만 사용하며, 가장 가까운 평일을 의미. 15W로 설정되어 있고 15일이 토요일이며, 가장 가까운 평일인 14일 금요일에 실행, 15일이 일요일이면 16일 월요일에 실행된다.
15일이 평일이면 그날 그대로 실행됨
LW
L과 W를 결합하여 사용, 그달의 마지막 평일의 의미
#
Day of Week에 사용, 6#3 의 경우 3번째 주 금요일에 실행된다.

사용 예)

0 0 12 * * * ==> 매일 12시에 실행

0 15 10 * * * ==> 매일 10시 15분에 실행

0 * 14 * * * ==> 매일 14시에 실행

0 0/5 14 18 * * * ==> 매일 14시, 18시에 시작해서 5분간격으로 실행

0 0-5 14 * * * ==> 매일 14시에 시작해서 0분동안 실행

728x90
반응형

댓글