1.包
•spring-2.0.6.jar Spring框架的核心包
•jta.jar 会用到包中的usertransaction,具体什么用此处暂时不论
•quartz-1.6.0.jar 会用到CronTrigger这个类,通过表达式实现精确的定时
•commons-logging-1.1.1.jar 日志管理包
•commons-collections-3.2.1.jar 集合工具包
2.配置定时器
我写在了ApplicationContext-timer.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置你要定时加载的任务类 ,可以有多个bean-->
<bean id="TimeFirstService" class="com.ecai.service.TimeFirst" />
<!-- 也可用@Service("TimeFirstService")写在类上面代替 -->
<!-- 定时器配置 ,可以有多个bean-->
<bean id="TimeFirstDtail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="TimeFirstService" /><!-- 指定任务类 -->
<property name="targetMethod" value="sfcFirst" /><!-- 指定任务方法 -->
<property name="concurrent" value="false" /> <!-- 不允许并发 -->
<!-- <property name="arguments" /> -->
</bean>
<!-- 定义时间间隔 触发器 ,可以有多个bean-->
<bean id="TimeFirstTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="TimeFirstDtail" />
<property name="cronExpression" value="0 59 23 ? * *" /><!-- "0 0/5 * * * ?" --><!-- "0 0 8 * * ?" -->
</bean>
<!-- 启动定时器 ,可以有多个<ref bean="TimeFirstTrigger" /> -->
<bean id="schedulerFactory"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
<property name="triggers">
<list>
<ref bean="TimeFirstTrigger" />
</list>
</property>
<property name="autoStartup" value="true"/>
</bean>
</beans>
3.在web.xml中添加上面的ApplicationContext-timer.xml文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value
classpath:spring/ApplicationContext-shiro.xml,
classpath:spring/ApplicationContext-redis.xml,
classpath:spring/ApplicationContext-timer.xml
</param-value>
</context-param>
附表:
“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 30 10 ? * 2,4,7”每周一、三、六的上午10:30触发
(每周从星期天为第一天,2表示周一)
“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触发
每隔5秒执行一次:/5 * * * ?
每隔1分钟执行一次:0 /1 * * ?
每天23点执行一次:0 0 23 * * ?
每天凌晨1点执行一次:0 0 1 * * ?
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L