Win10系统
JDK版本:11.0.13
SpringBoot版本:2.3.4。RELEASE
Quartz版本:2.3.2
1.创建一个空项目springboot-demo
2.springboot-demo下创建一个模快(maven项目,转为SpringBoot项目)
<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.0modelVersion>
<groupId>org.jinjingroupId>
<artifactId>quartz-demoartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>11maven.compiler.source>
<maven.compiler.target>11maven.compiler.target>
properties>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.4.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-quartzartifactId>
dependency>
dependencies>
project>
package org.jinjin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
* @Package org.jinjin.App
* @Description:SpringBoot启动类
* @author java-忆缘姐姐
* @date 2021/12/22 22:33 星期三
* @version v1.0
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
package org.jinjin.service;
import org.springframework.stereotype.Service;
import java.util.Date;
/*
* @Package org.jinjin.service.DemoService
* @Description:服务类,主要存放定时任务执行的方法
* @author java-忆缘姐姐
* @date 2021/12/22 22:36 星期三
* @version v1.0
*/
@Service
public class DemoService {
/**
* 定时任务执行方法一 (1个定时任务案例 使用的方法 多个定时任务案例 使用的第一个方法)
*/
public void test(){
System.out.println(new Date().toLocaleString());
}
/**
* 定时任务执行方法二 (多个定时任务案例 使用的第二个方法)
*/
public void test1(){
System.out.println("当前时间:"+new Date().toLocaleString());
}
}
package org.jinjin.quartz;
import org.jinjin.service.DemoService;
import org.quartz.CronTrigger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.ParseException;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import java.util.Objects;
/*
* @Package org.jinjin.quartz.QuartzConfigurationOne
* @Description:
* @author java-忆缘姐姐
* @date 2021/12/24 15:18 星期五
* @version v1.0
*/
@Configuration //标注是配置类
public class QuartzConfigurationOne {
/**
* 该方法设置定时执行的任务
*
* TargetObject:指定需要定时执行的那个对象
* targetMethod: 指定需要定时执行的方法
* concurrent:
* false:多个job不会并发执行,设置此次定时完成再执行下一次
* true:多个job并发执行
*
* @param demoService 指定需要定时执行的对象
* @return
*/
@Bean(name = "detailFactoryBean")
public MethodInvokingJobDetailFactoryBean detailFactoryBean(DemoService demoService){
MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
bean.setTargetObject (demoService);
bean.setTargetMethod ("test");
bean.setConcurrent (false);
return bean;
}
/**
* 该方法设置触发器(定时任务的执行方式)
*
* cronExpression:
* 0/5* * * * ? ===> 表示每5秒执行一次,具体可参考附表。
*
* @param detailFactoryBean
* @return
*/
@Bean(name = "cronTriggerBean")
public CronTriggerFactoryBean cronTriggerBean(@Qualifier("detailFactoryBean") MethodInvokingJobDetailFactoryBean detailFactoryBean){
CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
tigger.setJobDetail (Objects.requireNonNull(detailFactoryBean.getObject()));
try {
tigger.setCronExpression("0/5 * * * * ? ");//每5秒执行一次
} catch (ParseException e) {
e.printStackTrace ();
}
return tigger;
}
/**
* 该方法是通过调度工厂执行定时任务
*
* triggers:
* 可放置多个触发器
* @Qualifier:通过名字寻找对应的触发器(因为默认是通过类型找的触发器,这里的多个都是同一个类型)
*
*/
@Bean
public SchedulerFactoryBean schedulerFactory(@Qualifier("cronTriggerBean") CronTrigger cronTriggerBean){
SchedulerFactoryBean bean = new SchedulerFactoryBean ();
bean.setTriggers(cronTriggerBean);
return bean;
}
}
package org.jinjin.quartz;
import org.jinjin.service.DemoService;
import org.quartz.CronTrigger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.ParseException;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import java.util.Objects;
/*
* @Package org.jinjin.quartz.QuartzConfiguration
* @Description:quartz配置类
* @author java-忆缘姐姐
* @date 2021/12/22 22:35 星期三
* @version v1.0
*/
@Configuration
public class QuartzConfiguration {
/**
* 该方法设置定时执行的任务
*
* TargetObject:指定需要定时执行的那个对象
* targetMethod: 指定需要定时执行的方法
* concurrent:
* false:多个job不会并发执行,设置此次定时完成再执行下一次
* true:多个job并发执行
*
* @param demoService 指定需要定时执行的对象
* @return
*/
@Bean(name = "detailFactoryBean")
public MethodInvokingJobDetailFactoryBean detailFactoryBean(DemoService demoService){
MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
bean.setTargetObject (demoService);
bean.setTargetMethod ("test");
bean.setConcurrent (false);
return bean;
}
@Bean(name = "detailFactoryBean1")
public MethodInvokingJobDetailFactoryBean detailFactoryBean1(DemoService demoService){
MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
bean.setTargetObject (demoService);
bean.setTargetMethod ("test1");
bean.setConcurrent (false);
return bean;
}
/**
* 该方法设置触发器(定时任务的执行方式)
*
* cronExpression:
* 0/10 * * * * ? ===> 表示每10秒执行一次,具体可参考附表。
*
* @param detailFactoryBean
* @return
*/
@Bean(name = "cronTriggerBean")
public CronTriggerFactoryBean cronTriggerBean(@Qualifier("detailFactoryBean") MethodInvokingJobDetailFactoryBean detailFactoryBean){
CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
tigger.setJobDetail (Objects.requireNonNull(detailFactoryBean.getObject()));
try {
tigger.setCronExpression("0/10 * * * * ? ");//每10秒执行一次
} catch (ParseException e) {
e.printStackTrace ();
}
return tigger;
}
@Bean(name = "cronTriggerBean1")
public CronTriggerFactoryBean cronTriggerBean1(@Qualifier("detailFactoryBean1")MethodInvokingJobDetailFactoryBean detailFactoryBean1){
CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
tigger.setJobDetail (Objects.requireNonNull(detailFactoryBean1.getObject()));
try {
tigger.setCronExpression("0/5 * * * * ? ");//每10秒执行一次
} catch (ParseException e) {
e.printStackTrace ();
}
return tigger;
}
/**
* 该方法是通过调度工厂执行定时任务
*
* triggers:
* 可放置多个触发器
* @Qualifier:通过名字寻找对应的触发器(因为默认是通过类型找的触发器,这里的多个都是同一个类型)
*
*/
@Bean
public SchedulerFactoryBean schedulerFactory(@Qualifier("cronTriggerBean") CronTrigger cronTriggerBean, @Qualifier("cronTriggerBean1") CronTrigger cronTriggerBean1){
SchedulerFactoryBean bean = new SchedulerFactoryBean ();
bean.setTriggers(cronTriggerBean,cronTriggerBean1);
return bean;
}
}