版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Quartz的任务的临时启动和暂停和恢复【转】在项目中需要手动启停某些服务,那么需要有一个控制这些任务的类。由于任务是有Quartz控制的,我们只需要通过Quartz的相关的API实现相关的功能即可。 Java代码 1. package com.easyway.app.quartz.mgr; 2. 3. import java.util.Date; 4. import java.util.List; 5. import java.ut
2、il.Map; 6. 7. import org.quartz.JobDataMap; 8. import org.quartz.JobDetail; 9. import org.quartz.JobKey; 10. import org.quartz.Scheduler; 11. import org.quartz.SchedulerException; 12. import o
3、rg.quartz.SchedulerFactory; 13. import org.quartz.Trigger; 14. import org.quartz.TriggerKey; 15. import org.quartz.impl.StdSchedulerFactory; 16. import org.quartz.impl.matchers.GroupMatcher; 17. 18. /* 19.
4、160;* 一个简单的quartz任务管理器 20. * author longgangbai 21. * 22. */ 23. public class QuartzScheduleMgr 24. private static Scheduler scheduler=getScheduler(); 25.
5、60; /* 26. * 创建一个调度对象 27. * return 28. * throws SchedulerException 29. */ 30. private
6、60;static Scheduler getScheduler() 31. SchedulerFactory sf = new StdSchedulerFactory(); 32. Schedu
7、ler scheduler=null; 33. try 34. scheduler = sf.getScheduler(); 35.
8、; catch (SchedulerException e) 36. e.printStackTrace(); 37.
9、; 38. return scheduler; 39. 40. public static Scheduler getInstanceScheduler() 41.
10、160; return scheduler; 42. 43. 44. /* 45. * 启动一个调度对象 46. * throws SchedulerException
11、160;47. */ 48. public void start() throws SchedulerException 49. 50. scheduler.start(); 51.
12、; 52. 53. /* 54. * 检查调度是否启动 55. * return 56. * throws SchedulerException 57.
13、0; */ 58. public boolean isStarted() throws SchedulerException 59. 60. return scheduler.isStarted(); 61.
14、 62. 63. /* 64. * 关闭调度信息 65. * throws SchedulerException 66. */ 67. public
15、 void shutdown() throws SchedulerException 68. scheduler.shutdown(); 69. 70. /* 71. *
16、添加调度的job信息 72. * param jobdetail 73. * param trigger 74. * return 75. * throws SchedulerException 76.
17、 */ 77. public Date scheduleJob(JobDetail jobdetail, Trigger trigger) 78. throws SchedulerException 79.
18、160; return scheduler.scheduleJob(jobdetail, trigger); 80. 81. /* 82. * 添加相关的触发器 83.
19、 * param trigger 84. * return 85. * throws SchedulerException 86. */ 87. public Date scheduleJob(Trigg
20、er trigger) throws SchedulerException 88. return scheduler.scheduleJob(trigger); 89. 90. /* 91. *
21、添加多个job任务 92. * param triggersAndJobs 93. * param replace 94. * throws SchedulerException 95. */
22、0;96. public void scheduleJobs(Map<JobDetail, List<Trigger>> triggersAndJobs, boolean replace) throws SchedulerException 97. 98.
23、 scheduler.scheduleJobs(triggersAndJobs, replace); 99. 100. /* 101. * 停止调度Job任务 102. * param triggerkey 103.
24、160; * return 104. * throws SchedulerException 105. */ 106. public boolean unscheduleJob(TriggerKey triggerkey) 107.
25、160; throws SchedulerException 108. return scheduler.unscheduleJob(triggerkey); 109. 110. 111.
26、/* 112. * 停止调度多个触发器相关的job 113. * param list 114. * return 115. * throws SchedulerException 116.
27、;*/ 117. public boolean unscheduleJobs(List<TriggerKey> triggerKeylist) throws SchedulerException 118. return scheduler.unscheduleJobs(triggerKeylist); 1
28、19. 120. /* 121. * 重新恢复触发器相关的job任务 122. * param triggerkey 123. * param trigger 124.
29、160; * return 125. * throws SchedulerException 126. */ 127. public Date rescheduleJob(TriggerKey triggerkey, Trigger trigger) 1
30、28. throws SchedulerException 129. return scheduler.rescheduleJob(triggerkey, trigger); 130. 131. /* 132.
31、160; * 添加相关的job任务 133. * param jobdetail 134. * param flag 135. * throws SchedulerException 136. */ 137.
32、0; public void addJob(JobDetail jobdetail, boolean flag) 138. throws SchedulerException 139.
33、 scheduler.addJob(jobdetail, flag); 140. 141. 142. /* 143. * 删除相关的job任务 144. * param jobkey 145.
34、160; * return 146. * throws SchedulerException 147. */ 148. public boolean deleteJob(JobKey jobkey) throws SchedulerException 149.
35、160; return scheduler.deleteJob(jobkey); 150. 151. 152. /* 153. * 删除相关的多个job任务 154. * param
36、 jobKeys 155. * return 156. * throws SchedulerException 157. */ 158. public boolean deleteJobs(List<Job
37、Key> jobKeys) 159. throws SchedulerException 160. return scheduler.deleteJobs(jobKeys); 161. 162. /* 163.
38、 * 164. * param jobkey 165. * throws SchedulerException 166. */ 167. public void triggerJob(Job
39、Key jobkey) throws SchedulerException 168. scheduler.triggerJob(jobkey); 169. 170. /* 171. *
40、0; 172. * param jobkey 173. * param jobdatamap 174. * throws SchedulerException 175. */ 176. pu
41、blic void triggerJob(JobKey jobkey, JobDataMap jobdatamap) 177. throws SchedulerException 178. scheduler
42、.triggerJob(jobkey, jobdatamap); 179. 180. /* 181. * 停止一个job任务 182. * param jobkey 183. * throws
43、60;SchedulerException 184. */ 185. public void pauseJob(JobKey jobkey) throws SchedulerException 186. scheduler.pauseJob(jobkey)
44、; 187. 188. /* 189. * 停止多个job任务 190. * param groupmatcher 191. * throws SchedulerException 192
45、. */ 193. public void pauseJobs(GroupMatcher<JobKey> groupmatcher) 194. throws SchedulerException
46、; 195. scheduler.pauseJobs(groupmatcher); 196. 197. /* 198. * 停止使用相关的触发器 199. * param trigg
47、erkey 200. * throws SchedulerException 201. */ 202. public void pauseTrigger(TriggerKey triggerkey) 203.
48、 throws SchedulerException 204. scheduler.pauseTrigger(triggerkey); 205. 206. 207. public void p
49、auseTriggers(GroupMatcher<TriggerKey> groupmatcher) 208. throws SchedulerException 209. scheduler.pauseTriggers(groupmatcher
50、); 210. 211. /* 212. * 恢复相关的job任务 213. * param jobkey 214. * throws SchedulerException 215.
51、160; */ 216. public void resumeJob(JobKey jobkey) throws SchedulerException 217. scheduler.pauseJob(jobkey); 218.
52、60; 219. 220. public void resumeJobs(GroupMatcher<JobKey> matcher) 221. throws SchedulerException
53、; 222. scheduler.resumeJobs(matcher); 223. 224. 225. public void resumeTrigger(TriggerKey triggerkey) 226. &
54、#160; throws SchedulerException 227. scheduler.resumeTrigger(triggerkey); 228. 229. 230.
55、; public void resumeTriggers(GroupMatcher<TriggerKey> groupmatcher) 231. throws SchedulerException 232. 233.
56、 scheduler.resumeTriggers(groupmatcher); 234. 235. /* 236. * 暂停调度中所有的job任务 237. * throws Schedule
57、rException 238. */ 239. public void pauseAll() throws SchedulerException 240. 241. scheduler.pauseAll();
58、; 242. 243. /* 244. * 恢复调度中所有的job的任务 245. * throws SchedulerException 246. */ 247.
59、60;public void resumeAll() throws SchedulerException 248. 249. scheduler.resumeAll(); 250. 251.
60、252. 253. 254. 创建一个Job任务:Java代码 1. /* 2. * Copyright 2005 - 2009 Terracotta, Inc. 3. * 4. * Licensed&
61、#160;under the Apache License, Version 2.0 (the "License"); you may not 5. * use this file except in compliance with the License. You may obtain a copy
62、160; 6. * of the License at 7. * 8. * /licenses/LICENSE-2.0 9. * 10. * Unless required by applicable law or agreed
63、60;to in writing, software 11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12. * WARRANTIES OR CONDITIONS OF ANY KIND,&
64、#160;either express or implied. See the 13. * License for the specific language governing permissions and limitations 14. * under the License. 15. * 16.
65、*/ 17. 18. package com.easyway.app.quartz.mgr; 19. 20. import java.util.Date; 21. 22. import org.slf4j.Logger; 23. import org.slf4j.LoggerFactory; 24. import org.quartz.Job;
66、;25. import org.quartz.JobExecutionContext; 26. import org.quartz.JobExecutionException; 27. 28. /* 29. * 一个简单的quartz调用job 30. * author longgangbai 31. * 32. */ 33. public class
67、0;HelloJob implements Job 34. 35. private static Logger _log = LoggerFactory.getLogger(HelloJob.class); 36. 37. public HelloJob() 38.
68、60; 39. 40. public void execute(JobExecutionContext context) 41. throws JobExecutionException 42.
69、;_("Hello World! - " + new Date(); 43. 44. 45. 创建触发器和调用相关的JobJava代码 1. /* 2. * Copyright 2005 - 2009 Terracotta,
70、0;Inc. 3. * 4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5. * use this file except in compliance with t
71、he License. You may obtain a copy 6. * of the License at 7. * 8. * /licenses/LICENSE-2.0 9. * 10. * Unless
72、;required by applicable law or agreed to in writing, software 11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12. *
73、0;WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13. * License for the specific language governing permissions and limitations 14. * un
74、der the License. 15. * 16. */ 17. 18. package com.easyway.app.quartz.mgr; 19. 20. import static org.quartz.DateBuilder.evenMinuteDate; 21. import static org.quartz.JobBuilder.newJo
75、b; 22. import static org.quartz.TriggerBuilder.newTrigger; 23. 24. import java.util.Date; 25. 26. import org.quartz.JobDetail; 27. import org.quartz.Scheduler; 28. import org.quartz.Trigger;&
76、#160; 29. import org.slf4j.Logger; 30. import org.slf4j.LoggerFactory; 31. 32. /* 33. * 一个简单的测试quartz任务管理器测试类 34. * author longgangbai 35. * 36. */ 37. public class Quartz
77、ScheduleMain 38. 39. 40. /* 41. * 42. * throws Exception 43. */ 44.
78、; public void run() throws Exception 45. Logger log = LoggerFactory.getLogger(QuartzScheduleMain.class); 46. 47. log.i
79、nfo("- Initializing -"); 48. 49. / First we must get a reference to a scheduler 50. /从调度管理器中获取调度对象 &
80、#160;51. Scheduler sched = QuartzScheduleMgr.getInstanceScheduler(); 52. ("- Initialization Complete -"); 53. 54.
81、160; / computer a time that is on the next round minute 55. Date runTime = evenMinuteDate(new Date(); 56. 57.
82、60; ("- Scheduling Job -"); 58. 59. / define the job and tie it to our HelloJob class 60
83、. /创建相关的job信息 61. JobDetail job = newJob(HelloJob.class) 62. .withIdentity("job1",
84、60;"group1") 63. .build(); 64. 65. / Trigger the job to run
85、0;on the next round minute 66. /创建一个触发器的名称 67. Trigger trigger = newTrigger() 68.
86、 .withIdentity("trigger1", "group1") 69. .startAt(runTime) 70. .build(); 71.
87、0; 72. / Tell quartz to schedule the job using our trigger 73. /设置调度相关的Job 74.
88、; sched.scheduleJob(job, trigger); 75. (job.getKey() + " will run at: " + runTime); 76. 77.
89、0; / Start up the scheduler (nothing can actually run until the 78. / scheduler has been started) 79.
90、; /启动调度任务 80. sched.start(); 81. 82. ("- Started Scheduler -"); 83. 84. &
91、#160; try 85. Thread.sleep(25L * 1000L); 86. / executing. 87.
92、; catch (Exception e) 88. 89. /暂时停止Job任务开始执行 90. (&
93、quot;-pauseJob. -"); 91. sched.pauseJob(job.getKey(); 92. 93. try 94. Thread.sleep(10L
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年国际足球赛事场地租赁合同
- 2024年建筑施工劳务承包简约合同样本
- 2024桩基础工程专业分包合同模板
- 2024代理合同样式
- 2024技术参股合作协议书
- 2024版药品代理合同
- 二手房交易合同
- 店面承租协议书范本
- 2024项目开发全过程专项法律服务合同
- 2024常用合作合同范本
- 学生对教师评价表(共8页)
- 批发零售大个体 E204-3批发和零售业产业活动单位(个体经营户)商品销售和库存
- (完整版)青年就业创业见习基地汇报材料(完整版)
- 月光(羽泉)原版五线谱钢琴谱正谱乐谱.docx
- 660MW机组空预器声波吹灰器可行性研究报告最新(精华版)
- 控制柜安装施工方案
- 动车组火灾检测(报警)系统
- 水面垃圾自动打捞船的设计 (全套图纸)
- 烟草企业安全生产标准化 规范
- 装饰施工技术标准及要求
- 2018秋七年级虎外考试卷英语试卷
评论
0/150
提交评论