spring基於配置applicationContext.xml實現定時任務

來源:互聯網
上載者:User

標籤:nbsp   location   private   not   work   http   str   utils   find   

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

 

spring基於配置applicationContext.xml實現定時任務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.