基于springboot2.0.1
1.导入依赖
<!-- quartz任务调度依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
2.书写测试任务BaseJob
package com.shuaibiao.controller.base;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class BaseJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println(" Hi! :" + jobExecutionContext.getJobDetail().getKey());
}
}
3.书写quartz配置类
package com.shuaibiao.web.configuration; import com.shuaibiao.controller.base.BaseJob; import org.quartz.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class QuartzConfig { @Bean public JobDetail myJobDetail(){ JobDetail jobDetail = JobBuilder.newJob(BaseJob.class) .withIdentity("myJob1","myJobGroup1") //JobDataMap可以给任务execute传递参数 .usingJobData("job_param","job_param1") .storeDurably() .build(); return jobDetail; } @Bean public Trigger myTrigger(){ Trigger trigger = TriggerBuilder.newTrigger() .forJob(myJobDetail()) .withIdentity("myTrigger1","myTriggerGroup1") .usingJobData("job_trigger_param","job_trigger_param1") .startNow() //.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()) .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ? 2019")) .build(); return trigger; } }
4.application.yml中添加的配置
spring:
quartz:
#相关属性配置
properties:
org:
quartz:
scheduler:
instanceName: clusteredScheduler
instanceId: AUTO
jobStore:
class: org.quartz.impl.jdbcjobstore.JobStoreTX
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
tablePrefix: QRTZ_
isClustered: true
clusterCheckinInterval: 10000
useProperties: false
threadPool:
class: org.quartz.simpl.SimpleThreadPool
threadCount: 10
threadPriority: 5
threadsInheritContextClassLoaderOfInitializingThread: true
#数据库方式
job-store-type: jdbc
#初始化表结构
#jdbc:
#initialize-schema: never
5.根据自身数据库运行quartz文档中的生成语句,生成quartz的11张表,如果忘记生成,启动会报找不到表错误。
6.运行结果
原文:https://www.cnblogs.com/Begodpath/p/10611052.html