百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

Spring 定时任务Scheduled 开发详细图文

yuyutoo 2024-10-12 00:05 10 浏览 0 评论

文章目录

  • 一、前言
  • 1.1 定时任务
  • 1.2 开发环境
  • 1.3 技术实现
  • 二、创建包含WEB.xml 的Maven 项目
  • 2.1 创建多模块项目taskproject
  • 2.2 配置task-web 子模块Add Web
  • 2.3 配置Tomcat 运行Web 项目
  • 三、定时任务开发
  • 3.1 配置Spring
  • 3.2 编写自动任务类
  • 3.3 运行项目验证定时任务

一、前言

1.1 定时任务

Spring 框架的定时任务是基于Java 基础知识调度任务封装实现的;调度任务的实现方式多样,常见的有Java 本身工具类Timer,ScheduledExecutor 及开源工具Quartz、JCronTab。

1.2 开发环境

IDEA2018 + JDK1.8 + Tomcat8.0

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 模块的依赖,创建定时任务类,通过调用一个或多个业务类去定时执行具体的业务。

本项目源码链接: https://github.com/niaonao/taskproject

二、创建包含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依赖。

2.1.1 父模块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">
 <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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

子模块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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

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>
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9

按如图所示进入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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

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("平台用户资产更新完成!");
 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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();
 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

3.3 运行项目验证定时任务

此时项目结构图如图3-3-1所示。

图3-3-1、多模块项目结构图三

再次通过Tomcat 运行(快捷键Alt + Shift + D)项目,可以在控制台看到定时任务在执行,此处是每间隔10s,打印一次内容。运行效果如图3-3-2 所示.

图3-3-2、定时任务运行效果图

相关推荐

Mysql和Oracle实现序列自增(oracle创建序列的sql)

Mysql和Oracle实现序列自增/*ORACLE设置自增序列oracle本身不支持如mysql的AUTO_INCREMENT自增方式,我们可以用序列加触发器的形式实现,假如有一个表T_WORKM...

关于Oracle数据库12c 新特性总结(oracle数据库19c与12c)

概述今天主要简单介绍一下Oracle12c的一些新特性,仅供参考。参考:http://docs.oracle.com/database/121/NEWFT/chapter12102.htm#NEWFT...

MySQL CREATE TABLE 简单设计模板交流

推荐用MySQL8.0(2018/4/19发布,开发者说同比5.7快2倍)或同类型以上版本....

mysql学习9:创建数据库(mysql5.5创建数据库)

前言:我也是在学习过程中,不对的地方请谅解showdatabases;#查看数据库表createdatabasename...

MySQL面试题-CREATE TABLE AS 与CREATE TABLE LIKE的区别

执行"CREATETABLE新表ASSELECT*FROM原表;"后,新表与原表的字段一致,但主键、索引不会复制到新表,会把原表的表记录复制到新表。...

Nike Dunk High Volt 和 Bright Spruce 预计将于 12 月推出

在街上看到的PandaDunk的超载可能让一些球鞋迷们望而却步,但Dunk的浪潮仍然强劲,看不到尽头。我们看到的很多版本都是为女性和儿童制作的,这种新配色为后者引入了一种令人耳目一新的新选择,而...

美国多功能舰载雷达及美国海军舰载多功能雷达系统技术介绍

多功能雷达AN/SPY-1的特性和技术能力,该雷达已经在美国海军服役了30多年,其修改-AN/SPY-1A、AN/SPY-1B(V)、AN/SPY-1D、AN/SPY-1D(V),以及雷神...

汽车音响怎么玩,安装技术知识(汽车音响怎么玩,安装技术知识视频)

全面分析汽车音响使用或安装技术常识一:主机是大多数人最熟习的音响器材,有关主机的各种性能及规格,也是耳熟能详的事,以下是一些在使用或安装时,比较需要注意的事项:LOUDNESS:几年前的主机,此按...

【推荐】ProAc Response系列扬声器逐个看

有考牌(公认好声音)扬声器之称ProAcTablette小音箱,相信不少音响发烧友都曾经,或者现在依然持有,正当大家逐渐掌握Tablette的摆位设定与器材配搭之后,下一步就会考虑升级至表现更全...

#本站首晒# 漂洋过海来看你 — BLACK&amp;DECKER 百得 BDH2000L无绳吸尘器 开箱

作者:初吻给了烟sco混迹张大妈时日不短了,手没少剁。家里有了汪星人,吸尘器使用频率相当高,偶尔零星打扫用卧式的实在麻烦(汪星人:你这分明是找借口,我掉毛是满屋子都有,铲屎君都是用卧式满屋子吸的,你...

专题|一个品牌一件产品(英国篇)之Quested(罗杰之声)

Quested(罗杰之声)代表产品:Q212FS品牌介绍Quested(罗杰之声)是录音监听领域的传奇品牌,由英国录音师RogerQuested于1985年创立。在成立Quested之前,Roger...

常用半导体中英对照表(建议收藏)(半导体英文术语)

作为一个源自国外的技术,半导体产业涉及许多英文术语。加之从业者很多都有海外经历或习惯于用英文表达相关技术和工艺节点,这就导致许多英文术语翻译成中文后,仍有不少人照应不上或不知如何翻译。为此,我们整理了...

Fyne Audio F502SP 2.5音路低音反射式落地音箱评测

FyneAudio的F500系列,有新成员了!不过,新成员不是新的款式,却是根据原有款式提出特别版。特别版产品在原有型号后标注了SP字样,意思是SpecialProduction。Fyne一共推出...

有哪些免费的内存数据库(In-Memory Database)

以下是一些常见的免费的内存数据库:1.Redis:Redis是一个开源的内存数据库,它支持多种数据结构,如字符串、哈希表、列表、集合和有序集合。Redis提供了快速的读写操作,并且支持持久化数据到磁...

RazorSQL Mac版(SQL数据库查询工具)

RazorSQLMac特别版是一款看似简单实则功能非常出色的SQL数据库查询、编辑、浏览和管理工具。RazorSQLformac特别版可以帮你管理多个数据库,支持主流的30多种数据库,包括Ca...

取消回复欢迎 发表评论: