SPRING

Spring 3.x 스케쥴러

주니얍 2015. 6. 17. 11:52

Spring 3에서는 @Scheduled Annotation을 통한 스케줄링이 가능하다.
다음은 cron-expression을 통한 스케줄링의 예이다.

예 1) TestJob.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.xenomity.batch;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Test Job
*
* @author Xenomity™ a.k.a P-slinc' (이승한)
*/
@Component
public class TestJob {
@Scheduled(cron = "0/10 * * * * ?")
public void run() {
System.out.println("메롱~~~~~~~~~~~~~~~~~~~~");
}
}

Component-scan을 통한 자동 빈 등록을 위해, Job 클래스는 @Component Annotation으로 선언하였다.

예 2) applicationContext.xml

Scheduler 선언은 spring-task namespace에 의해 정의되며, 스케줄러에 대한 Thread Pool 크기도 정의할 수 있다.

예 3) 결과

...

정보: Server startup in 7113 ms
메롱~~~~~~~~~~~~~~~~~~~~
메롱~~~~~~~~~~~~~~~~~~~~



@Scheduled Annotation을 사용하지 않고 xml 설정으로 추출할 경우, 다음과 같은 방법으로 가능하다.
예 4) applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
...
<task:scheduler id="jobScheduler" pool-size="10"/>
<task:scheduled-tasks scheduler="jobScheduler">
<!-- fixed-rate, fixed-delay, cron-expression -->
<task:scheduled ref="testJob" method="run" cron="0/10 * * * * ?"/>
</task:scheduled-tasks>
<!-- job bean -->
<bean id="testJob" class="com.xenomity.batch.TestJob" />
...