前几天做SpringMVC的定时任务,在网上查找时,发现大部分说明的不是很清楚,特此记录一下我个人的做法,我的这个定时任务是基于Jeecg的,所以如果有用Jeecg开发的,借鉴起来会更轻松
对于spring的定时任务分为两种,一种是间隔执行的,一种是周期执行的,每一中都是遵循三个步骤完成的:1.定义任务2.任务执行配置3.启动任务
一、间隔执行
<bean id="smsSendTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="smsSendTask" /><!--执行函数所在的类-->
<property name="targetMethod" value="run" /><!--执行函数-->
<property name="concurrent" value="true" /><!--是否并发执行 true并发执行 false不并发执行-->
</bean>
<!-- 调度时间设置-->
<bean id="smsSendTaskCronTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"><!--注意:周期执行与间隔执行所加载的class是不同的-->
<property name="jobDetail">
<ref bean="smsSendTask" /> <!--执行函数所在的类-->
</property>
<property name="startDelay" value="60000"></property><!-- 延时启动时间,单位ms 也可以:时*分*秒*1000-->
<property name="repeatInterval" value="60000"></property><!-- 重复间隔时间,单位ms 也可以:时*分*秒*1000-->
</bean>
<!-- 定时任务调度器 -->
<bean id="schedulerFactory" lazy-init="false" autowire="no"
class="org.jeecgframework.core.timer.DataBaseSchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="smsSendTaskCronTrigger" /><!--调度时间的id-->
</list>
</property>
</bean>
二、周期执行
<!-- 定时任务配置 -->
<bean id="smsSendTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="smsSendTask" /><!--执行函数所在的类-->
<property name="targetMethod" value="run" /><!--执行函数-->
<property name="concurrent" value="true" /><!--是否并发执行 true并发执行 false不并发执行-->
</bean>
<bean id="smsSendTaskCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><!--注意:周期执行与间隔执行所加载的class是不同的-->
<property name="jobDetail" ref="smsSendTask" /><!--执行函数所在的类-->
<property name="cronExpression">
<value>0 00 10 ? * *</value><!--每天上午10点执行-->
</property>
</bean>
<!-- 定时任务调度器(启动任务)-->
<bean id="schedulerFactory" lazy-init="false" autowire="no"
class="org.jeecgframework.core.timer.DataBaseSchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="smsSendTaskCronTrigger" /><!--调度时间的id-->
</list>
</property>
</bean>
三、附上Quartz表达式详解
四、另附Jeecg中的timeTask
1.周期任务
<?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" />
<!-- 定时任务配置 smsSendTask 可配置到管理界面 -->
<bean id="smsSendTaskJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="smsSendTask" />
<property name="targetMethod" value="run" />
<property name="concurrent" value="true" />
</bean>
<bean id="smsSendTaskCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="smsSendTaskJob" />
<!-- <property name="cronExpression" value="0 23 10 * * ? *"></property> -->
<property name="cronExpression">
<value>0 00 10 ? * *</value>
</property>
</bean>
<!-- 定时任务调度器 -->
<bean id="schedulerFactory" lazy-init="false" autowire="no"
class="org.jeecgframework.core.timer.DataBaseSchedulerFactoryBean">
<property name="triggers">
<list>
<!-- <ref bean="taskDemoServiceTaskCronTrigger" /> -->
<ref bean="smsSendTaskCronTrigger" />
</list>
</property>
</bean>
</beans>
2.间隔任务
<?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" />
<!-- 定时任务配置 smsSendTask 可配置到管理界面 -->
<bean id="smsSendTaskJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="smsSendTask" />
<property name="targetMethod" value="run" />
<property name="concurrent" value="true" />
</bean>
<bean id="smsSendTaskCronTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="smsSendTaskJob" />
<property name="repeatInterval" value="60000"></property>
</bean>
<!-- 定时任务调度器 -->
<bean id="schedulerFactory" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="smsSendTaskCronTrigger" />
</list>
</property>
</bean>
</beans>