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>

posted @ 2016-09-27 23:37  cac2020  阅读(3005)  评论(0编辑  收藏  举报