Quartz的任务的临时启动和暂停和恢复

来源:互联网 发布:邮箱客户端 知乎 编辑:程序博客网 时间:2024/05/02 06:11

转自:http://topmanopensource.iteye.com/blog/1125753

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

 

 

 

创建一个Job任务:

Java代码  收藏代码
  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.   
  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;  
  25. import org.quartz.JobExecutionContext;  
  26. import org.quartz.JobExecutionException;  
  27.   
  28. /** 
  29.  * 一个简单的quartz调用job 
  30.  * @author longgangbai 
  31.  * 
  32.  */  
  33. public class HelloJob implements Job {  
  34.   
  35.     private static Logger _log = LoggerFactory.getLogger(HelloJob.class);  
  36.   
  37.     public HelloJob() {  
  38.     }  
  39.   
  40.     public void execute(JobExecutionContext context)  
  41.         throws JobExecutionException {  
  42.         _log.info("Hello World! - " + new Date());  
  43.     }  
  44.   
  45. }  

 

 

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

Java代码  收藏代码
  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.   
  18. package com.easyway.app.quartz.mgr;  
  19.   
  20. import static org.quartz.DateBuilder.evenMinuteDate;  
  21. import static org.quartz.JobBuilder.newJob;  
  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;  
  29. import org.slf4j.Logger;  
  30. import org.slf4j.LoggerFactory;  
  31.   
  32. /** 
  33.  * 一个简单的测试quartz任务管理器测试类 
  34.  * @author longgangbai 
  35.  * 
  36.  */  
  37. public class QuartzScheduleMain {  
  38.   
  39.       
  40.     /** 
  41.      *  
  42.      * @throws Exception 
  43.      */  
  44.     public void run() throws Exception {  
  45.         Logger log = LoggerFactory.getLogger(QuartzScheduleMain.class);  
  46.   
  47.         log.info("------- Initializing ----------------------");  
  48.   
  49.         // First we must get a reference to a scheduler  
  50.         //从调度管理器中获取调度对象  
  51.         Scheduler sched = QuartzScheduleMgr.getInstanceScheduler();  
  52.         log.info("------- Initialization Complete -----------");  
  53.   
  54.         // computer a time that is on the next round minute  
  55.         Date runTime = evenMinuteDate(new Date());  
  56.   
  57.         log.info("------- Scheduling Job  -------------------");  
  58.   
  59.         // define the job and tie it to our HelloJob class  
  60.         //创建相关的job信息  
  61.         JobDetail job = newJob(HelloJob.class)  
  62.             .withIdentity("job1""group1")  
  63.             .build();  
  64.           
  65.         // Trigger the job to run on the next round minute  
  66.         //创建一个触发器的名称  
  67.         Trigger trigger = newTrigger()  
  68.             .withIdentity("trigger1""group1")  
  69.             .startAt(runTime)  
  70.             .build();  
  71.           
  72.         // Tell quartz to schedule the job using our trigger  
  73.         //设置调度相关的Job  
  74.         sched.scheduleJob(job, trigger);  
  75.         log.info(job.getKey() + " will run at: " + runTime);    
  76.   
  77.         // Start up the scheduler (nothing can actually run until the   
  78.         // scheduler has been started)  
  79.         //启动调度任务  
  80.         sched.start();  
  81.   
  82.         log.info("------- Started Scheduler -----------------");  
  83.   
  84.         try {  
  85.             Thread.sleep(25L * 1000L);   
  86.             // executing...  
  87.         } catch (Exception e) {  
  88.         }  
  89.         //暂时停止Job任务开始执行  
  90.         log.info("-------pauseJob.. -------------");  
  91.         sched.pauseJob(job.getKey());  
  92.           
  93.         try {  
  94.             Thread.sleep(10L * 1000L);   
  95.         } catch (Exception e) {  
  96.         }  
  97.         log.info("------- resumeJob... -------------");  
  98.         //恢复Job任务开始执行  
  99.         sched.resumeJob(job.getKey());  
  100.         try {  
  101.             Thread.sleep(10L * 1000L);   
  102.             // executing...  
  103.         } catch (Exception e) {  
  104.         }  
  105.           
  106.           
  107.         // wait long enough so that the scheduler as an opportunity to   
  108.         // run the job!  
  109.         log.info("------- Waiting 65 seconds... -------------");  
  110.         try {  
  111.             // wait 65 seconds to show job  
  112.             Thread.sleep(65L * 1000L);   
  113.             // executing...  
  114.         } catch (Exception e) {  
  115.         }  
  116.   
  117.         // shut down the scheduler  
  118.         log.info("------- Shutting Down ---------------------");  
  119.         sched.shutdown(true);  
  120.         log.info("------- Shutdown Complete -----------------");  
  121.     }  
  122.   
  123.     public static void main(String[] args) throws Exception {  
  124.   
  125.         QuartzScheduleMain example = new QuartzScheduleMain();  
  126.         example.run();  
  127.   
  128.     }  
  129.   
  130. }