一开始自己写的程序有点问题,能运行但是有报错信息后来参考了:http://my.oschina.net/u/559635/blog/389558的文章正常了。
spring定时器目前有两种方式、一种是走配置文件,一种是注解下面来介绍这两种方式。
一、配置文件实现
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
">
<!-- 详细信息 -->
<bean id="methodInvokingJobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject"><ref bean="mainServiceImplTwo"/></property>
<property name="targetMethod"><value>springTrigger</value></property>
<!-- 一个定时器,可能会重复的执行,有可能第一次还没有执行完,第二次就开始了
设置此参数保证第一次定时器,还没有执行完时,不会执行第二次 -->
<property name="concurrent"><value>false</value></property>
</bean>
<!-- 触发器 3.2的实现方式 CronTriggerBean
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property>
<property name="cronExpression">
<value>0 * * * * ?</value>
</property>
</bean>
-->
<!-- 触发器 4.1及以后的实现方式 CronTriggerFactoryBean -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property>
<property name="cronExpression">
<value>0 * * * * ?</value>
</property>
</bean>
<!-- 定时任务列表 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
<value>0 * * * * ?</value> 表示时间配置:时间大小由小到大排列,从秒开始,顺序为 秒,分,时,天,月,年 *为任意 ?为无限制。
marven 引入定时器需要的包(不包含其他spring的包。)
<dependency> <!-- spring 定时器需要 -->
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency> <!-- spring 定时器需要 -->
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency> <!-- spring 定时器需要 -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
具体时间设定可参考
"0/10 * * * * ?" 每10秒触发 "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触发 每隔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 在26分、29分、33分执行一次:0 26,29,33 * * * ? 每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ? 二、注解形式实现
在springmvc.xml里面(根据自己的实际代码改变)
xmlns 多加下面的内容、
xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schemaLocation多加下面的内容、
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
<!-- task:注解定时器使用 这种配置有错误 会报找不到 org.springframework.scheduling.TaskScheduler 这个bean(这其实是一个接口)
<context:annotation-config/> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> -->
<!-- 这种配置是正常的 --->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/> 要放在
<mvc:annotation-driven/>
<context:component-scan base-package="com"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan> 之后
代码实现类实现如下:
@Component
public class UsersTimerImpl implements IUsersTimer{ //更新 支付密码状态 /// 定时器注解调用 @Scheduled(cron = "00 00 00 * * *") public void updatePayStatus(){ //执行逻辑的代码 } }
|
Spring 定时器 xml文件配置
最新推荐文章于 2020-12-11 18:03:02 发布