spring boot项目中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>
<!--声明项目描述符遵循哪一个POM模型版本。模型本身的版本很少改变,虽然如此,但它仍然是必不可少的,这是为了当Maven引入了新的特性或者其他模型变更的时候,确保稳定性。-->
<!--父项目的坐标。如果项目中没有规定某个元素的值,那么父项目中的对应值即为项目的默认值。 坐标包括group ID,artifact ID和 version。-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<!-- 父项目的pom.xml文件的相对路径。相对路径允许你选择一个不同的路径。默认值是../pom.xml。Maven首先在构建当前项目的地方寻找父项目的pom,
其次在文件系统的这个位置(relativePath位置),然后在本地仓库,最后在远程仓库寻找父项目的pom。-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.demo</groupId>
<artifactId>spring-boot-quartz</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--quartz 定时任务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
</dependencies>
</project>
JobInfo类,job的相关信息
package org.demo.dto;
import org.quartz.Job;
public class JobInfo {
/**
* 任务名称
*/
private String jobName;
/**
* 任务组
*/
private String jobGroup;
/**
* 触发器名称
*/
private String triggerName;
/**
* 触发器组
*/
private String triggerGroup;
/**
* cron表达式
*/
private String cron;
/**
* 类名
*/
private String className;
/**
* 状态
*/
private String status;
/**
* 下一次执行时间
*/
private String nextTime;
/**
* 上一次执行时间
*/
private String prevTime;
/**
* 配置信息(data)
*/
private String config;
private Class<? extends Job> clazz;
public String getJobName() {
return jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public String getJobGroup() {
return jobGroup;
}
public void setJobGroup(String jobGroup) {
this.jobGroup = jobGroup;
}
public String getTriggerName() {
return triggerName;
}
public void setTriggerName(String triggerName) {
this.triggerName = triggerName;
}
public String getTriggerGroup() {
return triggerGroup;
}
public void setTriggerGroup(String triggerGroup) {
this.triggerGroup = triggerGroup;
}
public String getCron() {
return cron;
}
public void setCron(String cron) {
this.cron = cron;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getNextTime() {
return nextTime;
}
public void setNextTime(String nextTime) {
this.nextTime = nextTime;
}
public String getPrevTime() {
return prevTime;
}
public void setPrevTime(String prevTime) {
this.prevTime = prevTime;
}
public String getConfig() {
return config;
}
public void setConfig(String config) {
this.config = config;
}
public Class<? extends Job> getClazz() {
return clazz;
}
public void setClazz(Class<? extends Job> clazz) {
this.clazz = clazz;
}
}
QuartzConfig类
package org.demo.config;
import org.demo.MyJob;
import org.demo.dto.JobInfo;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class QuartzConfig {
@Autowired
Scheduler scheduler;
@PostConstruct
public void configJob(){
JobInfo jobInfo = new JobInfo();
jobInfo.setJobName("my job1");
jobInfo.setJobGroup("my job1 group");
jobInfo.setTriggerGroup("my job1 trigger group");
jobInfo.setClazz(MyJob.class);
jobInfo.setTriggerName("my job1 trigger name");
jobInfo.setCron("0/5 * * * * ?");
JobKey jobKey = JobKey.jobKey(jobInfo.getJobName(), jobInfo.getJobGroup());
JobDetail jobDetail = JobBuilder.newJob(jobInfo.getClazz())
.withIdentity(jobKey)
.withIdentity(jobInfo.getJobName(), jobInfo.getJobGroup())
.storeDurably().build();
TriggerKey triggerKey = TriggerKey.triggerKey(jobInfo.getTriggerName(), jobInfo.getTriggerGroup());
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(triggerKey)
.withSchedule(CronScheduleBuilder.cronSchedule(jobInfo.getCron()).withMisfireHandlingInstructionDoNothing())
.build();
try {
scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
MyJob类,具体的job
package org.demo;
import org.demo.service.MyQuartzService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class MyJob extends QuartzJobBean {
@Autowired
MyQuartzService myQuartzService;
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " quartz job running ...");
myQuartzService.test();
}
}
MyQuartzService类
package org.demo.service;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Service
public class MyQuartzService {
public void test(){
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " myQuartzService running ...");
}
}
yml配置
server:
port: 8080
spring:
datasource:
url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
username: system
password: 123456
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
quartz:
auto-startup: true
job-store-type: jdbc
jdbc:
initialize-schema: always
标签:quartz,String,spring,void,boot,import,org,jobInfo,public
From: https://www.cnblogs.com/mmcode/p/17692225.html