Spring Timer Implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.