Spring 定時任務Scheduled 開發詳細圖文
一、前言
1.1 定時任務
Spring 框架的定時任務是基於Java 基礎知識排程任務封裝實現的;排程任務的實現方式多樣,常見的有Java 本身工具類 ofollow,noindex">Timer,ScheduledExecutor 及開源工具Quartz、JCronTab。
1.2 開發環境
1.3 技術實現
一般專案業務複雜或模組較多時會採用多模組建立專案,便於業務管理與開發。此處採用多模組形式(為了方便可以不用多模組),建立Maven 父專案taskproject,建立Maven 子模組task-service 作為普通模組,建立Maven 子模組task-web 作為web 模組。
task-web 模組做web 處理,建立task-web/src/main/webapp/WEB-INF/web.xml。在task-web/src/main/resources 下新建Spring 配置檔案。配置spring-context.xml 到web.xml。
task-service 模組下建立業務類,即要定時執行的任務。task-web 模組引入task-service 模組的依賴,建立定時任務類,通過呼叫一個或多個業務類去定時執行具體的業務。
本專案原始碼連結: github.com/niaonao/tas…
二、建立包含WEB.xml 的Maven 專案
2.1 建立多模組專案taskproject
使用IDEA 建立Maven 專案taskproject,父模組taskproject 不編寫的程式碼,此處刪除父模組的src 資料夾。在taskproject 下依此建立Maven 子模組專案普通模組task-service 和Web 模組task-web。
圖2-1-1、IDEA 建立Maven Project 圖
GroupId 和ArtifactId 座標填寫。
圖2-1-2、自定義taskproject 專案座標圖
刪除父模組的src 資料夾。
圖2-1-3、刪除父模組src 資料夾圖
建立兩個module 子模組task-service 和task-web。
圖2-1-4、建立子模組Module 圖
圖2-1-5、建立子模組task-service 圖
圖2-1-6、建立子模組task-web 圖
此時專案結構圖下
圖2-1-7、多模組專案結構圖一
父模組pom.xml 引入子模組依賴、Spring 相關依賴及Web依賴。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>pers.niaonao</groupId> <artifactId>taskproject</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <!--自動新增子模組--> <modules> <module>task-service</module> <module>task-web</module> </modules> <!--自定義屬性,用作版本控制--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <treasureFinal.version>1.0.0</treasureFinal.version> <junit.version>4.11</junit.version> <spring.version>4.3.3.RELEASE</spring.version> <spring.remoting.version>2.0.8</spring.remoting.version> <useragent.version>1.20</useragent.version> <aspect.version>1.8.9</aspect.version> </properties> <!--依賴新增--> <dependencies> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-remoting</artifactId> <version>${spring.remoting.version}</version> </dependency> <dependency> <groupId>eu.bitwalker</groupId> <artifactId>UserAgentUtils</artifactId> <version>${useragent.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspect.version}</version> </dependency> </dependencies> </project> 複製程式碼
子模組pom.xml 如下,其中task-web 引入了task-service 模組的依賴,這裡會呼叫task-service 模組的業務類。
2.1.2 子模組task-service 的pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--自動引入父模組--> <parent> <artifactId>taskproject</artifactId> <groupId>pers.niaonao</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>task-service</artifactId> </project> 複製程式碼
2.1.3 子模組task-web 的pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>taskproject</artifactId> <groupId>pers.niaonao</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <!-- 設定打包方式為war --> <packaging>war</packaging> <!--引入要依賴的模組task-service --> <dependencies> <dependency> <groupId>pers.niaonao</groupId> <artifactId>task-service</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> <artifactId>task-web</artifactId> </project> 複製程式碼
2.2 配置task-web 子模組Add Web
在task-web 模組右鍵選擇功能選單Open Module Settings 進行設定,在taskweb 模組新增Add Web
圖2-2-1、taskweb 子模組Open Module Settings 圖
圖2-2-2、taskweb 子模組Add Web 圖
配置Web 資原始檔夾Web Resource Directories,如下圖2-2-3 所示,編輯Web Resource Directory,配置Web resource directory Path,在task-web 子模組的src/main 下新建webapp 資料夾webapp 作為Web 資源目錄。
圖2-2-3、配置taskweb 子模組Resource Directories 圖
配置Web 部署檔案Deployment Descriptors,如下圖2-2-4 所示,選中Project Structure 下的Facets 配置Web(taskweb)。編輯Type 為Web Module Deployment Descriptor 的path。在Web Module Deployment Descriptor(web.xml) 的taskproject/task-web/src/main/webapp 路徑下新建WEB-INF,選中該路徑後,在路徑後新增\web.xml,此處通過嚮導建立Web 部署檔案web.xml
圖2-2-4、配置taskweb 子模組Deployment Descriptors 圖
圖2-2-5、配置taskweb 子模組web.xml 圖
依次點選Apply OK 即可。
2.3 配置Tomcat 執行Web 專案
在task-web 模組下的webapp 下新建index.jsp 檔案
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> Task Project! </body> </html> 複製程式碼
按如圖所示進入Run/Debug Configurations,新增Tomcat Server,選擇Local,在Application Server 選擇本地解壓縮的tomcat 的路徑即可。
圖2-3-1、IDEA 進入Edit Configurations 圖
圖2-3-2、配置Tomcat Server 圖
Tomcat Server 的Name 可以自定義,此處沒做修改,預設為Unnamed。Application Server 點選Configure 選擇配置本地的tomcat 即可。此時有個警告Warning:No artifacts configured 下面會進行artifacts 處理。
圖2-3-3、配置Application Server 圖
雙擊IDEA 工作區右側Maven Projects,選擇task-web 下的package 雙擊打包(task-web 的pom.xml 檔案已設定打包方式為war),生成war 包,如圖2-3-5 所示。
圖2-3-4、task-web 打包圖
圖2-3-5、task-web.war 圖
標記部署的檔案,解決上一步的警告。
圖2-3-6、標記部署檔案 圖
圖2-3-7、解決artifacts 警告圖
依此點選Apply OK 應用即可,選中配置的tomcat(Unnamed)執行專案,執行專案的快捷鍵Ctrl + Alt + D/X。然後訪問localhost:8080,此時能夠訪問到index.jsp。
圖2-3-8、Run/Debug 執行圖
三、定時任務開發
3.1 配置Spring
在子模組task-web 的src/main/resources 資源路徑下新建spring 資料夾,新建Spring 配置檔案spring-config.xml,定時任務配置檔案spring-task.xml。
3.1.1 spring-config.xml
下面配置中service 掃描包(pers.niaonao.taskservice,pers.niaonao.taskweb),後面編寫Java 類時建立。
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd "> <!--service註解掃描 --> <context:component-scan base-package="pers.niaonao.taskservice,pers.niaonao.taskweb"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 引入SPRING配置檔案--> <import resource="classpath:spring/spring-task.xml"/> </beans> 複製程式碼
3.1.2 spring-task.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:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <!-- 定時任務掃描器 --> <task:executor id="executor" pool-size="5"/> <task:scheduler id="scheduler" pool-size="5"/> <task:annotation-driven executor="executor" scheduler="scheduler"/> </beans> 複製程式碼
3.1.3 web.xml
將Spring 配置到web.xml,通過Tomcat 部署專案,通過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_4_0.xsd" version="4.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止spring記憶體溢位監聽器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> </web-app> 複製程式碼
3.2 編寫自動任務類
在task-service 子模組的src/main/java 下建立package 包pers.niaonao.taskservice,建立使用者資產任務類UserAssetTask.java,模擬要執行的任務。
在task-web 子模組的src/main/java 下建立package 包pers.niaonao.taskweb,建立自動任務類AutoTask.java,引入使用者資產任務,呼叫執行。
3.2.1 UserAssetTask.java
package pers.niaonao.taskservice; import org.springframework.stereotype.Service; /** * @Description :使用者資產任務 * @Author: niaonao * @Date: 2018/9/21 15:20 */ @Service(value = "userAssetTask") public class UserAssetTask { /** * 平臺使用者資產更新任務,模擬一個要執行的任務 */ public void platformUserAssetUpdate() { // 此處可呼叫你的service、util、entity 等,編寫你的定時任務具體的業務程式碼 System.out.println("平臺使用者資產更新完成!"); } } 複製程式碼
3.2.2 AutoTask.java
此處設定定時任務週期為10 S執行一次。通過註解@Scheduled(cron = "0/10 * * * * ? ")實現。 此處不介紹Cron 表示式,需要了解可參考此連結內容
package pers.niaonao.taskweb; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import pers.niaonao.taskservice.UserAssetTask; import javax.annotation.Resource; /** * @Description :定時器 * @Author: niaonao * @Date: 2018/9/21 15:17 */ @Component public class AutoTask { @Resource private UserAssetTask userAssetTask; /** * 使用者資產定時更新任務 * 10s 更新一次 */ @Scheduled(cron = "0/10 * ** * ? ") public void platformUserAssetUpdate() { userAssetTask.platformUserAssetUpdate(); } } 複製程式碼
3.3 執行專案驗證定時任務
此時專案結構圖如圖3-3-1所示。
圖3-3-1、多模組專案結構圖三
再次通過Tomcat 執行(快捷鍵Alt + Shift + D)專案,可以在控制檯看到定時任務在執行,此處是每間隔10s,列印一次內容。執行效果如圖3-3-2 所示.
圖3-3-2、定時任務執行效果圖