spring3.x可以通過<task>標簽輕易的定義定時任務,而且不需要依賴第三方jar包如quartz等,這個是spring自己的實現,但不支持集群。
在Spring3.0中引用了新的命名空間-task:
task:scheduler 用於定義一個ThreadPoolTaskScheduler,並可以指定線程池的大小,
即pool-size.所有任務隊列都將會在指定大小的線程池中運行:
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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd" default-autowire="byName" default-lazy-init="true"> <!-- 配置映射任務執行的類 --> <bean id="myTask" class="com.xsran.core.task.MyTask" /> <task:scheduled-tasks scheduler="myScheduler"> <!-- 每十分鍾執行一次 passStudyTask:任務類中執行的方法 --> <task:scheduled ref="myTask" method="passStudyTask"cron="0 0/10 * * * ?"/> </task:scheduled-tasks> <task:scheduler id="myScheduler" pool-size="10"/> </beans>
java代碼:
package com.xsran.core.task; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.xsran.core.mybase.Constants; import com.xsran.core.myutil.Collections3; import com.xsran.core.myutil.LogUtils; import com.xsran.modules.task.model.QuartzManage; import com.xsran.modules.task.service.QuartzManageService; public class MyTask { @Autowired private QuartzManageService quartzManageService; /** * 定時傳送 */ public void passStudyTask() { // Map<String, String> map = System.getenv(); try { QuartzManage quartzManage = quartzManageService.findQuartzByTaskName("TaskName");//執行方法獲取數據 if (quartzManage != null ) { System.out.println(quartzManage); } } catch (Exception e) { // TODO: handle exception } } }
Spring3.0同樣也使用cron表達式。與Quartz不同的是,Spring3.0不支持年,而Quartz支持年。
cron表達式:-是用空格分開的時間字段,不使用年。
*(秒0-59)
*(分鍾0-59)
*(小時0-23)
*(日期1-31)
*(月份1-12或是JAN-DEC)
*(星期1-7或是SUN-SAT)
示例:
*/5 * * * * 6-7 :: 每個周6到周日,每隔5秒鍾執行一次。
*/1 * * 7-9 1-2 1-7 :: 1月到2月中的7號到9號,且必須要滿足周一到周日,每隔1秒鍾執行一次。
*/1 * * 7-9 1,5 1-7 :: 注意里面的,(逗號),只有1月和5月的7到9號,且必須要滿足周一到周日,每一秒鍾執行一次。
*/1 17-59 * 7-9 1,5 1-7 :: 只解釋17-59,是指從第17分鍾到第59分鍾,在指定的時間內,每一秒種執行一次
* 17-59 * 7-9 1,5 1-7 :: 此代碼的功能與上面完全相同。如果不寫秒即為每一秒執行一次。
59 19-23 * 7-9 1,5 1-7 :: 19分-23分的每59秒鍾時只執行一次。
59 19,26 * 7-9 1,5 1-7 :: 注意里面的,(逗號),是指只有19分或是26分的56秒鍾時執行一次。
* * 16-23 7-9 1,5 1-7 :: 定義每天的16點到23點每一秒鍾執行一次。
59 59 23 * * 1-5 :: 定義每周1到周5,晚上23:59:59秒只執行一次。
這個相當用有。可以工作時間每天給用戶發郵件
注:1、本人blog旨在為自己做代碼筆記,同時供大家作參考;
2、本博文字內容拷自blog:http://xls9577087.iteye.com/blog/2123425