spring Quartz与Timer定时任务

来源:互联网 发布:心动网络官网地址 编辑:程序博客网 时间:2024/06/07 06:40
spring定时任务
Spring定时任务对Timer与Quartz都提供了支持,并且实现步骤基本一样
(一)配置Spring对Timer的支持
1.1、创建定时任务类


import java.util.TimerTask;


public class WordTask extends TimerTask{


public void run() {
//执行的定时任务
}


}


1.2、在xml文件中注册定时任务类,配置定时任务信息。
在WEB-INF下创建TimerConfig.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


<!-- 定时任务 -->
<bean id="wordTask" class="com.task.WordTask" />

<!-- 定时任务信息 -->
<bean id="wordTaskTimer" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<!-- 设置具体执行的任务 -->
<property name="timerTask" ref="wordTask" />
<!--任务周期-->
<property name="period" value="5000" />
<!-- 延迟多少秒启动任务 -->
<property name="delay" value="1000" />
</bean>


<!-- 启动定时器 -->
<bean id="wordTimerBean" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="wordTaskTimer" />
</list>
</property>
</bean>


</beans>


1.3、web.xml文件中的配置


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/TimerConfig.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>


(二)配置Spring对Quartz的支持
2.1、创建定时任务类




public class WordQuartzTask{


public void execute() {
//执行的定时任务
}


}


2.2、在xml文件中注册定时任务类,配置定时任务信息。
在WEB-INF下创建QuartzConfig.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


<!-- 定时任务 -->
<bean id="wordQuartzTask" class="com.task.WordQuartzTask" />

<!-- 注册定时任务 -->
<bean id="taskInfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 设置具体执行的任务 -->
<property name="targetObject" ref="wordQuartzTask" />
<!--定时任务执行的方法名称-->
<property name="targetMethod" value="execute" />
</bean>

<!-- 定时任务调度信息 -->
<bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 设置具体执行的任务 -->
<property name="jobDetail" ref="taskInfo" />
<!--任务周期-->
<property name="cronExpression" value="0 0 12 * * ?" />
</bean>


<!-- 启动定时器 -->
<bean id="registerQuartz" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="triggers">
<list>
<ref bean="quartzTrigger" />
</list>
</property>
</bean>


</beans>


2.3、web.xml文件中的配置


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/QuartzConfig.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>





3、定时器时间表达式


字段 允许值 允许的特殊字符   
秒 0-59 , - * /   
分 0-59 , - * /   
小时 0-23 , - * /   
日期 1-31 , - * ? / L W C   
月份 1-12 或者 JAN-DEC , - * /   
星期 1-7 或者 SUN-SAT , - * ? / L C #   
年(可选) 留空, 1970-2099 , - * /   
表达式意义   
"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触发   
每天早上6点   
0 6 * * *   
每两个小时   
0 */2 * * *   
晚上11点到早上8点之间每两个小时,早上八点   
0 23-7/2,8 * * *   
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点   
0 11 4 * 1-3   
1月1日早上4点   
0 4 1 1 *   
有些子表达式能包含一些范围或列表  
例如:子表达式(天(星期))可以为 “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”参数时,不要指定列表或范围,因为这会导致问题
0 0
原创粉丝点击