<?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
html
<?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
java
<?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>
複製代碼
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Task Project!
</body>
</html>
複製代碼
<?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
git
<?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>
複製代碼
將Spring 配置到web.xml,經過Tomcat 部署項目,經過web.xml 加載Spring 配置。
github
<?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>
複製代碼
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 表達式,須要瞭解可參考此連接內容
web
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();
}
}
複製代碼