如下所示:
<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>
放在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 misd 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.uproperties = fal#表的前缀,默认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;@configurationpublic class quartzconfig { @autowired private datasource datasource; @autowired private platformtransactionmanager txmanager; @bean(name = "quartzscheduler") public schedulerfactorybean quartzscheduler() throws ioexception { //创建schedulerfactorybean schedulerfactorybean factory = new schedulerfactorybean(); //设置调度器自动运行 factory.tautostartup(true); //设置配置文件位置 factory.tconfiglocation(new classpathresource("/quartz.properties")); //设置job工厂,使job可以自动注入 springbeanjobfactory jobfactory = new springbeanjobfactory(); factory.tjobfactory(jobfactory); //设置数据源 factory.tdatasource(datasource); //设置事务 factory.ttransactionmanager(txmanager); //设置重写已存在的job factory.toverwriteexistingjobs(true); return factory; } @bean(name = "scheduler") public scheduler scheduler() throws ioexception { return quartzscheduler().getscheduler(); }}
package com.anjie.manage.quartz;import com.anjie.auth.common.exception.badconfigurationexception;import com.anjie.manage.common.springcontextutil;import lombok.tter;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@tterpublic class scheduledtask implements job { private integer id; private string params; private string rvicebeanname; private string methodname; @override public void execute(jobexecutioncontext jobexecutioncontext) { try { //使用反射执行spring中的任务 applicationcontext wac = springcontextutil.getapplicationcontext(); class<?> rvicebean = wac.getbean(rvicebeanname).getclass(); if (stringutils.isnotempty(params)){ method method = rvicebean.getdeclaredmethod(methodname,string.class); method.invoke(wac.getbean(rvicebean),params); }el { method method = rvicebean.getdeclaredmethod(methodname,null); method.invoke(wac.getbean(rvicebean),null); } }catch (exception e){ throw new badconfigurationexception("任务执行失败"); } }}
package com.anjie.manage.common;import org.springframework.beans.beanxception;import org.springframework.context.applicationcontext;import org.springframework.context.applicationcontextaware;import org.springframework.stereotype.component;@componentpublic class springcontextutil implements applicationcontextaware { private static applicationcontext applicationcontext = null; @override public void tapplicationcontext(applicationcontext applicationcontext) throws beanxception { springcontextutil.applicationcontext = applicationcontext; } /** * 获取applicationcontext * @return */ public static applicationcontext getapplicationcontext() { return applicationcontext; }}
package com.anjie.manage.quartz.rvice;import com.anjie.manage.quartz.entity.po.quartztask;import java.util.list;public interface quartzrvice { void initalltask(list<quartztask> scheduledtaskbeanlist) throws exception; void addjob(quartztask scheduledtask); void updatejob(quartztask quartztask); void deletejob(quartztask quartztask); void runjobonce(quartztask quartztask);}
package com.anjie.manage.quartz.rvice.impl;import com.anjie.manage.quartz.entity.po.quartztask;import com.anjie.manage.quartz.scheduledtask;import com.anjie.manage.quartz.rvice.quartzrvice;import lombok.extern.slf4j.slf4j;import org.quartz.*;import org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.rvice;import org.springframework.util.collectionutils;import java.util.*;@rvice@slf4jpublic class quartzrviceimpl implements quartzrvice { @autowired private scheduler scheduler; @override public void initalltask(list<quartztask> quartztasklist) throws exception { log.info("程序启动 ==> 初始化所有任务开始 !"); if (collectionutils.impty(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[] rvicebean = quartztask.getjobclass().split("\\."); //获得jobdatamap,写入数据 map<string, object> parammap = new hashmap<>(); parammap.put("id", quartztask.getid()); parammap.put("params", quartztask.getparams()); parammap.put("methodname",rvicebean[1]); parammap.put("rvicebeanname",rvicebean[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 = crons经济学专业介绍chedulebuilder.cronschedule(cron).withmisfirehandlinginstructiondonothing(); // 按新的cronexpression表达式重新构建trigger trigger = trigger.gettriggerbuilder().startnow().withidentity(triggerkey).withschedule(schedulebuilder).build(); } string[] rvicebean = quartztask.getjobclass().split("\\."); //获得jobdatamap,写入数据 map<string, object> parammap = new hashmap<>(); parammap.put("id", quartztask.getid()); parammap.put("params", quartztask.getparams()); parammap.put("rvicebeanname",rvicebean[0]); parammap.put("methodname",rvicebean[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.pautrigger(triggerkey.trigger手游攻略key(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.ran必有domuuid().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() .withintervalinconds(1) .withrepeatcount(0))//重复执行的次数,因为加入任务的时候马上执行了,所以不需要重复,否则会多一次。 .build(); string[] rvicebean = quartztask.getjobclass().split("\\."); //获得jobdatamap,写入数据 map<string, object> parammap = new hashmap<>(); parammap.put("id", quartztask.getid()); parammap.put("params", quartztask.getparams()); parammap.put("rvicebeanname",rvicebean[0]); parammap.put("methodname",rvicebean[1]); simpletrigger.getjobdatamap().putall(parammap); scheduler.schedulejob(jobdetail, simpletrigger); }catch (exception e){ log.error("run job quarttask = {} is fail, msg = {}", quartztask, e); } }}
项目启动后获取数据库中的定时任务列表,初始化定时任务
package com.anjie.manage.quartz;import com.anjie.manage.quartz.entity.po.quartztask;import com.anjie.manage.quartz.rvice.quartzrvice;import com.anjie.manage.quartz.rvice.quartztaskrvice;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)@slf4jpublic class scheduledtaskrunner implements applicationrunner { @autowired private quartztaskrvice quartztaskrvice; @autowired private quartzrvice quartzrvice; @override public void run(applicationarguments args) throws exception { log.info(" >>>>>> 项目启动完毕, 开启 => 需要自启的任务 开始!"); list<quartztask> activatedtasklist = quartztaskrvice.list(); quartzrvice.initalltask(activatedtasklist); log.info(" >>>>>> 项目启动完毕, 开启 => 需要自启的任务 结束!"); }}
映射数据库自定义任务表
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 st诗配画ring 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.bamapper;import org.apache.ibatis.annotations.mapper;import org.springframework.stereotype.repository;@repository@mapperpublic interface quartztaskdao extends bamapper<quartztask> {}
package com.anjie.manage.quartz.rvice;import com.anjie.manage.quartz.entity.po.quartztask;import com.baomidou.mybatisplus.extension.rvice.irvice;public interface quartztaskrvice extends irvice<quartztask> {}
package com.anjie.manage.quartz.rvice.impl;import com.anjie.manage.quartz.entity.po.quartztask;import com.anjie.manage.quartz.dao.quartztaskdao;import com.anjie.manage.quartz.rvice.quartztaskrvice;import com.baomidou.mybatisplus.extension.rvice.impl.rviceimpl;import org.springframework.stereotype.rvice;@rvicepublic class quartztaskrviceimpl extends rviceimpl<quartztaskdao, quartztask> implements quartztaskrvice{}
package com.anjie.manage.quartz.controller;import com.anjie.manage.common.responmessage;import com.anjie.manage.quartz.rvice.quartzrvice;import com.anjie.manage.quartz.entity.po.quartztask;import com.anjie.manage.quartz.rvice.quartztaskrvice;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 quartzrvice quartzrvice; @autowired private quartztaskrvice quartztaskrvice; @postmapping("/job") public responmessage addjob(@requestbody quartztask quartztask){ querywrapper<quartztask> querywrapper = new querywrapper<>(); querywrapper.eq("task_name",quartztask.gettaskname()); quartztask queryresult = quartztaskrvice.getone(querywrapper); if (queryresult != null){ return responmessage.fail("任务名称已存在!"); } quartztaskrvice.save(quartztask); return responmessage.ok("操作成功!"); } @putmapping("/job") public responmessage updatejob(@requestbody quartztask quartztask){ //判断任务是否启用 boolean status = quartztaskrvice.getbyid(quartztask.getid()).getstatus(); if (status){ return responmessage.fail("任务正在运行中,请暂停后修改!"); } //判断任务名称是否重名 querywrapper<quartztask> querywrapper = new querywrapper<>(); querywrapper.eq("task_name",quartztask.gettaskname()); quartztask queryresult = quartztaskrvice.getone(querywrapper); if (queryresult != null &&!queryresult.getid().equals(quartztask.getid())){ return responmessage.fail("任务名称已存在!"); } quartztaskrvice.updatebyid(quartztask); return responmessage.ok("操作成功!"); } @deletemapping("/job/{id}") public responmessage deletejob(@pathvariable integer id){ //判断任务是否启用 boolean status = quartztaskrvice.getbyid(id).getstatus(); if (status){ return responmessage.fail("任务正在运行中,请暂停后删除!"); } quartztaskrvice.removebyid(id); return responmessage.ok("操作成功!"); } @getmapping("/job") public responmessage 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 = quartztaskrvice.page(page,querywrapper); return responmessage.ok("操作成功",result); } /** * 修改任务的cron * * @return */// @requestmapping(value = "/updatejobcron",method = requestmethod.put)// public object updatejobcron(integer id, string cron) {// quartztask quartztask = quartztaskrvice.getbyid(id);// map<string, string> resultmap = new hashmap<>();// //如果存在quartztask对象// if (quartztask != null) {// //修改任务的cron// quartztask.tcron(cron);// //更新quart_task// quartztaskrvice.updatebyid(quartztask);// //如果启用状态则修改当前已持久化的job// if (quartztask.getstatus()) {// quartzrvice.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 responmessage updatejobstatus(@requestbody quartztask params) { quartztask quartztask = quartztaskrvice.getbyid(params.getid()); if (quartztask != null) { //修改任务的启用状态 quartztask.tstatus(params.getstatus()); //更新quart_task boolean updateresult = quartztaskrvice.updatebyid(quartztask); //根据status判断是新增job还是删除job if (params.getstatus() && updateresult) { quartzrvice.addjob(quartztask); } el if (updateresult){ quartzrvice.deletejob(quartztask); } return responmessage.ok("修改状态成功"); } return responmessage.fail("修改状态失败"); } /** * 启用一次任务 * @author ** * @param id 任务id * @return java.lang.object */ @postmapping("/runjob/{id}") public responmessage runjobonce(@pathvariable integer id){ quartztask quartztask = quartztaskrvice.getbyid(id); if (quartztask != null) { quartzrvice.runjobonce(quartztask); return responmessage.ok("任务执行成功"); } return responmessage.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 t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_name` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_group` varchar(200) character t 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 t = 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 t utf8mb4 collate utf8mb4_unicode_ci not null,`calendar_name` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`calendar` blob not null,primary key (`sched_name`, `calendar_name`) using btree) engine = innodb character t = 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 t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_name` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_group` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`cron_expression` varchar(120) character t utf8mb4 collate utf8mb4_unicode_ci not null,`time_zone_id` varchar(80) character t 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 t = 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 t utf8mb4 collate utf8mb4_unicode_ci not null,`entry_id` varchar(95) character t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_name` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_group` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`instance_name` varchar(200) character t 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 t utf8mb4 collate utf8mb4_unicode_ci not null,`job_name` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci null default null,`job_group` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci null default null,`is_nonconcurrent` varchar(1) character t utf8mb4 collate utf8mb4_unicode_ci null default null,`requests_recovery` varchar(1) character t 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 t = 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 t utf8mb4 collate utf8mb4_unicode_ci not null,`job_name` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`job_group` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`description` varchar(250) character t utf8mb4 collate utf8mb4_unicode_ci null default null,`job_class_name` varchar(250) character t utf8mb4 collate utf8mb4_unicode_ci not null,`is_durable` varchar(1) character t utf8mb4 collate utf8mb4_unicode_ci not null,`is_nonconcurrent` varchar(1) character t utf8mb4 collate utf8mb4_unicode_ci not null,`is_update_data` varchar(1) character t utf8mb4 collate utf8mb4_unicode_ci not null,`requests_recovery` varchar(1) character t 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 t = 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 t utf8mb4 collate utf8mb4_unicode_ci not null,`lock_name` varchar(40) character t utf8mb4 collate utf8mb4_unicode_ci not null,primary key (`sched_name`, `lock_name`) using btree) engine = innodb character t = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic;-- ------------------------------ table structure for qrtz_paud_trigger_grps-- ----------------------------drop table if exists `qrtz_paud_trigger_grps`;create table `qrtz_paud_trigger_grps` (`sched_name` varchar(120) character t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_group` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,primary key (`sched_name`, `trigger_group`) using btree) engine = innodb character t = 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 t utf8mb4 collate utf8mb4_unicode_ci not null,`instance_name` varchar(200) character t 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 t = 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 t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_name` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_group` varchar(200) character t 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 t = 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 t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_name` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_group` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`str_prop_1` varchar(512) character t utf8mb4 collate utf8mb4_unicode_ci null default null,`str_prop_2` varchar(512) character t utf8mb4 collate utf8mb4_unicode_ci null default null,`str_prop_3` varchar(512) character t 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 t utf8mb4 collate utf8mb4_unicode_ci null default null,`bool_prop_2` varchar(1) character t 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 t = 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 t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_name` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_group` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`job_name` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`job_group` varchar(200) character t utf8mb4 collate utf8mb4_unicode_ci not null,`description` varchar(250) character t 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 t utf8mb4 collate utf8mb4_unicode_ci not null,`trigger_type` varchar(8) character t 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 t 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 t = 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 t utf8mb4 collate utf8mb4_unicode_ci null default null comment '任务名称',`group_name` varchar(255) character t utf8mb4 collate utf8mb4_unicode_ci null default null comment 'group名称',`job_class` varch吴镇宇的老婆ar(255) character t utf8mb4 collate utf8mb4_unicode_ci null default null comment 'job所在类',`params` varchar(255) character t utf8mb4 collate utf8mb4_unicode_ci null default null comment '参数',`cron` varchar(255) character t utf8mb4 collate utf8mb4_unicode_ci null default null comment 'cron表达式',`status` tinyint(1) null default 0 comment '状态',`des` varchar(500) character t utf8mb4 collate utf8mb4_unicode_ci null default null comment '描述',primary key (`id`) using btree) engine = innodb auto_increment = 16 character t = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic;
1、新建具体需要执行任务
package com.anjie.manage.quartz;import com.anjie.manage.quartz.rvice.quartztaskrvice;import org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.rvice;@rvicepublic class task01 {@autowiredprivate quartztaskrvice quartztaskrvice;public void test(string param){quartztaskrvice.list();system.out.println("test01被执行(任务示例)"+param);}}
2、配置定时任务
注意:
1.这里的任务路径是需要执行的类中的方法路径,采用bean的名称 + 方法名(例:task01.test),名称首字母小写,不然会找不到bean,也可以自己给需要执行的任务类bean起名
2.参数是字符串,可以自己写json字符串,是否传参与方法中保持一致,方法中有形参,这里就必传,方法没有形参,这里就不传,不然任务会执行失败
到此这篇关于springboot整合quartz定时任务框架的文章就介绍到这了,更多相关springboot整合quartz定时任务内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 14:25:50,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/d1a6c4710b8ef3f5696833cf935a2e0e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:springboot整合quartz定时任务框架的完整步骤.doc
本文 PDF 下载地址:springboot整合quartz定时任务框架的完整步骤.pdf
留言与评论(共有 0 条评论) |