Spring Task

網頁應用程式在使用的 Spring MVC 架構之後,在某些特定條件下,想要執行背景的定時排成工作,應該怎麼設定呢?需要使用到那些功能? spring 本身有定時排成的功能,可以把功能加上啟用 Task。 接下來,就啟用 Spring task 吧!

Spring TasK 環境

  • 先把 Spring 4.0.8 MVC 環境架構好,可以參考 這篇
  • 在設定檔中 dispatcher-servlet.xml 要增加  XSD 的宣告

xmlns:task=”http://www.springframework.org/schema/task”

完整如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
<span style="color: #ff0000;">xmlns:task="http://www.springframework.org/schema/task"</span>
xsi:schemaLocation="<span style="color: #ff0000;">http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd</span> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  
  
  
<context:component-scan base-package="com.tcg.action" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
 p:prefix="/"
 p:suffix=".jsp"/>
  
 <bean id="runScheduler" class="com.tcg.task.MyTask" />
<task:scheduled-tasks>
 <task:scheduled ref="runScheduler" method="run" fixed-delay="5000" />
</task:scheduled-tasks>
  
</beans>

設定為 固定 每五秒執行一次 < task:scheduled ref=”runScheduler” method=”run” fixed-delay=”5000″ >

3. 於 src 中新增 package com.tcg.task 再於此 package 中 新增 class 名稱為 MyTask

package com.tcg.task;
 
public class MyTask {
 
 public void run(){
 System.out.println("MyTask... run..");
  
 }
}

4. 啟動 tomcat

5. 因為目前的 spring 設定的關係  必須先執行其中一個 mvc , 讓 spring 的設定檔生效. 才能看到每五秒執行的結果

6. 若需要tomcat 一啟動  spring task 就要生效  則需要再 web.xml 中  增加 <load-on-startup>1</load-on-startup>

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>SpringTest</display-name>
 <servlet>
 <servlet-name>dispatcher</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>dispatcher</servlet-name>
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>
</web-app>

7. fixed-delay: 距離上一次執行完畢之後  於指定間格的時間再次執行(不會有重疊的情況)

fixed-rate: 距離上一次開始執行  於指定間格的時間再次執行(會有重疊的情況)

cron: 簡易說明
“0 0 12 * * ?”    每天中午十二點觸發
“0 15 10 ? * *”    每天早上10:15觸發
“0 15 10 * * ?”    每天早上10:15觸發
“0 15 10 * * ? *”    每天早上10:15觸發
“0 15 10 * * ? 2005″    2005年的每天早上10:15觸發
“0 * 14 * * ?”    每天從下午2點開始到2點59分每分鐘一次觸發
“0 0/5 14 * * ?”    每天從下午2點開始到2:55分结束每5分鐘一次觸發
“0 0/5 14,18 * * ?”    每天的下午2點至2:55和6點至6點55分

                                 兩個時間内每5分鐘一次觸發
“0 0-5 14 * * ?”    每天14:00至14:05每分鐘一次觸發
“0 10,44 14 ? 3 WED”    三月的每周三的14:10和14:44觸發
“0 15 10 ? * MON-FRI”    每个周一、周二、周三、周四、周五的10:15觸發

/**
   * cron  固定的時間或週期,週期式行為同 fixedRate
   * 固定六個值:秒(059) 分(059) 時(023) 日(131) 月(112) 週(1,7,六)
   * 日與週互斥,其中之一必須為 ?
   * 可使用的值有:單一數值(26)、範圍(5055)、清單(9,10)、不指定(*)與週期(*/ 3)
   */