스프링에서 쿼츠 잡스케줄링을 하는데 필요한 코드는 대부분 스프링 설정파일에 있다.
애플리케이션에서는 컨텍스트 파일을 로딩하기만 하면 스프링이 설정을 읽어서 스케줄러를 자동으로 실행한다. ^^
[ex: applicationContext_quartz.xml]
<?xml version="1.0" encoding="EUC-KR"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id ="job" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value ="com.bw.scheduler.QuartzJob"></property> <property name="jobDataAsMap"> <map> <entry key="message" value ="this is a message from the Spring config file!!!"></entry> </map> </property> </bean> ※ JobDetailBean 은 <bean> 태그의 id 를 잡 이름으로 사용하고,스케줄러의 기본 그룹을 그룹이름으로 사용한다. JobDataAsMap 속성을 사용하여 JobDataMap에 데이터를 추가 할 수 있다. <bean id ="SimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref ="job"></property> <property name="startDelay" value ="1000"></property> <property name="repeatInterval" value ="3000"></property> <property name="jobDataAsMap"> <map> <entry key="triggerMessage" value ="trigger message from the Spring config file!!!"></entry> </map> </property> </bean>
※ 스프링에서는 SimpleTrigger클래스를 감싼 SimpleTriggerBean 과 CronTrigger클래스를 감싼 CronTriggerBean을 제공하여 설정파일에서 선언적인 방법으로 JobDetailBean 과 연결할 수 있다. <bean id ="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref ="job"></property> <property name="cronExpression" value ="3/5 * 14,15,16,17 * * ?"></property> <property name="jobDataAsMap"> <map> <entry key="triggerMessage" value ="cronTrigger message from the Spring config file!!!"></entry> </map> </property> </bean> <bean id ="schedulerFactory" class ="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers" > <list> <ref local="simpleTrigger"></ref> <ref local="cronTrigger"></ref> </list> </property> </bean> </beans>
|
[ex:스케줄러 실행]
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TimerFactoryBean { public static void main(String[] args){ String[] src = new String[]{"classpath:com/bw/config/applicationContext_quartz.xml"}; ApplicationContext ctx = new ClassPathXmlApplicationContext(src); } }
|