1. 程式人生 > >spring schedule定時任務(一):註解的方式

spring schedule定時任務(一):註解的方式

我所知道的java定時任務的幾種常用方式: 1、spring schedule註解的方式; 2、spring schedule配置檔案的方式; 3、java類繼承TimerTask; 第一種方式的實現: 1、使用maven建立spring專案,schedule在spring-context.jar的包下邊,因此需要匯入與之相關的包;同時,我配的是spring web專案,也同時匯入了spring-web和spring-webmvc的包,如下:
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>

maven導包配置只需要如下就好,其他相關的依賴,maven會自行解決。 2、配置spring專案的基礎檔案spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:task="http://www.springframework.org/schema/task"
    xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">

    <!-- 開啟定時任務 -->
    <task:annotation-driven />
    <!-- 開啟註解 -->
    <context:annotation-config />
    <!-- 指定相關的包路徑 -->
    <context:component-scan base-package="scheduleTest"/>

</beans>

3、編寫java業務程式碼,需要在類宣告上邊新增@Component註解,並在需要定時任務執行的方法宣告上新增@Scheduled(cron = "0/5 * * * * ?")註解以及相關的引數。
package scheduleTest;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * spring定時器1
 * 
 * @author tuzongxun123
 *
 */
@Component
public class ScheduleTest {

    @Scheduled(cron = "0/5 * * * * ?")
    public void schTest1() {
        Date date = new Date();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sim.format(date);
        System.out.println("這是spring定時器1,每五秒執行一次,當前時間:" + dateStr);
    }
}


4、web專案的基礎配置檔案web.xml,:
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>appversion</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <listener>
    <description>spring監聽器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


上邊的配置中使用了spring的上下文監聽器,在這種情況下專案啟動後,spring.xml中的定時任務配置才會生效。但是這不是唯一的方法,也可以使用如下的配置,啟動專案後可以看到一樣的效果:
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>appversion</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring.xml</param-value> 
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

這裡是吧spring的監聽器換成了mvc的排程器,在排程器載入的時候就執行spring.xml中的定時任務配置,因此啟動專案後看到效果一樣。如果把上邊的監聽器和mvc的排程器一起配在這裡,會看到啟動專案後同一時間內這個定時任務需要執行的業務會執行兩次。