spring实现定时任务

1.建立一个定时任务控制器

/**
 * 描述:定时器
 * 做者:温海金
 * 最后更改时间:下午5:29:49
 */
public class TaskTimerScanner {
    @Resource
    private TaskTimerScannerI taskTimerScanner;
    /**
     * 功能描述:若30分钟没有付款则更改订单表的状态为订单失效 同时将包车状态改成已取消
     */
    public int updateOrderStatus2Invalid() {
           return  taskTimerScanner.updateOrderStatus2Invalid();//调用业务层实现具体的定时业务
    }
}

2.添加一个定时任务配置:spring-tasks.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:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


         
        <!-- Spring定时器注解开关-->  
        <task:annotation-driven />  
        
        <!-- cron表达式:* * * * * *(共6位,使用空格隔开,具体以下) cron表达式:*(秒0-59) *(分钟0-59) *(小时0-23)*(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT) 例如@Scheduled(cron="0/5 * * * * ? ")表示初始化的时候执行一次而后每5秒执行一次-->  
        <!--   <task:scheduled ref="scheduledTaskManager" method="invalidOrderStatus" cron="10/5 * * * * *"/>   -->
        <task:scheduled-tasks scheduler="myScheduler">  
            <!-- 若30分钟没有付款则更改订单表的状态为订单失效 -->
            <task:scheduled ref="myTaskTimerScanner" method="updateOrderStatus2Invalid" cron="0/5 * * * * ? "/> 
        </task:scheduled-tasks>  
        <task:scheduler id="myScheduler" pool-size="10"/>

</beans>

3.在spring的主配置文件中配置一个bean管理定时任务控制类,同时引入spring-tasks.xml

<bean id="myTaskTimerScanner" class="com.lantaiyuan.ebus.custom.scheme.TaskTimerScanner"/>

<!-- 引入spring的定时器 -->
    <import resource="classpath:spring/spring-tasks.xml" />
相关文章
相关标签/搜索