SpringQuartz
一、什么是SpringQuartz
- Quartz是启动定时任务的框架,可以帮助我们在特定的时间执行对应的程序,比如定时发送邮件等
二、快速开始
- 引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kuaishou.rao</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
</dependencies>
- 创建抽象类,创建抽象类的作用是可以让所有的定时任务都遵循一个规范
public abstract class AbstractJob extends QuartzJobBean {
public abstract String getCron();
public abstract String getName();
public abstract Class<? extends Job> getJobClass();
public String getGroup() {
return "TEST_GROUP";
}
public abstract Trigger getTrigger() throws ParseException;
public Trigger getDefaultTrigger() throws ParseException {
CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
//cron表达式
triggerFactoryBean.setCronExpression(getCron());
triggerFactoryBean.setName(getName());
triggerFactoryBean.setGroup(getGroup());
triggerFactoryBean.setJobDetail(getJobDetail());
//如果错过了这个执行周期,所有的misFire都不管,执行下一个周期的任务。
triggerFactoryBean.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
//在上述所有的bean都设置成功之后再执行下面的操作
triggerFactoryBean.afterPropertiesSet();
return triggerFactoryBean.getObject();
}
public abstract JobDetail getJobDetail();
public JobDetail getDefaultJobDetail() {
JobDetailFactoryBean detailFactoryBean = new JobDetailFactoryBean();
detailFactoryBean.setJobClass(getJobClass());
detailFactoryBean.setName(getName());
detailFactoryBean.setGroup(getGroup());
//之前的没有执行的任务将在服务重启后补执行
detailFactoryBean.setRequestsRecovery(true);
//把执行结果持久化到数据库
detailFactoryBean.setDurability(true);
detailFactoryBean.afterPropertiesSet();
return detailFactoryBean.getObject();
}
}
- 创建测试类,继承抽象类
@Configuration
public class TestJob extends AbstractJob{
@Override
public String getCron() {
return "1-59 * * * * ? *";
}
@Override
public String getName() {
return "TestJob";
}
@Override
public Class<? extends Job> getJobClass() {
return TestJob.class;
}
@Override
@Bean("TestJobTrigger")
public Trigger getTrigger() throws ParseException {
return getDefaultTrigger();
}
@Override
@Bean("TestJobDetail")
public JobDetail getJobDetail() {
return getDefaultJobDetail();
}
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("hello world");
}
}
三、运行结果
- 启动工程之后,在控制台每秒打印一次hello world
2020-06-05 11:21:24.672 INFO 1464 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
2020-06-05 11:21:24.672 INFO 1464 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler version: 2.3.2
2020-06-05 11:21:24.672 INFO 1464 --- [ main] org.quartz.core.QuartzScheduler : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@5a5c128
2020-06-05 11:21:24.696 INFO 1464 --- [ main] o.s.s.quartz.SchedulerFactoryBean : Starting Quartz Scheduler now
2020-06-05 11:21:24.696 INFO 1464 --- [ main] org.quartz.core.QuartzScheduler : Scheduler quartzScheduler_$_NON_CLUSTERED started.
hello world
2020-06-05 11:21:24.732 INFO 1464 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-06-05 11:21:24.735 INFO 1464 --- [ main] com.kuaishou.rao.demo.DemoApplication : Started DemoApplication in 1.414 seconds (JVM running for 2.062)
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world