spring结合quartz的定时的2种方式

本文引自http://gaojiewyh.iteye.com/blog/900446

1、 Spring 的org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean类,使用此方法使开发人员对Quartz完全透明,需要实现定时任务的方法只是一个普通方法。

<?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.5.xsd">  
    <!--   
    方法一:  
    Spring 的org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean类,  
    使用此方法使开发人员对Quartz完全透明,需要实现定时任务的方法只是一个普通方法。  
     -->  
    <!--1、 配置文件中添加业务类,该类为调用的工作类 -->  
    <bean id="businessWork" class="com.morningstar.quartz.BusinessWork" />    
      
    <!-- 2、定义任务,在spring文件中配置代理类 ,定义调用对象和调用对象的方法-->   
    <bean id="businessTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    
        <!-- 调用的类 -->          
        <property name="targetObject" ref="businessWork"/>  
         <!-- 调用类中的方法 -->  
        <property name="targetMethod" value="generateReport"/>   
        <!-- false,证明不执行并发任务 -->  
        <property name="concurrent" value="false"/>     
    </bean>   
    <!-- 3、配置触发器,定义触发时间,可以根据不同的时间对同一个任务定义多个触发器,下面是每隔5秒调用一个方法配置-->  
    <!-- cron表达式 -->  
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
        <property name="jobDetail" ref="businessTask"/>  
        <property name="cronExpression" value="10,15,20,25,30,35,40,45,50,55 * * * * ?"/>  
    </bean>  
  
    <!-- 4、配置调度器 ,容器启动就会执行调度程序  -->  
     <!-- 总管理类,如果lazy-init='false',则容器启动时就会执行调度程序-->    
     <!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序            -->    
    <bean id="schdulerFactory" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>  
                <ref bean="cronTrigger"/>  
            </list>  
        </property>  
    </bean>  
</beans>  

2、借助于Spring的org.springframework.scheduling.quartz.JobDetailBean的类功能, 继承 Spring封装Quartz的org.springframework.scheduling.quartz.QuartzJobBean类,实现 executeInternal方法,executeInternal方法中调用业务类。

<?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.5.xsd">  
    <!--   
    方法二:  
    借助于Spring的org.springframework.scheduling.quartz.JobDetailBean的类功能,  
    继承 Spring封装Quartz的org.springframework.scheduling.quartz.QuartzJobBean类,实现 executeInternal方法,  
    executeInternal方法中调用业务类  
     -->  
    <!-- 1、定义任务,在spring文件中配置代理类 ,定义调用对象和调用对象的方法-->   
    <bean id="businessTask" class="org.springframework.scheduling.quartz.JobDetailBean">    
        <!-- 调用的类 -->          
        <property name="jobClass" value="com.morningstar.quartz.BusinessWork"/>  
        <!-- 主要是为了在定时任务需要使用到Bean,通过Spring给注入进来,如果不写明,就会报java.lang.NullPointerException错误 -->  
        <!--    
        <property name="jobDataAsMap">  
            <map>  
                <entry key="someService">  
                    <ref bean="someService" />  
                </entry>  
            </map>  
        </property>  
        -->  
    </bean>   
      
    <!-- 2、配置触发器,定义触发时间,可以根据不同的时间对同一个任务定义多个触发器-->  
    <!-- cron表达式 -->  
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
        <property name="jobDetail" ref="businessTask"/>  
        <property name="cronExpression" value="10,15,20,25,30,35,40,45,50,55 * * * * ?"/>  
    </bean>  
  
    <!-- 3、配置调度器 ,容器启动就会执行调度程序  -->  
     <!-- 总管理类,如果lazy-init='false',则容器启动时就会执行调度程序-->    
     <!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序            -->    
    <bean id="schdulerFactory" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>  
                <ref bean="cronTrigger"/>  
            </list>  
        </property>  
    </bean>  
</beans>  
原文地址:https://www.cnblogs.com/google4y/p/3346409.html