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

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

yuyutoo 2024-10-12 00:05 6 浏览 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、定时任务运行效果图

相关推荐

野路子科技!2步教你把手机改造成一个FTP服务器,支持PC互传

哈喽,大家好,我是野路子科技,今天来给大家带来一个教程,希望大家喜欢。正如标题所言,就是教大家如何把售价改造成FTP服务器,而这个时候估计有朋友会问了,把手机改造成FTP服务器有什么用呢?现在有Q...

不得不看:别样于Server-U的群晖文件存储服务器的搭建与使用

我先前的作品中,有着关于Server-U的ftp文件存储服务器的搭建与访问的头条文章和西瓜视频,而且我们通过各种方式也给各位粉丝介绍了如何突破局域网实现真正意义上的公网访问机制技术。关于Server-...

Qt三种方式实现FTP上传功能_qt引入qftp库

FTP协议FTP的中文名称是“文件传输协议”,是FileTransferProtocol三个英文单词的缩写。FTP协议是TCP/IP协议组中的协议之一,其传输效率非常高,在网络上传输大的文件时,经...

Filezilla文件服务器搭建及客户端的使用

FileZilla是一个免费开源的FTP软件,分为客户端版本和服务器版本,具备所有的FTP软件功能。可控性、有条理的界面和管理多站点的简化方式使得Filezilla客户端版成为一个方便高效的FTP客户...

美能达柯美/震旦复印机FTP扫描怎么设置?

好多网友不知道怎么安装美能达/震旦复印机扫描,用得最多是SMB和FTP扫描,相对于SMB来说,FTP扫描安装步骤更为便捷,不容易出问题,不需要设置文件夹共享,所以小编推荐FTP来扫描以美能达机器为例详...

CCD(简易FTP服务器软件)_简单ftp服务器软件

CCD简易FTP服务器软件是一款很方便的FPT搭建工具,可以将我们的电脑快速变成一个FPT服务器。使用方法非常简单,只要运行软件就会自动生效,下载银行有该资源。该工具是不提供操作界面的,其他用户可以输...

Ubuntu系统搭建FTP服务器教程_ubuntu架设服务器

在Ubuntu系统上搭建FTP服务器是文件传输的一个非常实用方法,适合需要进行大量文件交换的场景。以下是一步步指导,帮助您在Ubuntu上成功搭建FTP服务器。1.安装vsftpd软件...

理光FTP扫描设置教程_理光ftp扫描设置方法

此教程主要用来解决WIN10系统下不能使用SMB文件夹扫描的问题,由于旧的SMB协议存在安全漏洞,所以微软在新的系统,WIN8/WIN10/SERVER201220162018里使用了新的SMB传...

纯小白如何利用wireshark学习网络技术

写在前面工欲善其事必先利其器!熟悉掌握一种神器对以后的工作必然是有帮助的,下面我将从简单的描述Wireshark的使用和自己思考去写,若有错误或不足还请批评指正。...

京东买13盘位32GB内存NAS:NAS系统安装设置教程

本内容来源于@什么值得买APP,观点仅代表作者本人|作者:yasden你没有看错,我在京东自营商城购买硬件,组装了一台13盘位,32GB内存的NAS,硬盘有13个盘位!CPU是AMD的5500!本文...

FileZilla搭建FTP服务器图解教程_filezilla server搭建ftp服务器

...

python教程之FTP相关操作_python ftps

ftplib类库常用相关操作importftplibftp=ftplib.FTP()ftp.set_debuglevel(2)#打开调试级别2,显示详细信息ftp.connect(“I...

xftp怎么用,xftp怎么用,具体使用方法

Xftp是一款界面化的ftp传输工具,用起来方便简单,这里为大家分享下Xftp怎么使用?希望能帮到有需要的朋友。IIS7服务器管理工具可以批量管理、定时上传下载、同步操作、数据备份、到期提醒、自动更新...

树莓派文件上传和下载,详细步骤设置FTP服务器

在本指南中,详细记录了如何在树莓Pi上设置FTP。设置FTP可以在网络上轻松地将文件传输到Pi上。FTP是文件传输协议的缩写,只是一种通过网络在两个设备之间传输文件的方法。还有一种额外的方法,你可以用...

win10电脑操作系统,怎么设置FTP?windows10系统设置FTP操作方法

打印,打印,扫描的日常操作是每一个办公工作人员的必需专业技能,要应用FTP作用扫描文件到电脑上,最先要必须一台可以接受文件的FTP服务器。许多软件都需要收费标准进行,但人们还可以应用Windows的系...

取消回复欢迎 发表评论: