springboot整合quartz定时任务框架的完整步骤
程序员文章站
2022-03-01 12:48:20
目录spring整合quartzpom文件对应的properties 文件配置类自定义任务类:scheduledtask获取spring中bean的工具类:springcontextutil定时任务服...
spring整合quartz
- 添加maven配置。
- 相关配置文件(不管是properties 还是yml。采用jdbc存储)。
- 创建quartz使用的数据表。
- 业务逻辑层中使用。
- 数据表
- 具体使用
pom文件
如下所示:
<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>
对应的properties 文件
放在resource目录下
# =========================================================================== # configure main scheduler properties 调度器属性 # =========================================================================== #调度标识名 集群中每一个实例都必须使用相同的名称 org.quartz.scheduler.instancename = defaultquartzscheduler #id设置为自动获取 每一个必须不同 org.quartz.scheduler.instanceid = auto #============================================================================ # configure threadpool #============================================================================ #线程池的实现类(一般使用simplethreadpool即可满足几乎所有用户的需求) org.quartz.threadpool.class = org.quartz.simpl.simplethreadpool #指定线程数,至少为1(无默认值)(一般设置为1-100直接的整数合适) org.quartz.threadpool.threadcount = 25 #设置线程的优先级(最大为java.lang.thread.max_priority 10,最小为thread.min_priority 1,默认为5) org.quartz.threadpool.threadpriority = 5 #============================================================================ # configure jobstore #============================================================================ # 触发失败阈值,未超过触发时间60s则直接触发,若超过则失火,log信息handling 1 trigger(s) that missed their scheduled fire-time. org.quartz.jobstore.misfirethreshold = 60000 #数据保存方式为数据库持久化 org.quartz.jobstore.class = org.quartz.impl.jdbcjobstore.jobstoretx #数据库代理类,一般org.quartz.impl.jdbcjobstore.stdjdbcdelegate可以满足大部分数据库 org.quartz.jobstore.driverdelegateclass = org.quartz.impl.jdbcjobstore.stdjdbcdelegate #jobdatamaps是否都为string类型 org.quartz.jobstore.useproperties = false #表的前缀,默认qrtz_ org.quartz.jobstore.tableprefix = qrtz_ #是否加入集群 org.quartz.jobstore.isclustered = true #调度实例失效的检查时间间隔 org.quartz.jobstore.clustercheckininterval = 20000
配置类
package com.anjie.manage.quartz; import org.quartz.scheduler; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.core.io.classpathresource; import org.springframework.scheduling.quartz.schedulerfactorybean; import org.springframework.scheduling.quartz.springbeanjobfactory; import org.springframework.transaction.platformtransactionmanager; import javax.sql.datasource; import java.io.ioexception; @configuration public class quartzconfig { @autowired private datasource datasource; @autowired private platformtransactionmanager txmanager; @bean(name = "quartzscheduler") public schedulerfactorybean quartzscheduler() throws ioexception { //创建schedulerfactorybean schedulerfactorybean factory = new schedulerfactorybean(); //设置调度器自动运行 factory.setautostartup(true); //设置配置文件位置 factory.setconfiglocation(new classpathresource("/quartz.properties")); //设置job工厂,使job可以自动注入 springbeanjobfactory jobfactory = new springbeanjobfactory(); factory.setjobfactory(jobfactory); //设置数据源 factory.setdatasource(datasource); //设置事务 factory.settransactionmanager(txmanager); //设置重写已存在的job factory.setoverwriteexistingjobs(true); return factory; } @bean(name = "scheduler") public scheduler scheduler() throws ioexception { return quartzscheduler().getscheduler(); } }
自定义任务类:scheduledtask
package com.anjie.manage.quartz; import com.anjie.auth.common.exception.badconfigurationexception; import com.anjie.manage.common.springcontextutil; import lombok.setter; import lombok.extern.slf4j.slf4j; import org.apache.commons.lang3.stringutils; import org.quartz.job; import org.quartz.jobexecutioncontext; import org.springframework.context.applicationcontext; import java.lang.reflect.method; @slf4j @setter public class scheduledtask implements job { private integer id; private string params; private string servicebeanname; private string methodname; @override public void execute(jobexecutioncontext jobexecutioncontext) { try { //使用反射执行spring中的任务 applicationcontext wac = springcontextutil.getapplicationcontext(); class<?> servicebean = wac.getbean(servicebeanname).getclass(); if (stringutils.isnotempty(params)){ method method = servicebean.getdeclaredmethod(methodname,string.class); method.invoke(wac.getbean(servicebean),params); }else { method method = servicebean.getdeclaredmethod(methodname,null); method.invoke(wac.getbean(servicebean),null); } }catch (exception e){ throw new badconfigurationexception("任务执行失败"); } } }
获取spring中bean的工具类:springcontextutil
package com.anjie.manage.common; import org.springframework.beans.beansexception; import org.springframework.context.applicationcontext; import org.springframework.context.applicationcontextaware; import org.springframework.stereotype.component; @component public class springcontextutil implements applicationcontextaware { private static applicationcontext applicationcontext = null; @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { springcontextutil.applicationcontext = applicationcontext; } /** * 获取applicationcontext * @return */ public static applicationcontext getapplicationcontext() { return applicationcontext; } }
定时任务服务接口:quartzservice
package com.anjie.manage.quartz.service; import com.anjie.manage.quartz.entity.po.quartztask; import java.util.list; public interface quartzservice { void initalltask(list<quartztask> scheduledtaskbeanlist) throws exception; void addjob(quartztask scheduledtask); void updatejob(quartztask quartztask); void deletejob(quartztask quartztask); void runjobonce(quartztask quartztask); }
quartzservice实现类:quartzserviceimpl
package com.anjie.manage.quartz.service.impl; import com.anjie.manage.quartz.entity.po.quartztask; import com.anjie.manage.quartz.scheduledtask; import com.anjie.manage.quartz.service.quartzservice; import lombok.extern.slf4j.slf4j; import org.quartz.*; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.springframework.util.collectionutils; import java.util.*; @service @slf4j public class quartzserviceimpl implements quartzservice { @autowired private scheduler scheduler; @override public void initalltask(list<quartztask> quartztasklist) throws exception { log.info("程序启动 ==> 初始化所有任务开始 !"); if (collectionutils.isempty(quartztasklist)) { scheduler.shutdown(); return; } for (quartztask quartztask : quartztasklist) { //判断是否启动状态 if (quartztask.getstatus()) { this.addjob(quartztask); } } log.info("程序启动 ==> 初始化所有任务结束 !"); } @override public void addjob(quartztask quartztask) { string taskname = quartztask.gettaskname(); string groupname = quartztask.getgroupname(); string cron = quartztask.getcron(); triggerkey triggerkey = triggerkey.triggerkey(taskname, groupname); try { boolean result = scheduler.checkexists(triggerkey); //job已存在,直接返回 log.info("checkexists quarttask = {} , result = {}", quartztask, result); if (result) { return; } job job = new scheduledtask(); //构建job信息 jobdetail jobdetail = jobbuilder.newjob(job.getclass()).withidentity(taskname, groupname).build(); //表达式调度构建器(即任务执行的时间) /*使用withmisfirehandlinginstructiondonothing是因为重启项目时之前添加的job都算失火 ,默认配置withmisfirehandlinginstructionfireandproceed失火后会立即执行一遍, 而withmisfirehandlinginstructiondonothing失火后不会立即执行,而是按照下一个cron执行 */ cronschedulebuilder schedulebuilder = cronschedulebuilder.cronschedule(cron).withmisfirehandlinginstructiondonothing(); //按新的cronexpression表达式构建一个新的trigger crontrigger trigger = triggerbuilder.newtrigger().startnow().withidentity(taskname, groupname).withschedule(schedulebuilder).build(); string[] servicebean = quartztask.getjobclass().split("\\."); //获得jobdatamap,写入数据 map<string, object> parammap = new hashmap<>(); parammap.put("id", quartztask.getid()); parammap.put("params", quartztask.getparams()); parammap.put("methodname",servicebean[1]); parammap.put("servicebeanname",servicebean[0]); trigger.getjobdatamap().putall(parammap); scheduler.schedulejob(jobdetail, trigger); log.info("addjob quarttask = {} is success", quartztask); } catch (exception e) { log.error("addjob quarttask = {} is fail, msg = {}", quartztask, e); } } @override public void updatejob(quartztask quartztask) { string taskname = quartztask.gettaskname(); string groupname = quartztask.getgroupname(); string cron = quartztask.getcron(); triggerkey triggerkey = triggerkey.triggerkey(taskname, groupname); try { crontrigger trigger = (crontrigger) scheduler.gettrigger(triggerkey); if (cron != null) { // 表达式调度构建器 cronschedulebuilder schedulebuilder = cronschedulebuilder.cronschedule(cron).withmisfirehandlinginstructiondonothing(); // 按新的cronexpression表达式重新构建trigger trigger = trigger.gettriggerbuilder().startnow().withidentity(triggerkey).withschedule(schedulebuilder).build(); } string[] servicebean = quartztask.getjobclass().split("\\."); //获得jobdatamap,写入数据 map<string, object> parammap = new hashmap<>(); parammap.put("id", quartztask.getid()); parammap.put("params", quartztask.getparams()); parammap.put("servicebeanname",servicebean[0]); parammap.put("methodname",servicebean[1]); trigger.getjobdatamap().putall(parammap); // 按新的trigger重新设置job执行 scheduler.reschedulejob(triggerkey, trigger); log.info("updatejob quarttask = {} is success", quartztask); } catch (schedulerexception e) { log.error("updatejob quarttask = {} is fail, msg = {}", quartztask, e); } } @override public void deletejob(quartztask quartztask) { string taskname = quartztask.gettaskname(); string groupname = quartztask.getgroupname(); try { scheduler.pausetrigger(triggerkey.triggerkey(taskname, groupname)); scheduler.unschedulejob(triggerkey.triggerkey(taskname, groupname)); scheduler.deletejob(jobkey.jobkey(taskname, groupname)); log.info("deletejob quarttask = {} is success", quartztask); } catch (schedulerexception e) { log.error("deletejob quarttask = {} is fail, msg = {}", quartztask, e); } } @override public void runjobonce(quartztask quartztask){ string taskname = uuid.randomuuid().tostring(); string groupname = uuid.randomuuid().tostring(); try { // class<?> aclass = class.forname(quartztask.getjobclass()); job job = new scheduledtask(); //构建job信息 jobdetail jobdetail = jobbuilder.newjob(job.getclass()).withidentity(taskname, groupname).build(); simpletrigger simpletrigger = triggerbuilder.newtrigger() .withidentity(taskname, groupname) .startat(new date()) .withschedule(simpleschedulebuilder.simpleschedule() .withintervalinseconds(1) .withrepeatcount(0))//重复执行的次数,因为加入任务的时候马上执行了,所以不需要重复,否则会多一次。 .build(); string[] servicebean = quartztask.getjobclass().split("\\."); //获得jobdatamap,写入数据 map<string, object> parammap = new hashmap<>(); parammap.put("id", quartztask.getid()); parammap.put("params", quartztask.getparams()); parammap.put("servicebeanname",servicebean[0]); parammap.put("methodname",servicebean[1]); simpletrigger.getjobdatamap().putall(parammap); scheduler.schedulejob(jobdetail, simpletrigger); }catch (exception e){ log.error("run job quarttask = {} is fail, msg = {}", quartztask, e); } } }
scheduledtaskrunner类
项目启动后获取数据库中的定时任务列表,初始化定时任务
package com.anjie.manage.quartz; import com.anjie.manage.quartz.entity.po.quartztask; import com.anjie.manage.quartz.service.quartzservice; import com.anjie.manage.quartz.service.quartztaskservice; import lombok.extern.slf4j.slf4j; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.applicationarguments; import org.springframework.boot.applicationrunner; import org.springframework.core.annotation.order; import org.springframework.stereotype.component; import java.util.list; @component @order(value = 1) @slf4j public class scheduledtaskrunner implements applicationrunner { @autowired private quartztaskservice quartztaskservice; @autowired private quartzservice quartzservice; @override public void run(applicationarguments args) throws exception { log.info(" >>>>>> 项目启动完毕, 开启 => 需要自启的任务 开始!"); list<quartztask> activatedtasklist = quartztaskservice.list(); quartzservice.initalltask(activatedtasklist); log.info(" >>>>>> 项目启动完毕, 开启 => 需要自启的任务 结束!"); } }
任务实体类:quartztask
映射数据库自定义任务表
package com.anjie.manage.quartz.entity.po; import com.baomidou.mybatisplus.annotation.idtype; import com.baomidou.mybatisplus.annotation.tableid; import com.baomidou.mybatisplus.annotation.tablename; import lombok.data; @data @tablename(value = "quartz_task") public class quartztask { /** * 主键 */ @tableid(type = idtype.auto) private integer id; /** * 任务名称 */ private string taskname; /** * group名称 */ private string groupname; /** * job所在类 */ private string jobclass; /** * 参数 */ private string params; /** * cron表达式 */ private string cron; /** * 状态 */ private boolean status; /** * 任务描述 */ private string des; }
任务dao层,我是用的是mybatis-plus
package com.anjie.manage.quartz.dao; import com.anjie.manage.quartz.entity.po.quartztask; import com.baomidou.mybatisplus.core.mapper.basemapper; import org.apache.ibatis.annotations.mapper; import org.springframework.stereotype.repository; @repository @mapper public interface quartztaskdao extends basemapper<quartztask> { }
任务service层
package com.anjie.manage.quartz.service; import com.anjie.manage.quartz.entity.po.quartztask; import com.baomidou.mybatisplus.extension.service.iservice; public interface quartztaskservice extends iservice<quartztask> { }
service实现类
package com.anjie.manage.quartz.service.impl; import com.anjie.manage.quartz.entity.po.quartztask; import com.anjie.manage.quartz.dao.quartztaskdao; import com.anjie.manage.quartz.service.quartztaskservice; import com.baomidou.mybatisplus.extension.service.impl.serviceimpl; import org.springframework.stereotype.service; @service public class quartztaskserviceimpl extends serviceimpl<quartztaskdao, quartztask> implements quartztaskservice { }
任务controller
package com.anjie.manage.quartz.controller; import com.anjie.manage.common.responsemessage; import com.anjie.manage.quartz.service.quartzservice; import com.anjie.manage.quartz.entity.po.quartztask; import com.anjie.manage.quartz.service.quartztaskservice; import com.baomidou.mybatisplus.core.conditions.query.querywrapper; import com.baomidou.mybatisplus.core.metadata.ipage; import com.baomidou.mybatisplus.extension.plugins.pagination.page; import lombok.extern.slf4j.slf4j; import org.apache.commons.lang3.stringutils; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.*; @restcontroller @slf4j @requestmapping("/quartz") public class jobcontroller { @autowired private quartzservice quartzservice; @autowired private quartztaskservice quartztaskservice; @postmapping("/job") public responsemessage addjob(@requestbody quartztask quartztask){ querywrapper<quartztask> querywrapper = new querywrapper<>(); querywrapper.eq("task_name",quartztask.gettaskname()); quartztask queryresult = quartztaskservice.getone(querywrapper); if (queryresult != null){ return responsemessage.fail("任务名称已存在!"); } quartztaskservice.save(quartztask); return responsemessage.ok("操作成功!"); } @putmapping("/job") public responsemessage updatejob(@requestbody quartztask quartztask){ //判断任务是否启用 boolean status = quartztaskservice.getbyid(quartztask.getid()).getstatus(); if (status){ return responsemessage.fail("任务正在运行中,请暂停后修改!"); } //判断任务名称是否重名 querywrapper<quartztask> querywrapper = new querywrapper<>(); querywrapper.eq("task_name",quartztask.gettaskname()); quartztask queryresult = quartztaskservice.getone(querywrapper); if (queryresult != null &&!queryresult.getid().equals(quartztask.getid())){ return responsemessage.fail("任务名称已存在!"); } quartztaskservice.updatebyid(quartztask); return responsemessage.ok("操作成功!"); } @deletemapping("/job/{id}") public responsemessage deletejob(@pathvariable integer id){ //判断任务是否启用 boolean status = quartztaskservice.getbyid(id).getstatus(); if (status){ return responsemessage.fail("任务正在运行中,请暂停后删除!"); } quartztaskservice.removebyid(id); return responsemessage.ok("操作成功!"); } @getmapping("/job") public responsemessage getjobpage(page<quartztask> page,quartztask params){ querywrapper<quartztask> querywrapper = new querywrapper<>(); if (stringutils.isnotempty(params.getdes())){ querywrapper.like("des",params.getdes()); } if (stringutils.isnotempty(params.gettaskname())){ querywrapper.like("task_name",params.gettaskname()); } if (stringutils.isnotempty(params.getgroupname())){ querywrapper.like("group_name",params.getgroupname()); } if (params.getstatus() != null){ querywrapper.eq("status",params.getstatus()); } querywrapper.orderbydesc("id"); ipage<quartztask> result = quartztaskservice.page(page,querywrapper); return responsemessage.ok("操作成功",result); } /** * 修改任务的cron * * @return */ // @requestmapping(value = "/updatejobcron",method = requestmethod.put) // public object updatejobcron(integer id, string cron) { // quartztask quartztask = quartztaskservice.getbyid(id); // map<string, string> resultmap = new hashmap<>(); // //如果存在quartztask对象 // if (quartztask != null) { // //修改任务的cron // quartztask.setcron(cron); // //更新quart_task // quartztaskservice.updatebyid(quartztask); // //如果启用状态则修改当前已持久化的job // if (quartztask.getstatus()) { // quartzservice.updatejob(quartztask); // } // resultmap.put("status", "0"); // resultmap.put("msg", "修改cron成功"); // return resultmap; // } // //不存在quartztask对象 // resultmap.put("status", "1"); // resultmap.put("msg", "修改cron失败"); // return resultmap; // } /** * 是否启用接口 * * @return */ @putmapping(value = "/jobstatus") public responsemessage updatejobstatus(@requestbody quartztask params) { quartztask quartztask = quartztaskservice.getbyid(params.getid()); if (quartztask != null) { //修改任务的启用状态 quartztask.setstatus(params.getstatus()); //更新quart_task boolean updateresult = quartztaskservice.updatebyid(quartztask); //根据status判断是新增job还是删除job if (params.getstatus() && updateresult) { quartzservice.addjob(quartztask); } else if (updateresult){ quartzservice.deletejob(quartztask); } return responsemessage.ok("修改状态成功"); } return responsemessage.fail("修改状态失败"); } /** * 启用一次任务 * @author ** * @param id 任务id * @return java.lang.object */ @postmapping("/runjob/{id}") public responsemessage runjobonce(@pathvariable integer id){ quartztask quartztask = quartztaskservice.getbyid(id); if (quartztask != null) { quartzservice.runjobonce(quartztask); return responsemessage.ok("任务执行成功"); } return responsemessage.fail("任务执行失败"); } }
到目前位置就对quartz框架整合完成,这里附上具体使用到的数据表
数据表
-- ---------------------------- -- table structure for qrtz_blob_triggers -- ---------------------------- drop table if exists `qrtz_blob_triggers`; create table `qrtz_blob_triggers` ( `sched_name` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_group` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `blob_data` blob null, primary key (`sched_name`, `trigger_name`, `trigger_group`) using btree, index `sched_name`(`sched_name`, `trigger_name`, `trigger_group`) using btree, constraint `qrtz_blob_triggers_ibfk_1` foreign key (`sched_name`, `trigger_name`, `trigger_group`) references `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`) on delete restrict on update restrict ) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic; -- ---------------------------- -- table structure for qrtz_calendars -- ---------------------------- drop table if exists `qrtz_calendars`; create table `qrtz_calendars` ( `sched_name` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `calendar_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `calendar` blob not null, primary key (`sched_name`, `calendar_name`) using btree ) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic; -- ---------------------------- -- table structure for qrtz_cron_triggers -- ---------------------------- drop table if exists `qrtz_cron_triggers`; create table `qrtz_cron_triggers` ( `sched_name` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_group` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `cron_expression` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `time_zone_id` varchar(80) character set utf8mb4 collate utf8mb4_unicode_ci null default null, primary key (`sched_name`, `trigger_name`, `trigger_group`) using btree, constraint `qrtz_cron_triggers_ibfk_1` foreign key (`sched_name`, `trigger_name`, `trigger_group`) references `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`) on delete restrict on update restrict ) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic; -- ---------------------------- -- table structure for qrtz_fired_triggers -- ---------------------------- drop table if exists `qrtz_fired_triggers`; create table `qrtz_fired_triggers` ( `sched_name` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `entry_id` varchar(95) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_group` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `instance_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `fired_time` bigint(13) not null, `sched_time` bigint(13) not null, `priority` int(11) not null, `state` varchar(16) character set utf8mb4 collate utf8mb4_unicode_ci not null, `job_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci null default null, `job_group` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci null default null, `is_nonconcurrent` varchar(1) character set utf8mb4 collate utf8mb4_unicode_ci null default null, `requests_recovery` varchar(1) character set utf8mb4 collate utf8mb4_unicode_ci null default null, primary key (`sched_name`, `entry_id`) using btree, index `idx_qrtz_ft_trig_inst_name`(`sched_name`, `instance_name`) using btree, index `idx_qrtz_ft_inst_job_req_rcvry`(`sched_name`, `instance_name`, `requests_recovery`) using btree, index `idx_qrtz_ft_j_g`(`sched_name`, `job_name`, `job_group`) using btree, index `idx_qrtz_ft_jg`(`sched_name`, `job_group`) using btree, index `idx_qrtz_ft_t_g`(`sched_name`, `trigger_name`, `trigger_group`) using btree, index `idx_qrtz_ft_tg`(`sched_name`, `trigger_group`) using btree ) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic; -- ---------------------------- -- table structure for qrtz_job_details -- ---------------------------- drop table if exists `qrtz_job_details`; create table `qrtz_job_details` ( `sched_name` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `job_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `job_group` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `description` varchar(250) character set utf8mb4 collate utf8mb4_unicode_ci null default null, `job_class_name` varchar(250) character set utf8mb4 collate utf8mb4_unicode_ci not null, `is_durable` varchar(1) character set utf8mb4 collate utf8mb4_unicode_ci not null, `is_nonconcurrent` varchar(1) character set utf8mb4 collate utf8mb4_unicode_ci not null, `is_update_data` varchar(1) character set utf8mb4 collate utf8mb4_unicode_ci not null, `requests_recovery` varchar(1) character set utf8mb4 collate utf8mb4_unicode_ci not null, `job_data` blob null, primary key (`sched_name`, `job_name`, `job_group`) using btree, index `idx_qrtz_j_req_recovery`(`sched_name`, `requests_recovery`) using btree, index `idx_qrtz_j_grp`(`sched_name`, `job_group`) using btree ) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic; -- ---------------------------- -- table structure for qrtz_locks -- ---------------------------- drop table if exists `qrtz_locks`; create table `qrtz_locks` ( `sched_name` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `lock_name` varchar(40) character set utf8mb4 collate utf8mb4_unicode_ci not null, primary key (`sched_name`, `lock_name`) using btree ) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic; -- ---------------------------- -- table structure for qrtz_paused_trigger_grps -- ---------------------------- drop table if exists `qrtz_paused_trigger_grps`; create table `qrtz_paused_trigger_grps` ( `sched_name` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_group` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, primary key (`sched_name`, `trigger_group`) using btree ) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic; -- ---------------------------- -- table structure for qrtz_scheduler_state -- ---------------------------- drop table if exists `qrtz_scheduler_state`; create table `qrtz_scheduler_state` ( `sched_name` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `instance_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `last_checkin_time` bigint(13) not null, `checkin_interval` bigint(13) not null, primary key (`sched_name`, `instance_name`) using btree ) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic; -- ---------------------------- -- table structure for qrtz_simple_triggers -- ---------------------------- drop table if exists `qrtz_simple_triggers`; create table `qrtz_simple_triggers` ( `sched_name` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_group` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `repeat_count` bigint(7) not null, `repeat_interval` bigint(12) not null, `times_triggered` bigint(10) not null, primary key (`sched_name`, `trigger_name`, `trigger_group`) using btree, constraint `qrtz_simple_triggers_ibfk_1` foreign key (`sched_name`, `trigger_name`, `trigger_group`) references `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`) on delete restrict on update restrict ) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic; -- ---------------------------- -- table structure for qrtz_simprop_triggers -- ---------------------------- drop table if exists `qrtz_simprop_triggers`; create table `qrtz_simprop_triggers` ( `sched_name` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_group` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `str_prop_1` varchar(512) character set utf8mb4 collate utf8mb4_unicode_ci null default null, `str_prop_2` varchar(512) character set utf8mb4 collate utf8mb4_unicode_ci null default null, `str_prop_3` varchar(512) character set utf8mb4 collate utf8mb4_unicode_ci null default null, `int_prop_1` int(11) null default null, `int_prop_2` int(11) null default null, `long_prop_1` bigint(20) null default null, `long_prop_2` bigint(20) null default null, `dec_prop_1` decimal(13, 4) null default null, `dec_prop_2` decimal(13, 4) null default null, `bool_prop_1` varchar(1) character set utf8mb4 collate utf8mb4_unicode_ci null default null, `bool_prop_2` varchar(1) character set utf8mb4 collate utf8mb4_unicode_ci null default null, primary key (`sched_name`, `trigger_name`, `trigger_group`) using btree, constraint `qrtz_simprop_triggers_ibfk_1` foreign key (`sched_name`, `trigger_name`, `trigger_group`) references `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`) on delete restrict on update restrict ) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic; -- ---------------------------- -- table structure for qrtz_triggers -- ---------------------------- drop table if exists `qrtz_triggers`; create table `qrtz_triggers` ( `sched_name` varchar(120) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_group` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `job_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `job_group` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci not null, `description` varchar(250) character set utf8mb4 collate utf8mb4_unicode_ci null default null, `next_fire_time` bigint(13) null default null, `prev_fire_time` bigint(13) null default null, `priority` int(11) null default null, `trigger_state` varchar(16) character set utf8mb4 collate utf8mb4_unicode_ci not null, `trigger_type` varchar(8) character set utf8mb4 collate utf8mb4_unicode_ci not null, `start_time` bigint(13) not null, `end_time` bigint(13) null default null, `calendar_name` varchar(200) character set utf8mb4 collate utf8mb4_unicode_ci null default null, `misfire_instr` smallint(2) null default null, `job_data` blob null, primary key (`sched_name`, `trigger_name`, `trigger_group`) using btree, index `idx_qrtz_t_j`(`sched_name`, `job_name`, `job_group`) using btree, index `idx_qrtz_t_jg`(`sched_name`, `job_group`) using btree, index `idx_qrtz_t_c`(`sched_name`, `calendar_name`) using btree, index `idx_qrtz_t_g`(`sched_name`, `trigger_group`) using btree, index `idx_qrtz_t_state`(`sched_name`, `trigger_state`) using btree, index `idx_qrtz_t_n_state`(`sched_name`, `trigger_name`, `trigger_group`, `trigger_state`) using btree, index `idx_qrtz_t_n_g_state`(`sched_name`, `trigger_group`, `trigger_state`) using btree, index `idx_qrtz_t_next_fire_time`(`sched_name`, `next_fire_time`) using btree, index `idx_qrtz_t_nft_st`(`sched_name`, `trigger_state`, `next_fire_time`) using btree, index `idx_qrtz_t_nft_misfire`(`sched_name`, `misfire_instr`, `next_fire_time`) using btree, index `idx_qrtz_t_nft_st_misfire`(`sched_name`, `misfire_instr`, `next_fire_time`, `trigger_state`) using btree, index `idx_qrtz_t_nft_st_misfire_grp`(`sched_name`, `misfire_instr`, `next_fire_time`, `trigger_group`, `trigger_state`) using btree, constraint `qrtz_triggers_ibfk_1` foreign key (`sched_name`, `job_name`, `job_group`) references `qrtz_job_details` (`sched_name`, `job_name`, `job_group`) on delete restrict on update restrict ) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic; -- ---------------------------- -- table structure for quartz_task -- ---------------------------- drop table if exists `quartz_task`; create table `quartz_task` ( `id` int(11) not null auto_increment comment '主键', `task_name` varchar(255) character set utf8mb4 collate utf8mb4_unicode_ci null default null comment '任务名称', `group_name` varchar(255) character set utf8mb4 collate utf8mb4_unicode_ci null default null comment 'group名称', `job_class` varchar(255) character set utf8mb4 collate utf8mb4_unicode_ci null default null comment 'job所在类', `params` varchar(255) character set utf8mb4 collate utf8mb4_unicode_ci null default null comment '参数', `cron` varchar(255) character set utf8mb4 collate utf8mb4_unicode_ci null default null comment 'cron表达式', `status` tinyint(1) null default 0 comment '状态', `des` varchar(500) character set utf8mb4 collate utf8mb4_unicode_ci null default null comment '描述', primary key (`id`) using btree ) engine = innodb auto_increment = 16 character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic;
具体使用
1、新建具体需要执行任务
package com.anjie.manage.quartz; import com.anjie.manage.quartz.service.quartztaskservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; @service public class task01 { @autowired private quartztaskservice quartztaskservice; public void test(string param){ quartztaskservice.list(); system.out.println("test01被执行(任务示例)"+param); } }
2、配置定时任务
注意:
1.这里的任务路径是需要执行的类中的方法路径,采用bean的名称 + 方法名(例:task01.test),名称首字母小写,不然会找不到bean,也可以自己给需要执行的任务类bean起名
2.参数是字符串,可以自己写json字符串,是否传参与方法中保持一致,方法中有形参,这里就必传,方法没有形参,这里就不传,不然任务会执行失败
具体效果
总结
到此这篇关于springboot整合quartz定时任务框架的文章就介绍到这了,更多相关springboot整合quartz定时任务内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!