maven项目集成Quartz定时任务框架,实现批处理功能
程序员文章站
2023-11-02 14:47:34
一、Quartz简介 主要做定时任务,即:在指定时间点或时间段,执行某项任务,可设置执行次数、时间间隔等。 二、Springcloud简介 对比传统的、庞大的、复杂的、以ssm或ssh为框架的web项目而言,springcloud显得格外轻巧,不错乱,易管理,至少模块清晰,功能明确。 三、Sprin ......
一、quartz简介
主要做定时任务,即:在指定时间点或时间段,执行某项任务,可设置执行次数、时间间隔等。
二、springcloud简介
对比传统的、庞大的、复杂的、以ssm或ssh为框架的web项目而言,springcloud显得格外轻巧,不错乱,易管理,至少模块清晰,功能明确。
三、springcloud集成quartz
就个人理解,quartz可集成到某个功能模块,也可以单独做一个服务(功能模块),本文介绍的就是单独作为一个服务进行集成的(未开启集群)。
第一步:在已有的父工程下新建一个子工程(可有可无)
第二步:依赖
第三步:配置
1 /** 2 * @title: myjobfactory.java 3 * @package com.ewp.data.controller.quartz 4 * @description: 解决不能spring注入bean的问题 5 * @author zxj 6 * @date 2018年2月26日 7 * @version v1.0 8 */ 9 package com.ewp.data.config; 10 11 import org.quartz.spi.triggerfiredbundle; 12 import org.springframework.beans.factory.annotation.autowired; 13 import org.springframework.beans.factory.config.autowirecapablebeanfactory; 14 import org.springframework.scheduling.quartz.adaptablejobfactory; 15 import org.springframework.stereotype.component; 16 17 /** 18 * @classname: myjobfactory 19 * @description: 解决不能spring注入bean的问题 20 * @author zxj 21 * @date 2018年2月26日 22 * 23 */ 24 @component 25 public class myjobfactory extends adaptablejobfactory { 26 @autowired 27 private autowirecapablebeanfactory capablebeanfactory; 28 29 @override 30 protected object createjobinstance(triggerfiredbundle bundle) throws exception { 31 object jobinstance = super.createjobinstance(bundle); 32 capablebeanfactory.autowirebean(jobinstance); 33 return jobinstance; 34 } 35 }
1 2 import java.io.ioexception; 3 import java.util.properties; 4 5 import org.quartz.scheduler; 6 import org.quartz.schedulerexception; 7 import org.springframework.beans.factory.annotation.autowired; 8 import org.springframework.beans.factory.annotation.value; 9 import org.springframework.context.annotation.bean; 10 import org.springframework.context.annotation.configuration; 11 import org.springframework.scheduling.quartz.schedulerfactorybean; 12 13 /** 14 * 15 * @classname: quartzconfig 16 * @description: 配置任务调度中心(服务启动时启动) 17 * @author zxj 18 * @date 2018年2月26日 19 * 20 */ 21 @configuration 22 public class quartzconfig { 23 24 @autowired 25 private myjobfactory myfactory; //自定义的factory 26 27 @value("${spring.datasource.url}") 28 private string url;// 数据源地址 29 30 @value("${spring.datasource.username}") 31 private string username;// 用户名 32 33 @value("${spring.datasource.password}") 34 private string password;// 密码 35 36 @bean 37 public schedulerfactorybean schedulerfactorybean() { 38 schedulerfactorybean schedulerfactorybean = new schedulerfactorybean(); 39 try { 40 schedulerfactorybean.setquartzproperties(quartzproperties()); 41 schedulerfactorybean.setjobfactory(myfactory); //指向自建的调度工厂,用于解决方法类无法注入的问题 42 } catch (ioexception e) { 43 e.printstacktrace(); 44 } 45 return schedulerfactorybean; 46 } 47 48 @bean 49 public scheduler scheduler() throws ioexception, schedulerexception { 50 scheduler scheduler = schedulerfactorybean().getscheduler(); 51 scheduler.start();// 服务启动shi 52 return scheduler; 53 } 54 /** 55 * 56 * @title: quartzproperties 57 * @description: 设置quartz属性 58 * @param @return 59 * @param @throws ioexception 参数 60 * @return properties 返回类型 61 * @throws 62 */ 63 public properties quartzproperties() throws ioexception { 64 properties prop = new properties(); 65 prop.put("org.quartz.scheduler.instancename", "quartzscheduler");// 调度器的实例名 66 prop.put("org.quartz.scheduler.instanceid", "auto");// 实例的标识 67 prop.put("org.quartz.scheduler.skipupdatecheck", "true");// 检查quartz是否有版本更新(true 不检查) 68 prop.put("org.quartz.jobstore.class", "org.quartz.impl.jdbcjobstore.jobstoretx"); 69 prop.put("org.quartz.jobstore.driverdelegateclass", "org.quartz.impl.jdbcjobstore.stdjdbcdelegate"); 70 prop.put("org.quartz.jobstore.tableprefix", "qrtz_");// 表名前缀 71 prop.put("org.quartz.jobstore.isclustered", "false");// 集群开关 72 prop.put("org.quartz.threadpool.class", "org.quartz.simpl.simplethreadpool");// 线程池的名字 73 prop.put("org.quartz.threadpool.threadcount", "10");// 指定线程数量 74 prop.put("org.quartz.threadpool.threadpriority", "5");// 线程优先级(1-10)默认为5 75 prop.put("org.quartz.threadpool.threadsinheritcontextclassloaderofinitializingthread", "true"); 76 prop.put("org.quartz.jobstore.datasource", "quartzdatasource"); 77 78 prop.put("org.quartz.datasource.quartzdatasource.driver", "com.mysql.jdbc.driver"); 79 prop.put("org.quartz.datasource.quartzdatasource.url", url); 80 prop.put("org.quartz.datasource.quartzdatasource.user", username); 81 prop.put("org.quartz.datasource.quartzdatasource.password", password); 82 prop.put("org.quartz.datasource.quartzdatasource.maxconnections", "50"); 83 return prop; 84 } 85 86 87 }
第四步:建类(任务管理类)
1 2 import java.io.serializable; 3 4 /** 5 * 6 * @classname: taskinfo 7 * @description: 管理定时任务 8 * @author zxj 9 * @date 2018年2月26日 10 * 11 */ 12 public class taskinfo implements serializable{ 13 private static final long serialversionuid = -8054692082716173379l; 14 15 /** 16 * 增加或修改标识 17 */ 18 private int id; 19 20 /** 21 * 任务名称 22 */ 23 private string jobname; 24 25 /** 26 * 任务分组 27 */ 28 private string jobgroup; 29 30 /** 31 * 任务描述 32 */ 33 private string jobdescription; 34 35 /** 36 * 任务状态 37 */ 38 private string jobstatus; 39 40 /** 41 * 任务表达式 42 */ 43 private string cronexpression; 44 45 /** 46 * 创建时间 47 */ 48 private string createtime; 49 50 /** 51 * 间隔时间(毫秒) 52 */ 53 private string milliseconds; 54 55 /** 56 * 重复次数 57 */ 58 private string repeatcount; 59 60 /** 61 * 起始时间 62 */ 63 private string startdate; 64 65 /** 66 * 终止时间 67 */ 68 private string enddate; 69 70 /** 71 * @return the milliseconds 72 */ 73 public string getmilliseconds() { 74 return milliseconds; 75 } 76 77 /** 78 * @param milliseconds the milliseconds to set 79 */ 80 public void setmilliseconds(string milliseconds) { 81 this.milliseconds = milliseconds; 82 } 83 84 /** 85 * @return the repeatcount 86 */ 87 public string getrepeatcount() { 88 return repeatcount; 89 } 90 91 /** 92 * @param repeatcount the repeatcount to set 93 */ 94 public void setrepeatcount(string repeatcount) { 95 this.repeatcount = repeatcount; 96 } 97 98 /** 99 * @return the startdate 100 */ 101 public string getstartdate() { 102 return startdate; 103 } 104 105 /** 106 * @param startdate the startdate to set 107 */ 108 public void setstartdate(string startdate) { 109 this.startdate = startdate; 110 } 111 112 /** 113 * @return the enddate 114 */ 115 public string getenddate() { 116 return enddate; 117 } 118 119 /** 120 * @param enddate the enddate to set 121 */ 122 public void setenddate(string enddate) { 123 this.enddate = enddate; 124 } 125 126 public string getjobname() { 127 return jobname; 128 } 129 130 public void setjobname(string jobname) { 131 this.jobname = jobname; 132 } 133 134 public string getjobgroup() { 135 return jobgroup; 136 } 137 138 public void setjobgroup(string jobgroup) { 139 this.jobgroup = jobgroup; 140 } 141 142 public string getjobdescription() { 143 return jobdescription; 144 } 145 146 public void setjobdescription(string jobdescription) { 147 this.jobdescription = jobdescription; 148 } 149 150 public string getjobstatus() { 151 return jobstatus; 152 } 153 154 public void setjobstatus(string jobstatus) { 155 this.jobstatus = jobstatus; 156 } 157 158 public string getcronexpression() { 159 return cronexpression; 160 } 161 162 public void setcronexpression(string cronexpression) { 163 this.cronexpression = cronexpression; 164 } 165 166 public string getcreatetime() { 167 return createtime; 168 } 169 170 public void setcreatetime(string createtime) { 171 this.createtime = createtime; 172 } 173 174 public int getid() { 175 return id; 176 } 177 178 public void setid(int id) { 179 this.id = id; 180 } 181 }
第五步:写方法(任务处理方法)
1 package com.ewp.data.service; 2 3 import java.util.arraylist; 4 import java.util.date; 5 import java.util.hashset; 6 import java.util.list; 7 8 import org.apache.commons.lang3.time.dateformatutils; 9 import org.quartz.cronschedulebuilder; 10 import org.quartz.crontrigger; 11 import org.quartz.job; 12 import org.quartz.jobbuilder; 13 import org.quartz.jobdetail; 14 import org.quartz.jobkey; 15 import org.quartz.scheduler; 16 import org.quartz.schedulerexception; 17 import org.quartz.simpleschedulebuilder; 18 import org.quartz.simpletrigger; 19 import org.quartz.trigger; 20 import org.quartz.triggerbuilder; 21 import org.quartz.triggerkey; 22 import org.quartz.impl.matchers.groupmatcher; 23 import org.slf4j.logger; 24 import org.slf4j.loggerfactory; 25 import org.springframework.beans.factory.annotation.autowired; 26 import org.springframework.stereotype.service; 27 28 import com.ewp.data.quartz.taskinfo; 29 import com.ewp.data.quartz.exception.serviceexception; 30 import com.ewp.data.util.dateutil; 31 32 /** 33 * 34 * @classname: taskservice 35 * @description: 任务处理类 36 * @author zxj 37 * @date 2018年2月26日 38 * 39 */ 40 @service 41 public class taskservice { 42 private static final logger logger = loggerfactory.getlogger(taskservice.class); 43 44 @autowired 45 private scheduler scheduler; 46 47 /** 48 * 49 * @title: list 50 * @description: 任务列表 51 * @param @return 参数 52 * @return list<taskinfo> 返回类型 53 * @throws 54 */ 55 public list<taskinfo> queryjoblist() { 56 logger.info("taskservice--data-s-->queryjoblist()"); 57 list<taskinfo> list = new arraylist<>(); 58 try { 59 for (string groupjob : scheduler.getjobgroupnames()) { 60 for (jobkey jobkey : scheduler.getjobkeys(groupmatcher.<jobkey> groupequals(groupjob))) { 61 list<? extends trigger> triggers = scheduler.gettriggersofjob(jobkey); 62 for (trigger trigger : triggers) { 63 trigger.triggerstate triggerstate = scheduler.gettriggerstate(trigger.getkey()); 64 jobdetail jobdetail = scheduler.getjobdetail(jobkey); 65 string cronexpression = ""; 66 string createtime = ""; 67 string milliseconds = ""; 68 string repeatcount = ""; 69 string startdate = ""; 70 string enddate = ""; 71 if (trigger instanceof crontrigger) { 72 crontrigger crontrigger = (crontrigger) trigger; 73 cronexpression = crontrigger.getcronexpression(); 74 createtime = crontrigger.getdescription(); 75 } else if (trigger instanceof simpletrigger) { 76 simpletrigger simpletrigger = (simpletrigger) trigger; 77 milliseconds = simpletrigger.getrepeatinterval()+ ""; 78 repeatcount = simpletrigger.getrepeatcount() + ""; 79 startdate = dateutil.getdatestr( 80 simpletrigger.getstarttime(), 81 dateutil.format_hour_date_time); 82 enddate = dateutil.getdatestr(simpletrigger.getendtime(),dateutil.format_hour_date_time); 83 } 84 taskinfo info = new taskinfo(); 85 info.setjobname(jobkey.getname()); 86 info.setjobgroup(jobkey.getgroup()); 87 info.setjobdescription(jobdetail.getdescription()); 88 info.setjobstatus(triggerstate.name()); 89 info.setcronexpression(cronexpression); 90 info.setcreatetime(createtime); 91 92 info.setrepeatcount(repeatcount); 93 info.setstartdate(startdate); 94 info.setmilliseconds(milliseconds); 95 info.setenddate(enddate); 96 list.add(info); 97 } 98 } 99 } 100 logger.info("任务的数量为:---------------->" + list.size()); 101 } catch (schedulerexception e) { 102 logger.info("查询任务失败,原因是:------------------>" + e.getmessage()); 103 e.printstacktrace(); 104 } 105 return list; 106 } 107 108 /** 109 * 110 * @title: setsimpletrigger 111 * @description: 简单调度 112 * @param @param inputmap 113 * @param @return 参数 114 * @return boolean 返回类型 115 * @throws 116 */ 117 @suppresswarnings({ "unchecked" }) 118 public void setsimpletriggerjob(taskinfo info) { 119 logger.info("taskservice--data-s-->setsimpletriggerjob()" + info); 120 string jobname = info.getjobname(); 121 string jobgroup = info.getjobgroup(); 122 string jobdescription = info.getjobdescription(); 123 long milliseconds = long.parselong(info.getmilliseconds()); 124 integer repeatcount = integer.parseint(info.getrepeatcount()); 125 date startdate = dateutil.parsedate(info.getstartdate()); 126 date enddate = dateutil.parsedate(info.getenddate()); 127 try { 128 triggerkey triggerkey = triggerkey.triggerkey(jobname, jobgroup);// 触发器的key值 129 jobkey jobkey = jobkey.jobkey(jobname, jobgroup);// job的key值 130 if (checkexists(jobname, jobgroup)) { 131 logger.info( 132 "===> addjob fail, job already exist, jobgroup:{}, jobname:{}", 133 jobgroup, jobname); 134 throw new serviceexception(string.format( 135 "job已经存在, jobname:{%s},jobgroup:{%s}", jobname, 136 jobgroup)); 137 } 138 /* 简单调度 */ 139 simpletrigger trigger = (simpletrigger) triggerbuilder 140 .newtrigger() 141 .withidentity(triggerkey) 142 .startat(startdate) 143 .withschedule( 144 simpleschedulebuilder.simpleschedule() 145 .withintervalinmilliseconds(milliseconds) 146 .withrepeatcount(repeatcount)) 147 .endat(enddate).build(); 148 class<? extends job> clazz = (class<? extends job>) class 149 .forname(jobname); 150 jobdetail jobdetail = jobbuilder.newjob(clazz).withidentity(jobkey) 151 .withdescription(jobdescription).build(); 152 scheduler.schedulejob(jobdetail, trigger); 153 } catch (schedulerexception | classnotfoundexception e) { 154 logger.info("任务添加失败!--->简单调度" + e.getmessage()); 155 throw new serviceexception("任务添加失败!--->简单调度"); 156 } 157 } 158 159 /** 160 * 161 * @title: addjob 162 * @description: 保存定时任务 163 * @param @param info 参数 164 * @return void 返回类型 165 * @throws 166 */ 167 @suppresswarnings("unchecked") 168 public void addjob(taskinfo info) { 169 logger.info("taskservice--data-s-->addjob()" + info); 170 string jobname = info.getjobname(), jobgroup = info.getjobgroup(), cronexpression = info 171 .getcronexpression(), jobdescription = info.getjobdescription(), createtime = dateformatutils 172 .format(new date(), "yyyy-mm-dd hh:mm:ss"); 173 try { 174 if (checkexists(jobname, jobgroup)) { 175 logger.info( 176 "===> addjob fail, job already exist, jobgroup:{}, jobname:{}", 177 jobgroup, jobname); 178 throw new serviceexception(string.format( 179 "job已经存在, jobname:{%s},jobgroup:{%s}", jobname, 180 jobgroup)); 181 } 182 183 triggerkey triggerkey = triggerkey.triggerkey(jobname, jobgroup); 184 jobkey jobkey = jobkey.jobkey(jobname, jobgroup); 185 186 cronschedulebuilder schedbuilder = cronschedulebuilder 187 .cronschedule(cronexpression) 188 .withmisfirehandlinginstructiondonothing(); 189 crontrigger trigger = triggerbuilder.newtrigger() 190 .withidentity(triggerkey).withdescription(createtime) 191 .withschedule(schedbuilder).build(); 192 193 class<? extends job> clazz = (class<? extends job>) class 194 .forname(jobname); 195 jobdetail jobdetail = jobbuilder.newjob(clazz).withidentity(jobkey) 196 .withdescription(jobdescription).build(); 197 scheduler.schedulejob(jobdetail, trigger); 198 } catch (schedulerexception | classnotfoundexception e) { 199 logger.info("保存定时任务-->类名不存在或执行表达式错误--->复杂调度" + e.getmessage()); 200 throw new serviceexception("类名不存在或执行表达式错误"); 201 } 202 } 203 204 /** 205 * 206 * @title: edit 207 * @description: 修改定时任务 208 * @param @param info 参数 209 * @return void 返回类型 210 * @throws 211 */ 212 public void editjob(taskinfo info) { 213 logger.info("taskservice--data-s-->editjob()" + info); 214 string jobname = info.getjobname(), jobgroup = info.getjobgroup(), cronexpression = info 215 .getcronexpression(), jobdescription = info.getjobdescription(), createtime = dateformatutils 216 .format(new date(), "yyyy-mm-dd hh:mm:ss"); 217 try { 218 if (!checkexists(jobname, jobgroup)) { 219 throw new serviceexception( 220 string.format("job不存在, jobname:{%s},jobgroup:{%s}", 221 jobname, jobgroup)); 222 } 223 triggerkey triggerkey = triggerkey.triggerkey(jobname, jobgroup); 224 jobkey jobkey = new jobkey(jobname, jobgroup); 225 cronschedulebuilder cronschedulebuilder = cronschedulebuilder 226 .cronschedule(cronexpression) 227 .withmisfirehandlinginstructiondonothing(); 228 crontrigger crontrigger = triggerbuilder.newtrigger() 229 .withidentity(triggerkey).withdescription(createtime) 230 .withschedule(cronschedulebuilder).build(); 231 232 jobdetail jobdetail = scheduler.getjobdetail(jobkey); 233 jobdetail.getjobbuilder().withdescription(jobdescription); 234 hashset<trigger> triggerset = new hashset<>(); 235 triggerset.add(crontrigger); 236 237 scheduler.schedulejob(jobdetail, triggerset, true); 238 } catch (schedulerexception e) { 239 logger.info("修改定时任务-->类名不存在或执行表达式错误--->复杂调度" + e.getmessage()); 240 throw new serviceexception("类名不存在或执行表达式错误"); 241 } 242 } 243 244 /** 245 * 246 * @title: delete 247 * @description: 删除定时任务 248 * @param @param jobname 249 * @param @param jobgroup 参数 250 * @return void 返回类型 251 * @throws 252 */ 253 public void deletejob(string jobname, string jobgroup) { 254 logger.info("taskservice--data-s-->deletejob()--jobname:" + jobname 255 + "jobgroup:" + jobgroup); 256 triggerkey triggerkey = triggerkey.triggerkey(jobname, jobgroup); 257 try { 258 if (checkexists(jobname, jobgroup)) { 259 scheduler.pausetrigger(triggerkey); 260 scheduler.unschedulejob(triggerkey); 261 logger.info("===> delete, triggerkey:{}", triggerkey); 262 } 263 } catch (schedulerexception e) { 264 logger.info("删除定时任务-->复杂调度" + e.getmessage()); 265 throw new serviceexception(e.getmessage()); 266 } 267 } 268 269 /** 270 * 271 * @title: pause 272 * @description: 暂停定时任务 273 * @param @param jobname 274 * @param @param jobgroup 参数 275 * @return void 返回类型 276 * @throws 277 */ 278 public void pausejob(string jobname, string jobgroup) { 279 logger.info("taskservice--data-s-->pausejob()--jobname:" + jobname 280 + "jobgroup:" + jobgroup); 281 triggerkey triggerkey = triggerkey.triggerkey(jobname, jobgroup); 282 try { 283 if (checkexists(jobname, jobgroup)) { 284 scheduler.pausetrigger(triggerkey); 285 logger.info("===> pause success, triggerkey:{}", triggerkey); 286 } 287 } catch (schedulerexception e) { 288 logger.info("暂停定时任务-->复杂调度" + e.getmessage()); 289 throw new serviceexception(e.getmessage()); 290 } 291 } 292 293 /** 294 * 295 * @title: resume 296 * @description: 恢复暂停任务 297 * @param @param jobname 298 * @param @param jobgroup 参数 299 * @return void 返回类型 300 * @throws 301 */ 302 public void resumejob(string jobname, string jobgroup) { 303 logger.info("taskservice--data-s-->resumejob()--jobname:" + jobname 304 + "jobgroup:" + jobgroup); 305 triggerkey triggerkey = triggerkey.triggerkey(jobname, jobgroup); 306 try { 307 if (checkexists(jobname, jobgroup)) { 308 scheduler.resumetrigger(triggerkey); 309 logger.info("===> resume success, triggerkey:{}", triggerkey); 310 } 311 } catch (schedulerexception e) { 312 logger.info("重新开始任务-->复杂调度" + e.getmessage()); 313 e.printstacktrace(); 314 } 315 } 316 317 /** 318 * 319 * @title: checkexists 320 * @description: 验证任务是否存在 321 * @param @param jobname 322 * @param @param jobgroup 323 * @param @return 324 * @param @throws schedulerexception 参数 325 * @return boolean 返回类型 326 * @throws 327 */ 328 private boolean checkexists(string jobname, string jobgroup) 329 throws schedulerexception { 330 logger.info("taskservice--data-s-->checkexists()--jobname:" + jobname 331 + "jobgroup:" + jobgroup); 332 triggerkey triggerkey = triggerkey.triggerkey(jobname, jobgroup); 333 return scheduler.checkexists(triggerkey); 334 } 335 336 }
第六步:执行sql脚本
drop table if exists qrtz_fired_triggers; drop table if exists qrtz_paused_trigger_grps; drop table if exists qrtz_scheduler_state; drop table if exists qrtz_locks; drop table if exists qrtz_simple_triggers; drop table if exists qrtz_simprop_triggers; drop table if exists qrtz_cron_triggers; drop table if exists qrtz_blob_triggers; drop table if exists qrtz_triggers; drop table if exists qrtz_job_details; drop table if exists qrtz_calendars; create table qrtz_job_details ( sched_name varchar(120) not null, job_name varchar(200) not null, job_group varchar(200) not null, description varchar(250) null, job_class_name varchar(250) not null, is_durable varchar(1) not null, is_nonconcurrent varchar(1) not null, is_update_data varchar(1) not null, requests_recovery varchar(1) not null, job_data blob null, primary key (sched_name,job_name,job_group) ); create table qrtz_triggers ( sched_name varchar(120) not null, trigger_name varchar(200) not null, trigger_group varchar(200) not null, job_name varchar(200) not null, job_group varchar(200) not null, description varchar(250) null, next_fire_time bigint(13) null, prev_fire_time bigint(13) null, priority integer null, trigger_state varchar(16) not null, trigger_type varchar(8) not null, start_time bigint(13) not null, end_time bigint(13) null, calendar_name varchar(200) null, misfire_instr smallint(2) null, job_data blob null, primary key (sched_name,trigger_name,trigger_group), foreign key (sched_name,job_name,job_group) references qrtz_job_details(sched_name,job_name,job_group) ); create table qrtz_simple_triggers ( sched_name varchar(120) not null, trigger_name varchar(200) not null, trigger_group varchar(200) 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), foreign key (sched_name,trigger_name,trigger_group) references qrtz_triggers(sched_name,trigger_name,trigger_group) ); create table qrtz_cron_triggers ( sched_name varchar(120) not null, trigger_name varchar(200) not null, trigger_group varchar(200) not null, cron_expression varchar(200) not null, time_zone_id varchar(80), primary key (sched_name,trigger_name,trigger_group), foreign key (sched_name,trigger_name,trigger_group) references qrtz_triggers(sched_name,trigger_name,trigger_group) ); create table qrtz_simprop_triggers ( sched_name varchar(120) not null, trigger_name varchar(200) not null, trigger_group varchar(200) not null, str_prop_1 varchar(512) null, str_prop_2 varchar(512) null, str_prop_3 varchar(512) null, int_prop_1 int null, int_prop_2 int null, long_prop_1 bigint null, long_prop_2 bigint null, dec_prop_1 numeric(13,4) null, dec_prop_2 numeric(13,4) null, bool_prop_1 varchar(1) null, bool_prop_2 varchar(1) null, primary key (sched_name,trigger_name,trigger_group), foreign key (sched_name,trigger_name,trigger_group) references qrtz_triggers(sched_name,trigger_name,trigger_group) ); create table qrtz_blob_triggers ( sched_name varchar(120) not null, trigger_name varchar(200) not null, trigger_group varchar(200) not null, blob_data blob null, primary key (sched_name,trigger_name,trigger_group), foreign key (sched_name,trigger_name,trigger_group) references qrtz_triggers(sched_name,trigger_name,trigger_group) ); create table qrtz_calendars ( sched_name varchar(120) not null, calendar_name varchar(200) not null, calendar blob not null, primary key (sched_name,calendar_name) ); create table qrtz_paused_trigger_grps ( sched_name varchar(120) not null, trigger_group varchar(200) not null, primary key (sched_name,trigger_group) ); create table qrtz_fired_triggers ( sched_name varchar(120) not null, entry_id varchar(95) not null, trigger_name varchar(200) not null, trigger_group varchar(200) not null, instance_name varchar(200) not null, fired_time bigint(13) not null, sched_time bigint(13) not null, priority integer not null, state varchar(16) not null, job_name varchar(200) null, job_group varchar(200) null, is_nonconcurrent varchar(1) null, requests_recovery varchar(1) null, primary key (sched_name,entry_id) ); create table qrtz_scheduler_state ( sched_name varchar(120) not null, instance_name varchar(200) not null, last_checkin_time bigint(13) not null, checkin_interval bigint(13) not null, primary key (sched_name,instance_name) ); create table qrtz_locks ( sched_name varchar(120) not null, lock_name varchar(40) not null, primary key (sched_name,lock_name) );
第七步:编写自定义任务(需实现job接口)
1 /** 2 * @title: reportmailbatch.java 3 * @package com.ewp.data.batch 4 * @description: 邮件汇报批 5 * @author zxj 6 * @date 2018年2月26日 7 * @version v1.0 8 */ 9 package com.ewp.data.batch; 10 11 import org.quartz.job; 12 import org.quartz.jobexecutioncontext; 13 import org.quartz.jobexecutionexception; 14 import org.slf4j.logger; 15 import org.slf4j.loggerfactory; 16 import org.springframework.beans.factory.annotation.autowired; 17 import org.springframework.stereotype.component; 18 19 import com.ewp.data.service.reportmailservice; 20 21 /** 22 * @classname: reportmailbatch 23 * @description: 邮件汇报批 24 * @author zxj 25 * @date 2018年2月26日 26 * 27 */ 28 @component 29 public class reportmailbatch implements job{ 30 31 private static final logger log = loggerfactory.getlogger(reportmailbatch.class); 32 33 @autowired 34 private reportmailservice service; 35 36 @override 37 public void execute(jobexecutioncontext arg0) throws jobexecutionexception { 38 log.info("reportmailbatch--data-c->execute()"); 39 service.sendreportmail();// 可换成自己业务逻辑 40 log.info("--本次邮件汇报批处理结束--"); 41 } 42 }
第八步:创建任务处理controller注入taskservice,将第七步编写的任务加入调度器,由线程调用执行。
上一篇: 抽象类、模板方法设计模式