spring mvc 任務定時器 @Scheduled

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

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.jarjava

 

2,建立spring_mvc xml文件,內容以下:web

<?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中配置,內容以下:spring

<?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,測試類,內容以下:express

/**
 * 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:mvc

 

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

Cron有以下兩種語法格式: 測試

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

每個域可出現的字符以下: 
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年spa

每個域都使用數字,但還能夠出現以下特殊字符,它們的含義是: 
(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,表示某月的第二個星期三。

相關文章
相關標籤/搜索