Spring統合Quartz


Spring統合Quartzの鍵はSpring xmlの構成にある
ApplicationContext.xml
 

  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:tx="http://www.springframework.org/schema/tx" 
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc" 
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
  8.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  9.     http://www.springframework.org/schema/context 
  10.     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
  11.     http://www.springframework.org/schema/tx 
  12.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
  13.     http://www.springframework.org/schema/mvc 
  14.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
  15.      
  16.       
  17.       
  18.     <!-- :java QuartzJobBean, --> 
  19.     <!-- JobDetail,java QuartzJobBean--> 
  20.         <bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean"> 
  21.             <property name="jobClass" value="com.yazuo.study.quartz.JobByQuartz" /> 
  22.             <property name="jobDataAsMap"> 
  23.                 <map> 
  24.                     <entry key="timeout" value="5" /><!--   --> 
  25.                 </map> 
  26.             </property> 
  27.         </bean> 
  28.          
  29.     <!--  simpleTrigger, , -->        
  30.     <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> 
  31.         <property name="jobDetail" ref="exampleJob" /><!--  --> 
  32.         <property name="startDelay" value="1000" /><!-- --> 
  33.         <property name="repeatInterval" value="1000" /><!-- --> 
  34.         <property name="repeatCount" value="10" /><!-- --> 
  35.     </bean>  
  36.          
  37.     <!-- : QuartzJobBean, java  --> 
  38.     <!-- java --> 
  39.     <bean id="exampleBusinessObject" class="com.yazuo.study.quartz.JobOnly"/> 
  40.     <!--MethodInvokingJobDetailFactoryBean,  quartz, java --> 
  41.     <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
  42.         <property name="targetObject" ref="exampleBusinessObject" /> 
  43.         <property name="targetMethod" value="doIt"/><!-- java --> 
  44.         <property name="concurrent" value="false" /><!-- --> 
  45.     </bean>  
  46.      
  47.      
  48. <!--  CronTrigger: ,   <property name="cronExpression" value="0/1 2 13 * * ?" />--> 
  49.     <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
  50.         <property name="jobDetail" ref="jobDetail" /> 
  51.         <!--cron  , readme.txt--> 
  52.         <property name="cronExpression" value="0/5 * * * * ?" /> 
  53.     </bean> 
  54.      
  55.     <!--  SchedulerFactory--> 
  56.     <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" > 
  57.         <property name="triggers"> 
  58.             <list> 
  59.                 <ref bean="cronTrigger" /> 
  60.                 <!--  
  61.                 <ref bean="simpleTrigger"/> 
  62.                 --> 
  63.             </list> 
  64.         </property> 
  65.     </bean> 
  66. </beans> 

 

  
  
  
  
  1. package com.yazuo.study.quartz; 
  2.  
  3. /** 
  4.  *  org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean 
  5.  *  QuartzJobBean, java , applicationContext.xml  doIt()   
  6.  *  doIt() ,  
  7.  * 
  8.  */ 
  9. public class JobOnly { 
  10.  
  11.     public void doIt() { 
  12.         System.out.println("test MethodInvokingJobDetailFactoryBean"); 
  13.     } 
  14.      

5秒ごとにdoIt()メソッドを実行!