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

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

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

相关推荐

电脑 CMD 命令大全:简单粗暴收藏版

电脑CMD命令大全包括了许多常用的命令,这些命令可以帮助用户进行各种系统管理和操作任务。以下是一些常用的CMD命令及其功能:1、系统信息和管理...

电脑维修高手必备!8个神奇DOS命令,自己动手不求人

我相信搞电脑维修或者维护的基本都会些DOS的命令。就算Windows操作系统是可视化的界面,但很多维护检查是离不开DOS命令的。掌握好这些命令,你不仅能快速诊断问题,还能解决90%的常见电脑故障。下...

一个互联网产品总监的设计技巧总结 - 技术篇

古语:工欲善其事必先利其器。往往在利其器后我们才能事半功倍。从这个角度出发成为一个合格的产品经理你需要的是“利其器”,这样你才能产品的设计过程中如鱼得水,得心应手。有些产品经理刚入职,什么都感觉自己欠...

超详解析Flutter渲染引擎|业务想创新,不了解底层原理怎么行?

作者|万红波(远湖)出品|阿里巴巴新零售淘系技术部前言Flutter作为一个跨平台的应用框架,诞生之后,就被高度关注。它通过自绘UI,解决了之前RN和weex方案难以解决的多端一致性...

瑞芯微RK3568|SDK开发之环境安装及编译操作

1.SDK简介一个通用LinuxSDK工程目录包含有buildroot、app、kernel、device、docs、external等目录。其中一些特性芯片如RK3308/RV1108/R...

且看L-MEM ECC如何守护i.MXRT1170从核CM4

大家好,我是痞子衡,是正经搞技术的痞子。今天痞子衡给大家分享的是恩智浦i.MXRT1170上Cortex-M4内核的L-MEMECC功能。本篇是《简析i.MXRT1170Cortex-M7F...

ECC给i.MXRT1170 FlexRAM带来了哪些变化?

大家好,我是痞子衡,是正经搞技术的痞子。今天痞子衡给大家分享的是恩智浦i.MXRT1170上Cortex-M7内核的FlexRAMECC功能。ECC是“ErrorCorrectingCode”...

PHP防火墙代码,防火墙,网站防火墙,WAF防火墙,PHP防火墙大全

PHP防火墙代码,防火墙,网站防火墙,WAF防火墙,PHP防火墙大全资源宝整理分享:https://www.htple.net...

从零开始移植最新版本(2023.10)主线Uboot到Orange Pi 3(全志H6)

本文将从零开始通过一步一步操作来实现将主线U-Boot最新代码移植到OrangePi3(全志H6)开发板上并正常运行起来。本文从通用移植思路的角度,展现是思考的过程,通过这种方式希望能让读者一通百...

可视化编程工具Blockly——定制工具箱

1概述本文重点讲解如何定制Blocklytoolbox上,主要包含如下几点目标:如何为toolbox不同类别添加背景色如何改变选中的类别的外观如何为toolbox类别添加定制化的css如何改变类别...

用户界面干货盘点(用户界面的基本操作方法)

DevExpressDevExpressWPF的DXSplashScreen控件在应用加载的时候显示一个启动界面。添加DXSplashScreen后,会默认生成一个XAML文件,当然,你也可...

Vue3+Bootstrap5整合:企业级后台管理系统实战

简洁而不简单,优雅而不失强大在当今快速发展的企业数字化进程中,高效、美观的后台管理系统已成为企业运营的核心支撑。作为前端开发者,我们如何选择技术栈,才能既保证开发效率,又能打造出专业级的用户体验?答案...

什么?这三款i.MXRT型号也开放了IAP API?

大家好,我是痞子衡,是正经搞技术的痞子。今天痞子衡给大家介绍的是i.MXRT1050/1020/1015系列ROM中的FlexSPI驱动API使用。今天痞子衡去4S店给爱车做保养了,...

OneCode基础组件介绍——表格组件(Grid)

在企业级应用开发中,表格组件是数据展示与交互的核心载体。OneCode平台自研的Grid表格组件,以模型驱动设计...

开源无线LoRa传感器(光照温湿度甲醛Tvoc)

本开源项目基于ShineBlinkC2M低代码单片机实现,无需复杂单片机C语言开发。即使新手也可很容易用FlexLua零门槛开发各种功能丰富稳定可靠的IoT硬件,更多学习教程可参考Flex...

取消回复欢迎 发表评论: