- 创建一个quartz demo,pom依赖如下: <?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.3.RELEASE
com.example
springbootquartz
0.0.1-SNAPSHOT
springbootquartz
Demo project for Spring Boot
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. 代码结构如下: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200910222753815.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zOTU2Mzc2OQ==,size_16,color_FFFFFF,t_70#pic_center) 3. 代码如下: package com.example.springbootquartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
public class MyJob implements Job {
执行任务调度方法
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("quartz MyJob date:" + new Date().getTime());
}
}
package com.example.springbootquartz;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Date;
@SpringBootApplication
public class SpringbootquartzApplication {
public static void main(String[] args) throws SchedulerException{
SpringApplication.run(SpringbootquartzApplication.class, args);
//执行任务调度方法
schedulerApplication();
}
public static void schedulerApplication() throws SchedulerException {
// 1.创建Scheduler的工厂
SchedulerFactory sf = new StdSchedulerFactory();
// 2.从工厂中获取调度器实例
Scheduler scheduler = sf.getScheduler();
// 3.创建JobDetail
JobDetail jb = JobBuilder.newJob(MyJob.class).withDescription("this is a ram job") // job的描述
.withIdentity("ramJob", "ramGroup") // job 的name和group
.build();
// 任务运行的时间,SimpleSchedle类型触发器有效
long time = System.currentTimeMillis() + 3 * 1000L; // 3秒后启动任务
Date statTime = new Date(time);
// 4.创建Trigger
// 使用SimpleScheduleBuilder或者CronScheduleBuilder
Trigger t = TriggerBuilder.newTrigger().withDescription("").withIdentity("ramTrigger", "ramTriggerGroup")
// .withSchedule(SimpleScheduleBuilder.simpleSchedule())
.startAt(statTime) // 默认当前时间启动
.withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?")) // 3秒执行一次
.build();
// 5.注册任务和定时器
scheduler.scheduleJob(jb, t);
// 6.启动 调度器
scheduler.start();
}
}
- 运行效果: