1、 在 pom.xml 中 添加 Quartz 所需要 的 依赖
<!--定时器 quartz-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
注意:
quartz 只能基于 springboot 的 parent 2.0 以上 版本 才能使用过,低于2.0 的版本无法加载到 quartz 的pom依赖包
2、创建要执行的任务类:
TestTask1 与 TestTask2
package com.example.poiutis.quartzHandler;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @ClassName TestTask1
* @Description TODO
* @Author wushaopei
* @Date 2019/7/26 12:28
* @Version 1.0
*/
public class TestTask1 extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("TestQuartz01----" + sdf.format(new Date()));
}
}
package com.example.poiutis.quartzHandler;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @ClassName TestTask2
* @Description TODO
* @Author wushaopei
* @Date 2019/7/26 12:28
* @Version 1.0
*/
public class TestTask2 extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("TestQuartz02----" + sdf.format(new Date()));
}
}
3、定时器(执行类):
package com.example.poiutis.quartzHandler;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName QuartzConfig
* @Description TODO
* @Author wushaopei
* @Date 2019/7/26 12:28
* @Version 1.0
*/
@Configuration
public class QuartzConfig {
private static Logger logger = LoggerFactory.getLogger(QuartzConfig.class);
// testTask1使用的固定间隔方式,testTask2使用的是cron表达式方式。
@Bean
public JobDetail testQuartz1() {
return JobBuilder.newJob(TestTask1.class).withIdentity("testTask1").storeDurably().build();
}
@Bean
public Trigger testQuartzTrigger1() {
//5秒执行一次
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5)
.repeatForever();
logger.info(System.currentTimeMillis()+"");
return TriggerBuilder.newTrigger().forJob(testQuartz1())
.withIdentity("testTask1")
.withSchedule(scheduleBuilder)
.build();
}
@Bean
public JobDetail testQuartz2() {
return JobBuilder.newJob(TestTask2.class).withIdentity("testTask2").storeDurably().build();
}
@Bean
public Trigger testQuartzTrigger2() {
//cron方式,每隔5秒执行一次
logger.info(System.currentTimeMillis()+"");
return TriggerBuilder.newTrigger().forJob(testQuartz2())
.withIdentity("testTask2")
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?"))
.build();
}
}
执行结果: