Spring施用Quartz配置调度事务

Spring使用Quartz配置调度事务

首先编写服务类:(需要调度的类)

package QuartzTest;

import java.util.Date;

public class CourseService {
   public void start(){
       System.out.println(new Date().getSeconds());
   }
}

 

 

编写调度类,需要继承QuartzJobBean :

package QuartzTest;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;


public class QuartzJob extends QuartzJobBean{    
    
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException{
        courseService.start();
    }

    private CourseService courseService; //使用注入方式获取对象

    public CourseService getCourseService(){
        return courseService;
    }

    public void setCourseService(CourseService courseService){
        this.courseService = courseService;
    }
    
}

 

编写配置文件

需要说明的是,我们有两种trigger,分别是simple和cron模式,simple方式和timertask类似,采用设置interval方式进行调度,而cron可以特有的语法很详细的定制调度执行时间,具体描述在配置文件的注释中

<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd">

   <bean id="courseService" class="QuartzTest.CourseService"/>
   <!-- 创建调度任务 使用单独编写的调度类QuartzJob    -->
   <bean id="reportJbo" class="org.springframework.scheduling.quartz.JobDetailBean">
     <property name="jobClass">
       <value>QuartzTest.QuartzJob</value>
     </property>
     <property name="jobDataAsMap">
       <map>
         <!--采用jobDataAsMap方式进行courseService注入-->
         <entry key="courseService">
           <ref bean="courseService"/>
          </entry>
       </map>
     </property>
   </bean>

   <!-- 创建调度任务 使用已有的service类方法,不需要单独编写调度类QuartzJob 
   <bean id="reportJbo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     //或者这么写<property name="targetObject" ref="courseService"/>
     <property name="targetObject">
      <ref bean="courseService"/>
     </property>
     //或者这么写<property name="targetMethod" value="start"/>
     <property name="targetMethod">
        <value>start</value>
     </property>
      //concurrent:对于相同的JobDetail,当指定多个Trigger时, 很可能第一个job完成之前,第二个job就开始了。指定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始。
      <property name="concurrent" value="false"/>
    </bean>
    -->
   <!-- 配置调度任务,简单模式   
   <bean id="simpleReportTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
      <property name="jobDetail">
        <ref bean="reportJbo"/>
      </property>
      <property name="repeatInterval">
        <value>1000</value>
      </property>
   </bean>
   -->
 
   <!-- 配置调度任务,复杂定制模式,月份中的日期和星期不能同时设置   -->
   <bean id="cronReportTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
      <property name="jobDetail">
        <ref bean="reportJbo"/>
      </property>
      <property name="cronExpression">
        <value>02 20 21 7 6 ? 2007</value> 
        <!-- 1.秒 0-59
             2.分钟 0-59
             3.小时 0-23
             4.月份中的日期 1-31
             5.月份 1-12或者Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec
             6.星期中的日期 1-7或者MON,TUE,WED,THU,FRI,SAT,SUN.
        -->
      </property>
   </bean>
 
   <!-- 启动调度 -->
   <bean id="start" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
     <property name="triggers">
       <list>
         <ref bean="cronReportTrigger"/>
       </list>
     </property>
   </bean>
</beans>

 

 

Spring还为我们提供了更简单的加载调度的方式,也就说我们在已经有业务方法CourseService时不需要再额外编写调度类QuartzJob,可以直接配置service的方法

 <bean id="reportJbo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     <property name="targetObject">
      <ref bean="courseService"/>
     </property>
     <property name="targetMethod">
        <value>start</value>
     </property>
   </bean>
配置文件中注释部分。

 

spring quartz 需要的包有:spring.jar   quartz-all-1.6.0.jar   log4j-1.2.14.jar   commons-collections.jar   jta.jar   commons-logging.jar