1.学习目标
1.1 定时任务概述
1.2 jdk实现任务调度
1.3 SpringTask实现任务调度
1.4 Spring-Task 分析
1.5 Cron表达式
https://cron.qqe2.com/
2. Quartz 基本应用
2.1 Quartz 基本介绍
2.2 Quartz API介绍
2.3 入门案例
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
2.3.1 新建任务类
2.3.2 执行步骤
package top.psjj.quartzstury;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import top.psjj.quartzstury.job.MyJob;
public class QuartzTest {
public static void main(String[] args) throws SchedulerException {
// 1.创建任务调度器
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
// 2.创建Job 任务类
JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
.withIdentity("job1", "group1")
.build();
// 3.创建触发器,定时任务的执行时机
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
// 使用简单触发器,每3秒执行一次
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).repeatForever())
.startNow()
.build();
// 4.执行任务
scheduler.scheduleJob(jobDetail,trigger);
// 开始执行
scheduler.start();
}
}
2.4 QuartzApi详解
2.4.1 JobDetail
2.4.2 简单触发器
2.4.3 CronTrigger
3. 基于Springboot整合Q uatrz
3.1 Springboot整合Quatrz
<dependencies>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、配置信息详见官网
https://www.quartz-scheduler.org/documentation/quartz-2.3.0/configuration/#configuration-reference
Controller
测试