在项目中需要手动启停某些服务,那么需要有一个控制这些任务的类。由于任务是有Quartz控制的,我们只需要通过Quartz的相关的API实现相关的功能即可。

  1. package com.easyway.app.quartz.mgr;
  2. import java.util.Date;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.quartz.JobDataMap;
  6. import org.quartz.JobDetail;
  7. import org.quartz.JobKey;
  8. import org.quartz.Scheduler;
  9. import org.quartz.SchedulerException;
  10. import org.quartz.SchedulerFactory;
  11. import org.quartz.Trigger;
  12. import org.quartz.TriggerKey;
  13. import org.quartz.impl.StdSchedulerFactory;
  14. import org.quartz.impl.matchers.GroupMatcher;
  15. /**
  16. * 一个简单的quartz任务管理器
  17. * @author longgangbai
  18. *
  19. */
  20. public class QuartzScheduleMgr {
  21. private static  Scheduler scheduler=getScheduler();
  22. /**
  23. * 创建一个调度对象
  24. * @return
  25. * @throws SchedulerException
  26. */
  27. private static Scheduler getScheduler() {
  28. SchedulerFactory sf = new StdSchedulerFactory();
  29. Scheduler scheduler=null;
  30. try {
  31. scheduler = sf.getScheduler();
  32. } catch (SchedulerException e) {
  33. e.printStackTrace();
  34. }
  35. return scheduler;
  36. }
  37. public static Scheduler getInstanceScheduler(){
  38. return scheduler;
  39. }
  40. /**
  41. * 启动一个调度对象
  42. * @throws SchedulerException
  43. */
  44. public  void start() throws SchedulerException
  45. {
  46. scheduler.start();
  47. }
  48. /**
  49. * 检查调度是否启动
  50. * @return
  51. * @throws SchedulerException
  52. */
  53. public  boolean isStarted() throws SchedulerException
  54. {
  55. return scheduler.isStarted();
  56. }
  57. /**
  58. * 关闭调度信息
  59. * @throws SchedulerException
  60. */
  61. public  void shutdown() throws SchedulerException   {
  62. scheduler.shutdown();
  63. }
  64. /**
  65. * 添加调度的job信息
  66. * @param jobdetail
  67. * @param trigger
  68. * @return
  69. * @throws SchedulerException
  70. */
  71. public  Date scheduleJob(JobDetail jobdetail, Trigger trigger)
  72. throws SchedulerException{
  73. return scheduler.scheduleJob(jobdetail, trigger);
  74. }
  75. /**
  76. * 添加相关的触发器
  77. * @param trigger
  78. * @return
  79. * @throws SchedulerException
  80. */
  81. public  Date scheduleJob(Trigger trigger) throws SchedulerException{
  82. return scheduler.scheduleJob(trigger);
  83. }
  84. /**
  85. * 添加多个job任务
  86. * @param triggersAndJobs
  87. * @param replace
  88. * @throws SchedulerException
  89. */
  90. public  void scheduleJobs(Map<JobDetail, List<Trigger>> triggersAndJobs, boolean replace) throws SchedulerException
  91. {
  92. scheduler.scheduleJobs(triggersAndJobs, replace);
  93. }
  94. /**
  95. * 停止调度Job任务
  96. * @param triggerkey
  97. * @return
  98. * @throws SchedulerException
  99. */
  100. public  boolean unscheduleJob(TriggerKey triggerkey)
  101. throws SchedulerException{
  102. return scheduler.unscheduleJob(triggerkey);
  103. }
  104. /**
  105. * 停止调度多个触发器相关的job
  106. * @param list
  107. * @return
  108. * @throws SchedulerException
  109. */
  110. public  boolean unscheduleJobs(List<TriggerKey> triggerKeylist) throws SchedulerException{
  111. return scheduler.unscheduleJobs(triggerKeylist);
  112. }
  113. /**
  114. * 重新恢复触发器相关的job任务
  115. * @param triggerkey
  116. * @param trigger
  117. * @return
  118. * @throws SchedulerException
  119. */
  120. public  Date rescheduleJob(TriggerKey triggerkey, Trigger trigger)
  121. throws SchedulerException{
  122. return scheduler.rescheduleJob(triggerkey, trigger);
  123. }
  124. /**
  125. * 添加相关的job任务
  126. * @param jobdetail
  127. * @param flag
  128. * @throws SchedulerException
  129. */
  130. public  void addJob(JobDetail jobdetail, boolean flag)
  131. throws SchedulerException   {
  132. scheduler.addJob(jobdetail, flag);
  133. }
  134. /**
  135. * 删除相关的job任务
  136. * @param jobkey
  137. * @return
  138. * @throws SchedulerException
  139. */
  140. public  boolean deleteJob(JobKey jobkey) throws SchedulerException{
  141. return scheduler.deleteJob(jobkey);
  142. }
  143. /**
  144. * 删除相关的多个job任务
  145. * @param jobKeys
  146. * @return
  147. * @throws SchedulerException
  148. */
  149. public     boolean deleteJobs(List<JobKey> jobKeys)
  150. throws SchedulerException{
  151. return scheduler.deleteJobs(jobKeys);
  152. }
  153. /**
  154. *
  155. * @param jobkey
  156. * @throws SchedulerException
  157. */
  158. public  void triggerJob(JobKey jobkey) throws SchedulerException    {
  159. scheduler.triggerJob(jobkey);
  160. }
  161. /**
  162. *
  163. * @param jobkey
  164. * @param jobdatamap
  165. * @throws SchedulerException
  166. */
  167. public  void triggerJob(JobKey jobkey, JobDataMap jobdatamap)
  168. throws SchedulerException   {
  169. scheduler.triggerJob(jobkey, jobdatamap);
  170. }
  171. /**
  172. * 停止一个job任务
  173. * @param jobkey
  174. * @throws SchedulerException
  175. */
  176. public  void pauseJob(JobKey jobkey) throws SchedulerException  {
  177. scheduler.pauseJob(jobkey);
  178. }
  179. /**
  180. * 停止多个job任务
  181. * @param groupmatcher
  182. * @throws SchedulerException
  183. */
  184. public  void pauseJobs(GroupMatcher<JobKey> groupmatcher)
  185. throws SchedulerException   {
  186. scheduler.pauseJobs(groupmatcher);
  187. }
  188. /**
  189. * 停止使用相关的触发器
  190. * @param triggerkey
  191. * @throws SchedulerException
  192. */
  193. public  void pauseTrigger(TriggerKey triggerkey)
  194. throws SchedulerException   {
  195. scheduler.pauseTrigger(triggerkey);
  196. }
  197. public  void pauseTriggers(GroupMatcher<TriggerKey> groupmatcher)
  198. throws SchedulerException   {
  199. scheduler.pauseTriggers(groupmatcher);
  200. }
  201. /**
  202. * 恢复相关的job任务
  203. * @param jobkey
  204. * @throws SchedulerException
  205. */
  206. public  void resumeJob(JobKey jobkey) throws SchedulerException {
  207. scheduler.pauseJob(jobkey);
  208. }
  209. public  void resumeJobs(GroupMatcher<JobKey> matcher)
  210. throws SchedulerException   {
  211. scheduler.resumeJobs(matcher);
  212. }
  213. public  void resumeTrigger(TriggerKey triggerkey)
  214. throws SchedulerException   {
  215. scheduler.resumeTrigger(triggerkey);
  216. }
  217. public  void resumeTriggers(GroupMatcher<TriggerKey>  groupmatcher)
  218. throws SchedulerException
  219. {
  220. scheduler.resumeTriggers(groupmatcher);
  221. }
  222. /**
  223. * 暂停调度中所有的job任务
  224. * @throws SchedulerException
  225. */
  226. public  void pauseAll() throws SchedulerException
  227. {
  228. scheduler.pauseAll();
  229. }
  230. /**
  231. * 恢复调度中所有的job的任务
  232. * @throws SchedulerException
  233. */
  234. public  void resumeAll() throws SchedulerException
  235. {
  236. scheduler.resumeAll();
  237. }
  238. }

创建一个Job任务:

  1. /*
  2. * Copyright 2005 - 2009 Terracotta, Inc.
  3. *
  4. * Licensed 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
  6. * of the License at
  7. *
  8. *   http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. *
  16. */
  17. package com.easyway.app.quartz.mgr;
  18. import java.util.Date;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.quartz.Job;
  22. import org.quartz.JobExecutionContext;
  23. import org.quartz.JobExecutionException;
  24. /**
  25. * 一个简单的quartz调用job
  26. * @author longgangbai
  27. *
  28. */
  29. public class HelloJob implements Job {
  30. private static Logger _log = LoggerFactory.getLogger(HelloJob.class);
  31. public HelloJob() {
  32. }
  33. public void execute(JobExecutionContext context)
  34. throws JobExecutionException {
  35. _log.info("Hello World! - " + new Date());
  36. }
  37. }

创建触发器和调用相关的Job

  1. /*
  2. * Copyright 2005 - 2009 Terracotta, Inc.
  3. *
  4. * Licensed 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
  6. * of the License at
  7. *
  8. *   http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. *
  16. */
  17. package com.easyway.app.quartz.mgr;
  18. import static org.quartz.DateBuilder.evenMinuteDate;
  19. import static org.quartz.JobBuilder.newJob;
  20. import static org.quartz.TriggerBuilder.newTrigger;
  21. import java.util.Date;
  22. import org.quartz.JobDetail;
  23. import org.quartz.Scheduler;
  24. import org.quartz.Trigger;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. /**
  28. * 一个简单的测试quartz任务管理器测试类
  29. * @author longgangbai
  30. *
  31. */
  32. public class QuartzScheduleMain {
  33. /**
  34. *
  35. * @throws Exception
  36. */
  37. public void run() throws Exception {
  38. Logger log = LoggerFactory.getLogger(QuartzScheduleMain.class);
  39. log.info("------- Initializing ----------------------");
  40. // First we must get a reference to a scheduler
  41. //从调度管理器中获取调度对象
  42. Scheduler sched = QuartzScheduleMgr.getInstanceScheduler();
  43. log.info("------- Initialization Complete -----------");
  44. // computer a time that is on the next round minute
  45. Date runTime = evenMinuteDate(new Date());
  46. log.info("------- Scheduling Job  -------------------");
  47. // define the job and tie it to our HelloJob class
  48. //创建相关的job信息
  49. JobDetail job = newJob(HelloJob.class)
  50. .withIdentity("job1", "group1")
  51. .build();
  52. // Trigger the job to run on the next round minute
  53. //创建一个触发器的名称
  54. Trigger trigger = newTrigger()
  55. .withIdentity("trigger1", "group1")
  56. .startAt(runTime)
  57. .build();
  58. // Tell quartz to schedule the job using our trigger
  59. //设置调度相关的Job
  60. sched.scheduleJob(job, trigger);
  61. log.info(job.getKey() + " will run at: " + runTime);
  62. // Start up the scheduler (nothing can actually run until the
  63. // scheduler has been started)
  64. //启动调度任务
  65. sched.start();
  66. log.info("------- Started Scheduler -----------------");
  67. try {
  68. Thread.sleep(25L * 1000L);
  69. // executing...
  70. } catch (Exception e) {
  71. }
  72. //暂时停止Job任务开始执行
  73. log.info("-------pauseJob.. -------------");
  74. sched.pauseJob(job.getKey());
  75. try {
  76. Thread.sleep(10L * 1000L);
  77. } catch (Exception e) {
  78. }
  79. log.info("------- resumeJob... -------------");
  80. //恢复Job任务开始执行
  81. sched.resumeJob(job.getKey());
  82. try {
  83. Thread.sleep(10L * 1000L);
  84. // executing...
  85. } catch (Exception e) {
  86. }
  87. // wait long enough so that the scheduler as an opportunity to
  88. // run the job!
  89. log.info("------- Waiting 65 seconds... -------------");
  90. try {
  91. // wait 65 seconds to show job
  92. Thread.sleep(65L * 1000L);
  93. // executing...
  94. } catch (Exception e) {
  95. }
  96. // shut down the scheduler
  97. log.info("------- Shutting Down ---------------------");
  98. sched.shutdown(true);
  99. log.info("------- Shutdown Complete -----------------");
  100. }
  101. public static void main(String[] args) throws Exception {
  102. QuartzScheduleMain example = new QuartzScheduleMain();
  103. example.run();
  104. }
  105. }

Quartz的任务的临时启动和暂停和恢复的更多相关文章

  1. Spring 3整合Quartz 2实现定时任务三:动态暂停 恢复 修改和删除任务

    前面我们已经完成了spring 3和quartz 2的整合以及动态添加定时任务,我们接着来完善它,使之能支持更多的操作,例如暂停.恢复.修改等. 在动态添加定时任务中其实已经涉及到了其中的一些代码,这 ...

  2. 【Android Developers Training】 16. 暂停和恢复一个Activity

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. 使用 suspend 和 resume 暂停和恢复线程

    suspend 和 resume 的使用 在 Thread 类中有这样两个方法:suspend 和 resume,这两个方法是成对出现的. suspend() 方法的作用是将一个线程挂起(暂停), r ...

  4. 利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复(转)

    利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复 打印过程可能很长,这时候有可能需要暂停下来做一些事情,然后回来继续接着打印 打印过程中有2个线程:一个是程序运行的主线程, ...

  5. 暂停和恢复Activity Android

    暂停和恢复Activity(Pausing and Resuming an Activity) 在正常的应用程序使用,前台activity有时会被其他可视化组件遮挡,从而 造成activity的暂停. ...

  6. WPF控制动画开始、停止、暂停和恢复

    1.闲言 好久也没更新一博客了,自己有点发懒,同时确实这几个月来也有点忙.风机监测软件,项目中,有这样一个小需求:正常风机在旋转的时候,上位机软要做一个风机的图片,让它不停地旋转,一但检测到下面风机停 ...

  7. Java/Android倒计时(开始,暂停,恢复,停止)

    由于要做暂停和恢复,这里我就没有使用Android的CountDownTimer,而是用了Java的Timer.所以,这个方法在java肯定是通用.我也外加了Android独有的Service,有些计 ...

  8. CALayer的上动画的暂停和恢复

    CHENYILONG Blog CALayer上动画的暂停和恢复 #pragma mark 暂停CALayer的动画-(void)pauseLayer:(CALayer*)layer{CFTimeIn ...

  9. Linux暂停和恢复进程

    Linux暂停和恢复进程 kill -STOP 1234 将该进程暂停. 如果要让它恢复到后台,用kill -CONT 1234 (很多在前台运行的程序这样是不行的) 如果要恢复到前台,请在当时运行该 ...

随机推荐

  1. [JAVA][RCP]Clean project之后报错:java.lang.RuntimeException: No application id has been found.

    Clean了一下Project,然后就报了如下错误 !ENTRY com.release.nattable.well_analysis 2 0 2015-11-20 17:04:44.609 !MES ...

  2. android----sqlite中的 query() 参数分析

    public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, Strin ...

  3. 【iOS】屏幕旋转,屏幕自适应方向变化

    1. iOS有四个方向的旋转,为了保证自己的代码能够支持旋转,我们必须首先处理一个函数: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInter ...

  4. 收起虚拟键盘的各种方法 -- IOS

    使用虚拟键盘来输入资讯,是 iOS 的重要互动方式之一,虚拟键盘通常会自动出现在可以编辑的 UITextField 或是 UITextView 的编辑事件中,叫出键盘固然容易,但是要把它收起来,可就没 ...

  5. 分布式文件系统 - FastDFS

    分布式文件系统 - FastDFS 别问我在哪里 也许我早已不是我自己,别问我在哪里,我一直在这里. 突然不知道说些什么了... 初识 FastDFS 记得那是我刚毕业后进入的第一家公司,一个技术小白 ...

  6. ASP.NET Web - 回送

    如果希望把更改事件立即传送给服务器,可以把AutoPostback属性设置为true.这样就会使用客户端的JavaScript把窗体数据立即提交给服务器.当然,网络通信量也会增加.使用这个功能时要小心 ...

  7. Android keyevent 中的各个值

    Android keyevent 中的各个值,在使用adb shell input 的时候用得到. 是从http://blog.csdn.net/huiguixian/article/details/ ...

  8. ascx aspx ashx asmx 文件的作用

    ascx aspx ashx asmx 文件的作用 ascx: Ascx 是给予Web的用户控件(UserControl),一般是用来重用的,不能直接被访问只能插入aspx页面呈现.头部文件<% ...

  9. C#: Create a WebRequest with HTTPClient

    http://www.cnblogs.com/shanyou/archive/2012/03/21/2410739.html http://msdn.microsoft.com/zh-cn/libra ...

  10. request 路径随笔

    1. 路劲可分为 绝对路径 和 相对路径 2. 绝对路径 (开头带"/") 前端: http://localhost:8080/myWebApp/user/login.jsp /m ...