合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
如果没有将任务进行持久化,当遇到程序版本更新、或机器宕机重启后,那些没有开始、或还没有执行完的任务就没了。所以我们需要把任务持久化到数据库中。 ```xml <!-- 我用的 maven 坐标 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.0.32</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> ``` 步骤如下: **1. 确定自己使用的数据库并建表,我这里用 MySQL** ![](https://img.kancloud.cn/37/ad/37ade834ed6abcd5a753ae59388eab96_1996x432.png) 建表的脚本在`org/quartz/impl/jdbcjobstore/`目录下,我这里用`tables_mysql_innodb.sql`脚本。 ``` # 这个是数据库平台,下面将会用到 # org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate ``` ![](https://img.kancloud.cn/e9/c9/e9c9a1ee8e13b8474edfc3bc10c098b8_1827x299.png) <br/> **2. quartz 相关配置`applicaton.yml`** ```yml spring: quartz: properties: org: quartz: scheduler: #调度器名称,随便一个字符串就行 instanceName: quartz02Cluster #调度器ID。AUTO就是quartz自动生成 instanceId: AUTO threadPool: #线程池类,使用quartz自带的SimpleThreadPool也几乎满足所有用户的需求了 class: org.quartz.simpl.SimpleThreadPool threadCount: 10 threadPriority: 5 jobStore: #将任务存储到数据库中,默认是org.quartz.simpl.RAMJobStore,存储在内存中 class: org.quartz.impl.jdbcjobstore.JobStoreTX #数据库平台,MySQL对应的就是StdJDBCDelegate driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate useProperties: false #是否在集群环境下使用,true是、false不是 isClustered: true #quartz相关表的表名前缀,方便quartz定位到相关的表。可自定义,我这里保持和官方一样的就好 tablePrefix: qrtz_ #数据源别名,可自定义 dataSource: mysql dataSource: #mysql,就是上面的jobStore.dataSource: mysql 设置的别名 mysql: driver: com.mysql.cj.jdbc.Driver URL: jdbc:mysql://127.0.0.1:3306/quartzdb?useUnicode=true&characterEncoding=utf-8&useSSL=false user: root password: uhg</flEt3dff #持久化到数据库 job-store-type: jdbc jdbc: #在项目启动时是否自动建quartz相关表,建议用never,不自动建表。 initialize-schema: never ``` **** 其它配置参考:https://www.w3cschool.cn/quartz_doc/quartz_doc-i7oc2d9l.html <br/> **3. 定时任务要执行的业务代码** ```java @Slf4j public class GoogsJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { JobDataMap dataMap = context.getJobDetail().getJobDataMap(); log.info("name:{}", dataMap.get("name")); } } ``` <br/> **4. 创建定时任务** ```java @RestController public class GoogsJobController { @Autowired private Scheduler scheduler; @GetMapping("/goodsJob1") public void goodsJob1() throws SchedulerException { JobDetail jobDetail = JobBuilder .newJob(GoogsJob.class) .usingJobData("name", "劳斯劳斯") .withIdentity("goods-job1", "job-group1") .build(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("goods-trigger1", "trigger-group1") .startNow() .withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(4) .repeatForever() ) .build(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); } } ``` <br/> **5. 测试:创建任务后,日志输出如下** ``` 2023-04-06T22:17:45 ...job.GoogsJob : name:劳斯劳斯 2023-04-06T22:17:49 ...job.GoogsJob : name:劳斯劳斯 2023-04-06T22:17:53 ...job.GoogsJob : name:劳斯劳斯 ``` <br/> **6. 测试:先关掉 SpringBoot 项目,然后再重启,任务就会自动恢复执行了** ``` 2023-04-06T22:21:17 ...job.GoogsJob : name:劳斯劳斯 2023-04-06T22:21:21 ...job.GoogsJob : name:劳斯劳斯 2023-04-06T22:21:25 ...job.GoogsJob : name:劳斯劳斯 ```