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" />