quartz官方文档链接:https://www.w3cschool.cn/quartz_doc/
代码基本实现#引入依赖
org.springframework.boot spring-boot-starter-quartz
springboot项目记得引入
org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE
#配置文件,命名为quartz.properties
#ID设置为自动获取 每一个必须不同 (所有调度器实例中是唯一的) org.quartz.scheduler.instanceId=AUTO #指定调度程序的主线程是否应该是守护线程 org.quartz.scheduler.makeSchedulerThreadDaemon=true #ThreadPool实现的类名 org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool #ThreadPool配置线程守护进程 org.quartz.threadPool.makeThreadsDaemons=true #线程数量 org.quartz.threadPool.threadCount:20 #线程优先级 org.quartz.threadPool.threadPriority:5 #数据保存方式为持久化 org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX #StdJDBCDelegate说明支持集群 org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate #quartz内部表的前缀 org.quartz.jobStore.tablePrefix=QRTZ_ #是否加入集群 org.quartz.jobStore.isClustered=true #容许的最大作业延长时间 org.quartz.jobStore.misfireThreshold=25000
以下内容转自https://blog.csdn.net/chenmingxu438521/article/details/94485695
#创建job,具体的业务逻辑
public class DateTimeJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//获取JobDetail中关联的数据
String msg = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("msg");
System.out.println("current time :"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "---" + msg);
}
}
#配置类
@Configuration
public class QuartzConfig {
@Bean
public JobDetail printTimeJobDetail(){
return JobBuilder.newJob(DateTimeJob.class)//PrintTimeJob我们的业务类
.withIdentity("DateTimeJob")//可以给该JobDetail起一个id
//每个JobDetail内都有一个Map,包含了关联到这个Job的数据,在Job类中可以通过context获取
.usingJobData("msg", "Hello Quartz")//关联键值对
.storeDurably()//即使没有Trigger关联时,也不需要删除该JobDetail
.build();
}
@Bean
public Trigger printTimeJobTrigger() {
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/1 * * * * ?");
return TriggerBuilder.newTrigger()
.forJob(printTimeJobDetail())//关联上述的JobDetail
.withIdentity("quartzTaskService")//给Trigger起个名字
.withSchedule(cronScheduleBuilder)
.build();
}
}



