第一种:spring自带的定时任务功能
一)在xml里加入task的命名空间
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
(二)启用注解驱动的定时任务
<task:annotation-driven scheduler="myScheduler"/>
(三)配置定时任务的线程池(多任务时需要进行配置)
配置线程池,若不配置多任务下会有问题。后面会详细说明单线程的问题。
<task:scheduler id="myScheduler" pool-size="5"/>
(四)任务类
@Component("regularTasks")
public class RegularTasks {
@Resource
private ContractManager contractManager;
@Resource
private PayDetailsManager payDetailsManger;
/*@Scheduled(fixedRate = 1000 * 5) //心跳更新。启动时执行一次,之后每五秒执行一次
public void updateContractPayStatus() {
}*/
}
上面是spring的注解方式搞的定时任务。第二种:spring结合quartz配置定时任务
(一)依赖:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
(二)配置<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- Timer schedule --> <!-- 总管理类如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" > <property name="triggers"> <!-- 作业调度器,list下可加入其他的调度器 --> <list> <ref bean="sendMessageTrigger" /> </list> </property> <property name="autoStartup" value="true"/> </bean> <!-- start 轮巡发送信息--> <bean id="sendMessageJobBean" class="com.biz.timeTask.TimeTask"/> <!-- 任务类 --> <bean id="sendMessageJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 调用的类 --> <property name="targetObject" ref="sendMessageJobBean" /> <!-- 调用类中的方法 --> <property name="targetMethod" value="execute" /> <!-- 将并发设置为false --> <property name="concurrent" value="false" /> </bean> <bean id="sendMessageTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="sendMessageJobDetail" /> <property name="cronExpression" value="0 0/10 * * * ?" /><!-- 0 0/60 * * * ? 0 0 12 * * ?--> </bean> <!-- 这个value是写入执行时间表达式的 --> <!-- end 轮巡发送信息--> </beans>
public class TimeTask {
public void execute() {
}
}
定时的表达式详解 ===============================================================================
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
===============================================================================
取值范围
秒(0~59)
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
===============================================================================
表达式 意义
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
“0 0 12 ? * WED”表示每个星期三中午12点
===============================================================================
有些子表达式能包含一些范围或列表
例如:子表达式(天(星期))可以为 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”
-------------------------------------------------------------------------------
“*”字符代表所有可能的值
因此,“*”在子表达式(月)里表示每个月的含义,“*”在子表达式(天(星期))表示星期的每一
天
-------------------------------------------------------------------------------
“/”字符用来指定数值的增量
例如:在子表达式(分钟)里的“0/15”表示从第0分钟开始,每15分钟
在子表达式(分钟)里的“3/20”表示从第3分钟开始,每20分钟(它和“3,23,43”)的含义一样
-------------------------------------------------------------------------------
“?”字符仅被用于天(月)和天(星期)两个子表达式,表示不指定值
当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?”
-------------------------------------------------------------------------------
“L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写
但是它在两个子表达式里的含义是不同的。
在天(月)子表达式中,“L”表示一个月的最后一天
在天(星期)自表达式中,“L”表示一个星期的最后一天,也就是SAT
如果在“L”前有具体的内容,它就具有其他的含义了
例如:“6L”表示这个月的倒数第6天,“FRIL”表示这个月的最一个星期五
注意:在使用“L”参数时,不要指定列表或范围,因为这会导致问题
===============================================================================