任務調度開源框架Quartz動態添加、修改和刪除定時任務


  Quartz 是個開源的作業調度框架,為在 Java 應用程序中進行作業調度提供了簡單卻強大的機制。Quartz框架包含了調度器監聽、作業和觸發器監聽。你可以配置作業和觸發器監聽為全局監聽或者是特定於作業和觸發器的監聽。Quartz 允許開發人員根據時間間隔(或天)來調度作業。它實現了作業和觸發器的多對多關系,還能把多個作業與不同的觸發器關聯。整合了 Quartz 的應用程序可以重用來自不同事件的作業,還可以為一個事件組合多個作業。並且還能和Spring配置整合使用。Quartz在功能上遠遠超越了JDK自帶的Timer,很好很強大!好啦,直接上代碼:

/** 
 * @Description:  
 * 
 * @Title: QuartzManager.java 
 * @Package com.joyce.quartz 
 * @Copyright: Copyright (c) 2014 
 *  
 * @version V2.0 
 */    
  
import org.quartz.CronTrigger;  
import org.quartz.JobDetail;  
import org.quartz.Scheduler;  
import org.quartz.SchedulerFactory;  
import org.quartz.impl.StdSchedulerFactory;  
  
/** 
 * @Description: 定時任務管理類 
 *  
 * @ClassName: QuartzManager 
 * @Copyright: Copyright (c) 2014 
 *  
 * @author Comsys-LZP 
 * @date 2014-6-26 下午03:15:52 
 * @version V2.0 
 */  
public class QuartzManager {  
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME"; private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME"; /** * @Description: 添加一個定時任務,使用默認的任務組名,觸發器名,觸發器組名 * * @param jobName * 任務名 * @param cls * 任務 * @param time * 時間設置,參考quartz說明文檔 * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:47:44 * @version V2.0 */ @SuppressWarnings("unchecked") public static void addJob(String jobName, Class cls, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);// 任務名,任務組,任務執行類 // 觸發器 CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// 觸發器名,觸發器組 trigger.setCronExpression(time);// 觸發器時間設定 sched.scheduleJob(jobDetail, trigger); // 啟動 if (!sched.isShutdown()) { sched.start(); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 添加一個定時任務 * * @param jobName * 任務名 * @param jobGroupName * 任務組名 * @param triggerName * 觸發器名 * @param triggerGroupName * 觸發器組名 * @param jobClass * 任務 * @param time * 時間設置,參考quartz說明文檔 * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:48:15 * @version V2.0 */ @SuppressWarnings("unchecked") public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任務名,任務組,任務執行類 // 觸發器 CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 觸發器名,觸發器組 trigger.setCronExpression(time);// 觸發器時間設定 sched.scheduleJob(jobDetail, trigger); } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 修改一個任務的觸發時間(使用默認的任務組名,觸發器名,觸發器組名) * * @param jobName * @param time * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:49:21 * @version V2.0 */ @SuppressWarnings("unchecked") public static void modifyJobTime(String jobName, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName,TRIGGER_GROUP_NAME); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { JobDetail jobDetail = sched.getJobDetail(jobName,JOB_GROUP_NAME); Class objJobClass = jobDetail.getJobClass(); removeJob(jobName); addJob(jobName, objJobClass, time); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 修改一個任務的觸發時間 * * @param triggerName * @param triggerGroupName * @param time * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:49:37 * @version V2.0 */ public static void modifyJobTime(String triggerName, String triggerGroupName, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName,triggerGroupName); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { CronTrigger ct = (CronTrigger) trigger; // 修改時間 ct.setCronExpression(time); // 重啟觸發器 sched.resumeTrigger(triggerName, triggerGroupName); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 移除一個任務(使用默認的任務組名,觸發器名,觸發器組名) * * @param jobName * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:49:51 * @version V2.0 */ public static void removeJob(String jobName) { try { Scheduler sched = gSchedulerFactory.getScheduler(); sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 停止觸發器 sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除觸發器 sched.deleteJob(jobName, JOB_GROUP_NAME);// 刪除任務 } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 移除一個任務 * * @param jobName * @param jobGroupName * @param triggerName * @param triggerGroupName * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:50:01 * @version V2.0 */ public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) { try { Scheduler sched = gSchedulerFactory.getScheduler(); sched.pauseTrigger(triggerName, triggerGroupName);// 停止觸發器 sched.unscheduleJob(triggerName, triggerGroupName);// 移除觸發器 sched.deleteJob(jobName, jobGroupName);// 刪除任務 } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description:啟動所有定時任務 * * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:50:18 * @version V2.0 */ public static void startJobs() { try { Scheduler sched = gSchedulerFactory.getScheduler(); sched.start(); } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description:關閉所有定時任務 * * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:50:26 * @version V2.0 */ public static void shutdownJobs() { try { Scheduler sched = gSchedulerFactory.getScheduler(); if (!sched.isShutdown()) { sched.shutdown(); } } catch (Exception e) { throw new RuntimeException(e); } } }

以上就是quartz任務調度對於任務的常用操作,封裝起來以便在外部調用!這樣我們就需要任務的執行了:

/** 
 * @Description:  
 * 
 * @Title: QuartzJob.java 
 * @Package com.joyce.quartz 
 * @Copyright: Copyright (c) 2014 
 * 
 * @author Comsys-LZP 
 * @date 2014-6-26 下午03:37:11 
 * @version V2.0 
 */  
package com.joyce.quartz;  
  
import java.text.SimpleDateFormat;  
import java.util.Date;  
  
import org.quartz.Job;  
import org.quartz.JobExecutionContext;  
import org.quartz.JobExecutionException;  
  
/** 
 * @Description: 任務執行類 
 * 
 * @ClassName: QuartzJob 
 * @Copyright: Copyright (c) 2014 
 * 
 * @version V2.0 
 */  
public class QuartzJob implements Job {  
  
    @Override  
    public void execute(JobExecutionContext arg0) throws JobExecutionException {  
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+ ".........");    
    }  
}  

Ok,我們來測試一下:

/** 
 * @Description:  
 * 
 * @Title: QuartzTest.java 
 * @Package com.joyce.quartz.main 
 * @Copyright: Copyright (c) 2014 
 * 
 * @author Comsys-LZP 
 * @date 2014-6-26 下午03:35:05 
 * @version V2.0 
 */  
package com.joyce.quartz.main;  
  
import com.joyce.quartz.QuartzJob;  
import com.joyce.quartz.QuartzManager;  
  
/** 
 * @Description: 測試類 
 * 
 * @ClassName: QuartzTest 
 * @Copyright: Copyright (c) 2014 
 *  
 * @version V2.0 
 */  
public class QuartzTest {  
    public static void main(String[] args) {  
        try {  
            String job_name = "動態任務調度";  
            System.out.println("【系統啟動】開始(每1秒輸出一次)...");    
            QuartzManager.addJob(job_name, QuartzJob.class, "0/1 * * * * ?");    
              
            Thread.sleep(5000);    
            System.out.println("【修改時間】開始(每2秒輸出一次)...");    
            QuartzManager.modifyJobTime(job_name, "10/2 * * * * ?");    
            Thread.sleep(6000);    
            System.out.println("【移除定時】開始...");    
            QuartzManager.removeJob(job_name);    
            System.out.println("【移除定時】成功");    
              
            System.out.println("【再次添加定時任務】開始(每10秒輸出一次)...");    
            QuartzManager.addJob(job_name, QuartzJob.class, "*/10 * * * * ?");    
            Thread.sleep(60000);    
            System.out.println("【移除定時】開始...");    
            QuartzManager.removeJob(job_name);    
            System.out.println("【移除定時】成功");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  

運行一下,看看效果

文章轉載至 http://blog.csdn.net/luo201227/article/details/37511137#


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM