scheduler定时调度系统是大多行业项目都需要的,传统的spring-job模式,个人感觉已经out了,因为存在很多的问题,特别是定时调度的追加、修改、删除等,需要修改xml,xml的配置生效无非是热部署灰度发布方案或者直接停止、重启服务器,完全不能做到自动启动、修复方式。
提醒:可以对应用进行集群部署,在对定时调度配置时可以使用集群方式或者单边配置应用方式,今天讲解的是使用spring4+scheduler实现定时调度,闲话少说,直接把步骤记录下来:
1. 在项目的pom.xml文件中引入quartz的jar包,如下:
Java代码
![](https://i-blog.csdnimg.cn/blog_migrate/9020096d3730333ebc37625c8fc38187.webp?x-image-process=image/format,png)
lt;dependency>
org.quartz-scheduler
quartz
1.8.5
lt;/dependency>
2. 定义quartz的配置文件spring-context-quartz.xml:
Java代码
![](https://i-blog.csdnimg.cn/blog_migrate/a810b39bf278aaf64b741ba72e34384f.webp?x-image-process=image/format,png)
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"
default-lazy-init="false">
3. 在项目的web.xml文件中引入spring-context-quartz.xml配置文件
Java代码
![](https://i-blog.csdnimg.cn/blog_migrate/66481a9b0bbbb321c3098257c45b91d3.webp?x-image-process=image/format,png)
4. 定义job实体对象
Java代码
![](https://i-blog.csdnimg.cn/blog_migrate/1d9f6ea8cec8112334d65b22fa227bfa.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/d112c85fc493dd8beacbe98bffa680b3.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/1ba5b012b26dbc763c1e475f2ace16ea.webp?x-image-process=image/format,png)
5. 编写quartz的jobServvice类:
Java代码
![](https://i-blog.csdnimg.cn/blog_migrate/72a2a52c2d04b53c287ca245be6872d9.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/8a0b57dd1057d066ceb73de27bfcad4b.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/6498688c1a699722466dbb8a53329293.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/994362ea8973673388dadf0320994b5e.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/1867d2b2e847f544b975e0e1bc802322.webp?x-image-process=image/format,png)
6. 编写相关job的Controller、dao、dao.xml我这边就不写了,其实就是对数据的增删改查操作
7. 启动项目验证quartz是否成功:
项目启动个控制台:
![](https://i-blog.csdnimg.cn/blog_migrate/f7e9235051a53f93ccd8bfdbd47bdfc6.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/90f6bd0a0e9ccd6c1ed63103dd54a367.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/ef440bc0b5ffdadc75ef0fe8977d399f.webp?x-image-process=image/format,png)
愿意了解框架技术或者源码的朋友直接求求交流分享技术:2042849237
分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨