1. 程式人生 > >spring mvc 任務定時器 @Scheduled

spring mvc 任務定時器 @Scheduled

在github看到一遍部落格講述springMVC定時任務,實現如下:

1,在web工程lib中加入以下jar:

com.springsource.org.aopalliance-1.0.0.jar
commons-logging-1.2.jar
spring-aop-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar

 2,建立spring_mvc 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: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/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
"> <context:component-scan base-package="com.a"/> <task:annotation-driven></task:annotation-driven> </beans>
3,web.xml中配置,內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    
  <!--application範圍內的引數,存放在servletcontext中-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring_mvc.xml</param-value>
    </context-param>
      
    <servlet>
        <servlet-name>Task</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--servlet範圍內的引數,只能在servlet的init()方法中取得-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring_mvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>Task</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    
</web-app>
4,測試類,內容如下:
/**
 * Created by ben0101 on 2016-05-15.
 */
@Component
public class TaskTest {

    int a=0;
    @Scheduled(cron="0/5 * * * * ? ")//5秒一次
    public void task(){
        System.out.println(a++);
    }
}
5,result:


附錄:Corn語法知識(from blog)

Cron有如下兩種語法格式: 

Seconds Minutes Hours DayofMonth Month DayofWeek Year或 
Seconds Minutes Hours DayofMonth Month DayofWeek

每一個域可出現的字元如下: 
Seconds:可出現", - * /"四個字元,有效範圍為0-59的整數 
Minutes:可出現", - * /"四個字元,有效範圍為0-59的整數 
Hours:可出現", - * /"四個字元,有效範圍為0-23的整數 
DayofMonth:可出現", - * / ? L W C"八個字元,有效範圍為0-31的整數 
Month:可出現", - * /"四個字元,有效範圍為1-12的整數或JAN-DEc 
DayofWeek:可出現", - * / ? L C #"四個字元,有效範圍為1-7的整數或SUN-SAT兩個範圍。1表示星期天,2表示星期一, 依次類推 
Year:可出現", - * /"四個字元,有效範圍為1970-2099年

每一個域都使用數字,但還可以出現如下特殊字元,它們的含義是: 
(1)*:表示匹配該域的任意值,假如在Minutes域使用*, 即表示每分鐘都會觸發事件。

(2)?:只能用在DayofMonth和DayofWeek兩個域。它也匹配域的任意值,但實際不會。因為DayofMonth和DayofWeek會相互影響。例如想在每月的20日觸發排程,不管20日到底是星期幾,則只能使用如下寫法: 13 13 15 20 * ?, 其中最後一位只能用?,而不能使用*,如果使用*表示不管星期幾都會觸發,實際上並不是這樣。 

(3)-:表示範圍,例如在Minutes域使用5-20,表示從5分到20分鐘每分鐘觸發一次 

(4)/:表示起始時間開始觸發,然後每隔固定時間觸發一次,例如在Minutes域使用5/20,則意味著5分鐘觸發一次,而25,45等分別觸發一次. 

(5),:表示列出列舉值值。例如:在Minutes域使用5,20,則意味著在5和20分每分鐘觸發一次。 

(6)L:表示最後,只能出現在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味著在最後的一個星期四觸發。 

(7)W:表示有效工作日(週一到週五),只能出現在DayofMonth域,系統將在離指定日期的最近的有效工作日觸發事件。例如:在 DayofMonth使用5W,如果5日是星期六,則將在最近的工作日:星期五,即4日觸發。如果5日是星期天,則在6日(週一)觸發;如果5日在星期一到星期五中的一天,則就在5日觸發。另外一點,W的最近尋找不會跨過月份 

(8)LW:這兩個字元可以連用,表示在某個月最後一個工作日,即最後一個星期五。 

(9)#:用於確定每個月第幾個星期幾,只能出現在DayofMonth域。例如在4#2,表示某月的第二個星期三。