Spring+Quartz 整合一:常規整合


 步驟一: 定時任務需要一個配置文件(spring-mvc-timeTask.xml 隨便起名),將其在web.xml中加載

1 <context-param>
2 <param-name>contextConfigLocation</param-name>
3 <param-value>classpath*:spring-*.xml</param-value>
4 </context-param>

 

步驟二:編寫調度任務配置文件spring-mvc-timeTask.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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
    default-autowire="byName" default-lazy-init="false">

    <!-- 定時任務配置 scheduler 方式 注解 暫時不支持動態更新 
    <context:component-scan base-package="org.jeecgframework.core.timer" />
    <task:executor id="executor" pool-size="5" />
    <task:scheduler id="scheduler" pool-size="10" />
    <task:annotation-driven executor="executor" scheduler="scheduler" />
    -->
    
     <!-- 定時器 -->
    <bean id="msgService" class="com.buss.timer.impl.MsgServiceImpl"/>
    <bean id="sendUAttenceMsgTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="msgService" />
        <property name="targetMethod" value="sendUAttenceMsg" />
        <property name="concurrent" value="true" />
    </bean>
    <!-- 觸發器 -->
    <bean id="sendUAttenceMsgCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="sendUAttenceMsgTaskJob" />
        <property name="cronExpression" value="0 0/1 * * * ?" />
    </bean>
    
    
    <!-- 定時器 -->
    <bean id="sendColoumeMsgTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="msgService" />
        <property name="targetMethod" value="sendColoumeMsg" />
        <property name="concurrent" value="true" />
    </bean>
    <!-- 觸發器 -->
    <bean id="sendColoumeMsgCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="sendColoumeMsgTaskJob" />
        <property name="cronExpression" value="0 0/1 * * * ?" />
    </bean>

    <!-- 調度器 -->
    <bean id="schedulerFactory" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="sendUAttenceMsgCronTrigger" />
                <ref bean="sendColoumeMsgCronTrigger" />
            </list>
        </property>
    </bean>

</beans>

 

 

步驟三:編寫定時任務具體執行類:
兩種方式:
方式一:繼承org.springframework.scheduling.quartz.QuartzJobBean
方式二:是在配置文件里定義任務類和要執行的方法,類和方法可以是普通類。很顯然,使用配置文件的目的就是要用這種方式。

 

注意: 在Spring配置和Quartz集成內容時,有兩點需要注意

1、在<Beans>中不能夠設置default-lazy-init="true",否則定時任務不觸發,如果不明確指明default-lazy-init的值,默認是false。
2、在<Beans>中不能夠設置default-autowire="byName"的屬性,否則后台會報org.springframework.beans.factory.BeanCreationException錯誤,這樣就不能通過Bean名稱自動注入,必須通過明確引用注入
3、spring和quartz的整合對版本是有要求的。
spring3.1以下的版本必須使用quartz1.x系列,3.1以上的版本才支持quartz 2.x,不然會出錯。
至於原因,則是spring對於quartz的支持實現,org.springframework.scheduling.quartz.CronTriggerBean繼承了org.quartz.CronTrigger,在quartz1.x系列中org.quartz.CronTrigger是個類,而在quartz2.x系列中org.quartz.CronTrigger變成了接口,從而造成無法用spring的方式配置quartz的觸發器(trigger)。

<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"default-autowire="byName" default-lazy-init="false">
<!-- 定時任務配置 scheduler 方式 注解 暫時不支持動態更新 <context:component-scan base-package="org.jeecgframework.core.timer" /><task:executor id="executor" pool-size="5" /><task:scheduler id="scheduler" pool-size="10" /><task:annotation-driven executor="executor" scheduler="scheduler" />--> <!-- 定時器 --><bean id="msgService" class="com.buss.timer.impl.MsgServiceImpl"/><bean id="sendUAttenceMsgTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="msgService" /><property name="targetMethod" value="sendUAttenceMsg" /><property name="concurrent" value="true" /></bean><!-- 觸發器 --><bean id="sendUAttenceMsgCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail" ref="sendUAttenceMsgTaskJob" /><property name="cronExpression" value="0 0/1 * * * ?" /></bean><!-- 定時器 --><bean id="sendColoumeMsgTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="msgService" /><property name="targetMethod" value="sendColoumeMsg" /><property name="concurrent" value="true" /></bean><!-- 觸發器 --><bean id="sendColoumeMsgCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail" ref="sendColoumeMsgTaskJob" /><property name="cronExpression" value="0 0/1 * * * ?" /></bean>
<!-- 調度器 --><bean id="schedulerFactory" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="sendUAttenceMsgCronTrigger" /><ref bean="sendColoumeMsgCronTrigger" /></list></property></bean>
</beans>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM