Timers: Inheriting the Java.util.TimerTask class to implement the Run method
Package Com.zbb.framework.util.timer;import Java.util.timertask;import com.zbb.business.user.service.iusorderservice;/** * * @author Vortex * Inherit Java.util.TimerTask class implementation Run method * This class implements the spring timer function, Spring config file Spring-util.xml * This timer feature allows the user to pay the order within the specified time, otherwise cancel the payment permission */public class Timertaskexample extends Timertask{private static Boolean isrunning = True;private iusorderservice usorderservice; @Overridepublic void Run () {
if (!isrunning) { isrunning = true; / * Business Implementation area */try { usorderservice.updatebyids ();} catch (Exception e) {e.printstacktrace ();} IsRunning = false; } else{ System.out.println ("Timer start ... "); IsRunning = false; }} Public Iusorderservice Getusorderservice () {return usorderservice;} public void Setusorderservice (Iusorderservice usorderservice) {this.usorderservice = Usorderservice;}}
Spring configuration file: 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 ">
<!--Configure the classes that need to run-
<bean id= "Reporttimertask" class= "Com.zbb.framework.util.timer.TimerTaskExample" >
<property name= "Usorderservice" ref= "Usorderservice"/>
</bean>
<!--Configure Spring Timers--
<!--TimerTask Property tells Scheduledtimertask which Bean to run, period property runs once per interval (30 seconds), delay property delayed start (10000 ms) Spring Timer--
<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>
<!--start Spring Timer--
<bean id= "Schedulereportfactory" class= "Org.springframework.scheduling.timer.TimerFactoryBean" >
<property name= "Scheduledtimertasks" >
<list><ref bean= "Schedulereporttask"/></list>
</property>
</bean>
</beans>
Spring Timer Implementation