本文主要是介绍SpringBoot整合自定义quartz实现任务调度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在不引入quartz相关表的情况下,自定义任务调度存储表,实现SpringBoot项目启动后自动执行自定义任务调度类。
1、建立自定义任务调度存储表
DROP TABLE IF EXISTS `bmd_flow_schedule`;
CREATE TABLE `bmd_flow_schedule` (`taskID` char(20) NOT NULL,`taskName` varchar(32) DEFAULT NULL,`taskTypeID` char(20) DEFAULT NULL,`taskTypeName` varchar(32) DEFAULT NULL,`taskClass` varchar(128) DEFAULT NULL,`exp` varchar(64) DEFAULT NULL,`taskStatus` char(1) DEFAULT NULL,PRIMARY KEY (`taskID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、添加quartz的依赖到pom.xml中
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId></dependency><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz-jobs</artifactId><version>2.3.0</version></dependency>
3、创建任务调度执行类HelloworldJob.java,实现Job接口
package com.example.demo.quartz;import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;import java.util.Date;public class HelloworldJob implements Job {@Overridepublic void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {System.out.println("HelloworldJob start......");System.out.println(new Date()+"----Hello world!:" + jobExecutionContext.getJobDetail().getKey());System.out.println("HelloworldJob end......");}
}
4、在第1步中创建的bmd_flow_schedule表中配置任务调度执行类HelloworldJob.java
INSERT INTO `bmd_flow_schedule` (`taskID`, `taskName`, `taskTypeID`, `taskTypeName`, `taskClass`, `exp`, `taskStatus`) VALUES ('1', 'HelloworldJob', 'VM', '', 'com.example.demo.quartz.HelloworldJob', '0/1 * * * * ? 2019', '1');
5、创建SpringBoot启动后的InitJob方法类,并在InitJob方法中将任务执行类HelloworldJob.java自动加载到quartz中,启动任务。
package com.example.demo.init;import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;/*
启动即执行特定方法*/
@Component
public class InitJob implements ApplicationRunner{@Resourceprivate JdbcTemplate jdbcTemplate;@Override@SuppressWarnings("unchecked")public void run(ApplicationArguments args) throws Exception {System.out.println(new Date());SchedulerFactory schedulerFactory = new StdSchedulerFactory();Scheduler scheduler = schedulerFactory.getScheduler();// 启动schedulerscheduler.start();List list = jdbcTemplate.queryForList("select taskName,taskClass,exp from bmd_flow_schedule where taskStatus='1'");Iterator it = list.iterator(); //这里需要注意的是要用list的iterator遍历if(list.size()>0){while(it.hasNext()){Map<?, ?> rece = (Map<?, ?>) it.next(); //这里要注意,it.next()得到这一条数据System.out.println(rece.get("exp"));//1、HelloWorldJobString taskClass = rece.get("taskClass").toString();String exp = rece.get("exp").toString();String taskName = rece.get("taskName").toString();Class classObj = Class.forName(taskClass);// 创建HelloworldJob的JobDetail实例,并设置name/groupJobDetail jobDetail = JobBuilder.newJob(classObj).withIdentity("jobDetailTaskName",taskName)//JobDataMap可以给任务传递参数.usingJobData("jobDetailTaskName",taskName).build();// 创建Trigger触发器设置使用cronSchedule方式调度Trigger trigger = TriggerBuilder.newTrigger().withIdentity("triggerTaskName",taskName).usingJobData("triggerTaskName",taskName).startNow()//.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).withSchedule(CronScheduleBuilder.cronSchedule(exp)).build();// 注册JobDetail实例到scheduler以及使用对应的Trigger触发时机scheduler.scheduleJob(jobDetail,trigger);}}}
}
6、启动SpringBoot项目,执行任务调度
这篇关于SpringBoot整合自定义quartz实现任务调度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!