본문 바로가기

Spring

[Spring] Scheduled

로그분석기를 개편하면서 (회사내 개인 프로젝트)

기존에 cron으로 돌리던 job을 quartz + spring으로 바꾸려고 이것저것 찾아보다보니

spring 3.1 부터는 어노테이션으로 이게 가능해진것을 알았다.


간단히 테스트해본 내용을 올려봅니다.


우선, 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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.tistory.devyongsik"></context:component-scan>
<tx:annotation-driven transaction-manager="transactionManager" />
<task:scheduler id="taskScheduler"/>
<task:executor id="taskExecutor" pool-size="1" />
<task:annotation-driven executor="taskExecutor" scheduler="taskScheduler" />
</beans>
view raw gistfile1.xml hosted with ❤ by GitHub

task에 대한 namespace 선언이 필요하며 

<context:component-scan base-package="com.tistory.devyongsik"></context:component-scan>
<task:scheduler id="taskScheduler"/>
<task:executor id="taskExecutor" pool-size="1" />
<task:annotation-driven executor="taskExecutor" scheduler="taskScheduler" />

설정이 필요합니다.
실제 스케쥴에 의해서 실행될 job 클래스는 아래와 같이 구현합니다.



package com.tistory.devyongsik.analysis.job;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* @author need4spd, need4spd@11st.co.kr, 2013. 1. 10.
*
*/
@Service("searchLogParseExecutor")
public class SearchLogParseExecutor {
@Scheduled(cron="0 26 18 * * * *")
//@Scheduled(fixedDelay=5000)
public void something() {
System.out.println("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
}
}
view raw gistfile1.java hosted with ❤ by GitHub
@Service나 @Component나 spring에서 어노테이션으로 스캔하여 bean으로 등록될 수 있으면 상관은 없는듯하며
@Scheduled 어노테이션으로 cron 형태로 스케쥴을 정할 수 있습니다.
다만 맨 앞이 초입니다. 0 34 18 * * *의 경우 매일 18시 34분 0초에 실행한다는 뜻입니다.

그리고 fixedDelay도 있는데 이건 ms단위로 실행 주기입니다. 위와 같이 500으로 설정할경우 500ms에 한번씩 실행됩니다.