Spring Timer实现
2015-08-03 18:04
288 查看
定时器:继承java.util.TimerTask类实现run方法
spring配置文件:spring-util.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"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 配置需要运行的类 -->
<bean id="reportTimerTask" class="com.zbb.framework.util.timer.TimerTaskExample">
<property name="usOrderService" ref="usOrderService" />
</bean>
<!-- 配置Spring定时器 -->
<!-- timerTask属性告诉ScheduledTimerTask运行哪个bean, period属性每间隔(30秒)运行一次, delay属性延迟启动(10000毫秒)Spring定时器 -->
<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="reportTimerTask"></property>
<property name="period"><value>30000</value></property>
<property name="delay"><value>0</value></property>
</bean>
<!-- 启动Spring定时器 -->
<bean id="scheduleReportFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list><ref bean="scheduleReportTask" /></list>
</property>
</bean>
</beans>
package com.zbb.framework.util.timer; import java.util.TimerTask; import com.zbb.business.user.service.IUsOrderService; /** * * @author Vortex * 继承java.util.TimerTask类实现run方法 * 该类实现spring定时器功能,spring配置文件spring-util.xml * 该定时器功能实现限制用户在规定时间内支付订单,否则取消支付权限 */ public class TimerTaskExample extends TimerTask{ private static boolean isrunning = true; private IUsOrderService usOrderService; @Override public void run() { if(!isrunning){ isrunning = true; /* * 业务实现区 */ try { usOrderService.updateByIds(); } catch (Exception e) { e.printStackTrace(); } isrunning = false; }else{ System.out.println("定时器启动。。。"); isrunning = false; } } public IUsOrderService getUsOrderService() { return usOrderService; } public void setUsOrderService(IUsOrderService usOrderService) { this.usOrderService = usOrderService; } }
spring配置文件:spring-util.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"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 配置需要运行的类 -->
<bean id="reportTimerTask" class="com.zbb.framework.util.timer.TimerTaskExample">
<property name="usOrderService" ref="usOrderService" />
</bean>
<!-- 配置Spring定时器 -->
<!-- timerTask属性告诉ScheduledTimerTask运行哪个bean, period属性每间隔(30秒)运行一次, delay属性延迟启动(10000毫秒)Spring定时器 -->
<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="reportTimerTask"></property>
<property name="period"><value>30000</value></property>
<property name="delay"><value>0</value></property>
</bean>
<!-- 启动Spring定时器 -->
<bean id="scheduleReportFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list><ref bean="scheduleReportTask" /></list>
</property>
</bean>
</beans>
相关文章推荐
- spring对AOP的支持
- struts2 日期类型自动转换类型
- java_web学习(8)会话与状态管
- JAVA 程序 对给定整数60,获取其16进制表现形式
- MyEclipse 快捷键
- 数字在排序数组中出现的次数java实现
- java对cookie的操作
- java转型问题
- java - (04) 域名
- java 自定义动态脚本
- Java 学习笔记--------------做背景图片
- java发送邮件(1)
- 我的第一个Java程序 helloword
- Java内存泄露的理解与解决
- JAVA学习第十三天
- 自定义 Spring Security 4 的UserDetailsService和UserDetails
- java.lang.UnsatisfiedLinkError:Could not load bd_wsp_v1_0 loader dalvik.system
- Java实现“年-月-日 上午/下午时:分:秒”的简单代码
- Eclipse注解模板设置
- java 学习笔记------画图函数的认识