项目实践过程中碰到一个动态管理定时任务的需求:针对每个人员进行信息的定时更新,具体更新时间可随时调整、启动、暂定等。
思路
将每个人员信息的定时配置保存到数据库中,这样实现了任务的动态展示和管理。任务的每一次新增或变更,都会去数据库变更信息。
设置一个统一的任务管理器,专门负责动态任务的增删改查。
pom依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.example</groupid> <artifactid>dynamic-quartz</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>dynamic-quartz</name> <description>动态定时任务管理</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 2.0 . 2 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version> 1.1 . 1 </version> </dependency> <!-- jdbc driver --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version> 5.1 . 44 </version> </dependency> <dependency> <groupid>org.quartz-scheduler</groupid> <artifactid>quartz</artifactid> <version> 2.3 . 0 </version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> application.yml spring: datasource: url: jdbc:mysql: //localhost:3306/wujiwen username: root password: root driver- class -name: com.mysql.jdbc.driver profiles: active: dev logback-spring.xml <configuration> <jmxconfigurator /> <appender name= "stdout" class = "ch.qos.logback.core.consoleappender" > <layout class = "ch.qos.logback.classic.patternlayout" > <pattern>%date [%thread] %-5level %logger{ 80 } - %msg%n</pattern> </layout> </appender> <logger name= "org.springframework" > <level value= "error" /> </logger> <logger name= "org.mybatis" > <level value= "error" /> </logger> <logger name= "java.sql.connection" > <level value= "debug" /> </logger> <logger name= "java.sql.statement" > <level value= "debug" /> </logger> <logger name= "java.sql.preparedstatement" > <level value= "debug" /> </logger> <logger name= "java.sql.resultset" > <level value= "debug" /> </logger> <logger name= "org.apache.http" > <level value= "info" /> </logger> <logger name= "org.quartz" > <level value= "info" /> </logger> <springprofile name= "dev,test" > <root> <level value= "debug" /> <appender-ref ref= "stdout" /> </root> </springprofile> </configuration> cronentity.java 保存在数据库中的表达式管理器. package com.example.dynamicquartz.bean; import java.io.serializable; import java.util.date; /** * created with intellij idea. * packagename : com.example.dynamicquartz.bean * author : wujw * date : 2018/9/9 16:23 * version : 1.0.0 * description : todo */ public class cronentity implements serializable { private static final long serialversionuid = -3406421161273529348l; private string id; private string userid; //用户标识 private string cron; //表达式 private string quarzname; //任务名称 private string schedulerclass; //定时任务类 private date time; public string getid() { return id; } public void setid(string id) { this .id = id; } public string getuserid() { return userid; } public void setuserid(string userid) { this .userid = userid; } public string getcron() { return cron; } public void setcron(string cron) { this .cron = cron; } public string getquarzname() { return quarzname; } public void setquarzname(string quarzname) { this .quarzname = quarzname; } public string getschedulerclass() { return schedulerclass; } public void setschedulerclass(string schedulerclass) { this .schedulerclass = schedulerclass; } public date gettime() { return time; } public void settime(date time) { this .time = time; } @override public string tostring() { return "cronentity{" + "id='" + id + '\ '' + ", userid='" + userid + '\ '' + ", cron='" + cron + '\ '' + ", quarzname='" + quarzname + '\ '' + ", schedulerclass='" + schedulerclass + '\ '' + ", time=" + time + '}' ; } } cronmapper package com.example.dynamicquartz.dao; import com.example.dynamicquartz.bean.cronentity; import org.apache.ibatis.annotations.*; import java.util.list; /** * created with intellij idea. * packagename : com.example.dynamicquartz * author : wujw * date : 2018/9/9 20:14 * version : 1.0.0 * description : todo */ @mapper public interface cronmapper{ @select ( "select * from cron_table where id = #{id}" ) @results ({ @result (property = "id" , column = "id" ), @result (property = "userid" , column = "user_id" ), @result (property = "cron" , column = "cron" ), @result (property = "quarzname" , column = "quarz_name" ), @result (property = "schedulerclass" , column = "scheduler_class" ), @result (property = "time" , column = "time" ) }) cronentity load(string id); @insert ( "insert into cron_table(id,userid,cron,quartzname,schedulerclass,time)" + "values(#{id},#{user_id},#{cron},#{quartz_name},#{scheduler_class},# {time})" ) void insert(cronentity cronentity); @select ( "select * from cron_table" ) @results ({ @result (property = "id" , column = "id" ), @result (property = "userid" , column = "user_id" ), @result (property = "cron" , column = "cron" ), @result (property = "quarzname" , column = "quarz_name" ), @result (property = "schedulerclass" , column = "scheduler_class" ), @result (property = "time" , column = "time" ) }) list<cronentity> queryall(); @update ( "update cron_table set cron=#{1} where id =#{0}" ) void updatecron(string id,string cron); } icronservice package com.example.dynamicquartz.serivce; import com.example.dynamicquartz.bean.cronentity; import java.util.list; /** * created with intellij idea. * packagename : com.example.dynamicquartz * author : wujw * date : 2018/9/9 19:54 * version : 1.0.0 * description : todo */ public interface icronservice{ void sendemail(string quartzname); void sendsms(string quartzname); cronentity findbycronid(string id ); void update(cronentity cronentity); list<cronentity> findall(); } cronserviceimpl package com.example.dynamicquartz.serivce; import com.example.dynamicquartz.bean.cronentity; import com.example.dynamicquartz.dao.cronmapper; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import java.util.list; /** * created with intellij idea. * packagename : com.example.dynamicquartz.serivce * author : wujw * date : 2018/9/9 19:55 * version : 1.0.0 * description : todo */ @service public class cronserviceimpl implements icronservice{ @autowired private cronmapper cronmapper; @override public void sendemail(string quartzname) { system.out.println(quartzname + " 正在发送邮件" ); } @override public void sendsms(string quartzname) { system.out.println(quartzname + "正在发送短信" ); } @override public cronentity findbycronid(string id) { return this .cronmapper.load(id); } @override public void update(cronentity cronentity) { this .cronmapper.insert(cronentity); } @override public list<cronentity> findall() { return this .cronmapper.queryall(); } } quartzmanager package com.example.dynamicquartz.serivce; import com.example.dynamicquartz.bean.cronentity; import org.quartz.*; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.configuration; import java.util.date; /** * created with intellij idea. * packagename : com.example.dynamicquartz * author : wujw * date : 2018/9/9 19:31 * version : 1.0.0 * description : todo */ @configuration public class quartzmanager { @autowired private scheduler scheduler; @autowired private icronservice icronservice; public void start(string id) throws schedulerexception { cronentity cronentity = icronservice.findbycronid(id); if (cronentity!= null ){ startjob(scheduler,cronentity.getquarzname(),cronentity.getcron(),cronentity.getschedulerclass()); } } private void startjob(scheduler scheduler,string name,string cron,string classname) throws schedulerexception { // 通过jobbuilder构建jobdetail实例,jobdetail规定只能是实现job接口的实例 // jobdetail 是具体job实例 class <job> jobclass = null ; try { //实例化具体的job任务 jobclass = ( class <job>) class .forname(classname); } catch (classnotfoundexception e) { e.printstacktrace(); } jobdetail jobdetail = jobbuilder.newjob(jobclass).withidentity(name, "group1" ).build(); // 基于表达式构建触发器 cronschedulebuilder cronschedulebuilder = cronschedulebuilder.cronschedule(cron); // crontrigger表达式触发器 继承于trigger // triggerbuilder 用于构建触发器实例 crontrigger crontrigger = triggerbuilder.newtrigger().withidentity(name, "group1" ) .withschedule(cronschedulebuilder).build(); scheduler.schedulejob(jobdetail, crontrigger); } /** * 获取job信息 * * @param name * @param group * @return * @throws schedulerexception */ public string getjobinfo(string name, string group) throws schedulerexception { triggerkey triggerkey = new triggerkey(name, group); crontrigger crontrigger = (crontrigger) scheduler.gettrigger(triggerkey); return string.format( "time:%s,state:%s" , crontrigger.getcronexpression(), scheduler.gettriggerstate(triggerkey).name()); } /** * 修改某个任务的执行时间 * * @param name * @param group * @param time * @return * @throws schedulerexception */ public boolean modifyjob(string name, string group, string time) throws schedulerexception { date date = null ; triggerkey triggerkey = new triggerkey(name, group); crontrigger crontrigger = (crontrigger) scheduler.gettrigger(triggerkey); string oldtime = crontrigger.getcronexpression(); if (!oldtime.equalsignorecase(time)) { cronschedulebuilder cronschedulebuilder = cronschedulebuilder.cronschedule(time); crontrigger trigger = triggerbuilder.newtrigger().withidentity(name, group) .withschedule(cronschedulebuilder).build(); date = scheduler.reschedulejob(triggerkey, trigger); } return date != null ; } /** * 暂停所有任务 * * @throws schedulerexception */ public void pausealljob() throws schedulerexception { scheduler.pauseall(); } /** * 暂停某个任务 * * @param name * @param group * @throws schedulerexception */ public void pausejob(string name, string group) throws schedulerexception { jobkey jobkey = new jobkey(name, group); jobdetail jobdetail = scheduler.getjobdetail(jobkey); if (jobdetail == null ) return ; scheduler.pausejob(jobkey); } /** * 恢复所有任务 * * @throws schedulerexception */ public void resumealljob() throws schedulerexception { scheduler.resumeall(); } /** * 恢复某个任务 * * @param name * @param group * @throws schedulerexception */ public void resumejob(string name, string group) throws schedulerexception { jobkey jobkey = new jobkey(name, group); jobdetail jobdetail = scheduler.getjobdetail(jobkey); if (jobdetail == null ) return ; scheduler.resumejob(jobkey); } /** * 删除某个任务 * * @param name * @param group * @throws schedulerexception */ public void deletejob(string name, string group) throws schedulerexception { jobkey jobkey = new jobkey(name, group); jobdetail jobdetail = scheduler.getjobdetail(jobkey); if (jobdetail == null ) return ; scheduler.deletejob(jobkey); } } 创建两个task类,分别执行不同的任务 public class emailtask implements job { private logger logger = loggerfactory.getlogger(emailtask. class ); @autowired private icronservice icronservice; @override public void execute(jobexecutioncontext jobexecutioncontext) throws jobexecutionexception { logger.info( "emailtask task start execute." ); //模拟任务执行 icronservice.sendemail(jobexecutioncontext.getjobdetail().getkey().getname()); } } public class smstask implements job { private logger logger = loggerfactory.getlogger(smstask. class ); @autowired private icronservice icronservice; @override public void execute(jobexecutioncontext jobexecutioncontext) throws jobexecutionexception { logger.info( "smstask task start execute." ); //模拟任务执行 icronservice.sendsms(jobexecutioncontext.getjobdetail().getkey().getname()); } } schedulercontroller @restcontroller @requestmapping ( "/scheduler" ) public class schedulercontroller{ @autowired quartzmanager quartzmanager; @requestmapping (value = "/start" , method = requestmethod.get) @responsebody public string start( @requestparam (name = "id" , defaultvalue = "" ) string id) { try { quartzmanager.start(id); } catch (schedulerexception e) { e.printstacktrace(); } return "启动成功" ; } } cron_table.sql create table `cron_table` ( `id` varchar( 11 ) collate utf8_bin not null , `user_id` varchar( 11 ) collate utf8_bin default null , `cron` varchar( 255 ) collate utf8_bin default null , `quarz_name` varchar( 255 ) collate utf8_bin default null , `scheduler_class` varchar( 255 ) collate utf8_bin default null , `time` datetime default null , primary key (`id`) ) engine=myisam default charset=utf8 collate=utf8_bin; -- ---------------------------- -- records of cron_table -- ---------------------------- insert into `cron_table` values ( '1' , 'aaa' , '0/5 * * * * ?' , '用户aaa' , 'com.example.dynamicquartz.job.smstask' , '2018-09-06 20:26:55' ); insert into `cron_table` values ( '2' , 'bbb' , '0/2 * * * * ?' , '用户bbb' , 'com.example.dynamicquartz.job.emailtask' , '2018-09-09 21:02:08' ); 测试结果 http: //localhost:8080/scheduler/start?id=1 http: //localhost:8080/scheduler/start?id=2 2018 - 09 - 09 21 : 05 : 08 , 386 [http-nio- 8080 -exec- 2 ] debug com.example.dynamicquartz.dao.cronmapper.load - ==> preparing: select * from cron_table where id = ? 2018 - 09 - 09 21 : 05 : 08 , 408 [http-nio- 8080 -exec- 2 ] debug com.example.dynamicquartz.dao.cronmapper.load - ==> parameters: 1 (string) 2018 - 09 - 09 21 : 05 : 08 , 427 [http-nio- 8080 -exec- 2 ] debug com.example.dynamicquartz.dao.cronmapper.load - <== total: 1 2018 - 09 - 09 21 : 05 : 10 , 036 [quartzscheduler_worker- 1 ] info com.example.dynamicquartz.job.smstask - smstask task start execute. 用户aaa正在发送短信 2018 - 09 - 09 21 : 05 : 11 , 581 [http-nio- 8080 -exec- 1 ] debug com.example.dynamicquartz.dao.cronmapper.load - ==> preparing: select * from cron_table where id = ? 2018 - 09 - 09 21 : 05 : 11 , 582 [http-nio- 8080 -exec- 1 ] debug com.example.dynamicquartz.dao.cronmapper.load - ==> parameters: 2 (string) 2018 - 09 - 09 21 : 05 : 11 , 584 [http-nio- 8080 -exec- 1 ] debug com.example.dynamicquartz.dao.cronmapper.load - <== total: 1 2018 - 09 - 09 21 : 05 : 12 , 013 [quartzscheduler_worker- 2 ] info com.example.dynamicquartz.job.emailtask - emailtask task start execute. 用户bbb 正在发送邮件 2018 - 09 - 09 21 : 05 : 14 , 002 [quartzscheduler_worker- 3 ] info com.example.dynamicquartz.job.emailtask - emailtask task start execute. 用户bbb 正在发送邮件 2018 - 09 - 09 21 : 05 : 15 , 001 [quartzscheduler_worker- 4 ] info com.example.dynamicquartz.job.smstask - smstask task start execute. 用户aaa正在发送短信 2018 - 09 - 09 21 : 05 : 16 , 001 [quartzscheduler_worker- 5 ] info com.example.dynamicquartz.job.emailtask - emailtask task start execute. 用户bbb 正在发送邮件 2018 - 09 - 09 21 : 05 : 18 , 001 [quartzscheduler_worker- 6 ] info com.example.dynamicquartz.job.emailtask - emailtask task start execute. 用户bbb 正在发送邮件 2018 - 09 - 09 21 : 05 : 20 , 000 [quartzscheduler_worker- 7 ] info com.example.dynamicquartz.job.smstask - smstask task start execute. 用户aaa正在发送短信 2018 - 09 - 09 21 : 05 : 20 , 001 [quartzscheduler_worker- 8 ] info com.example.dynamicquartz.job.emailtask - emailtask task start execute. 用户bbb 正在发送邮件 2018 - 09 - 09 21 : 05 : 22 , 001 [quartzscheduler_worker- 9 ] info com.example.dynamicquartz.job.emailtask - emailtask task start execute. 用户bbb 正在发送邮件 2018 - 09 - 09 21 : 05 : 24 , 000 [quartzscheduler_worker- 10 ] info com.example.dynamicquartz.job.emailtask - emailtask task start execute. 用户bbb 正在发送邮件 2018 - 09 - 09 21 : 05 : 25 , 001 [quartzscheduler_worker- 1 ] info com.example.dynamicquartz.job.smstask - smstask task start execute. 用户aaa正在发送短信 2018 - 09 - 09 21 : 05 : 26 , 000 [quartzscheduler_worker- 2 ] info com.example.dynamicquartz.job.emailtask - emailtask task start execute. 用户bbb 正在发送邮件 2018 - 09 - 09 21 : 05 : 28 , 001 [quartzscheduler_worker- 3 ] info com.example.dynamicquartz.job.emailtask - emailtask task start execute. |
可以看出,aaa的间隔时间时5s,bbb的间隔时间时2s.
到这里我们的测试已经一本完成了,接下来就是针对任务的更新,停止和启动了,只需要调用 quartzmanager 对应的方法即可,必要时更新一下数据库就好啦。
总结
以上所述是小编给大家介绍的quartz+spring boot实现动态管理定时任务,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/wujiwen/p/9615120.html