Task Scheduling open-source framework Quartz dynamically add, modify, and delete scheduled tasks

Source: Internet
Author: User

Task Scheduling open-source framework Quartz dynamically add, modify, and delete scheduled tasks

Quartz is an open-source job scheduling framework that provides a simple but powerful mechanism for Job Scheduling in Java applications. The Quartz framework includes the scheduler listener, job, and trigger listener. You can configure a job and trigger listener as a global listener or a job and trigger-specific listener. Quartz allows developers to schedule jobs by time interval (or day. It realizes the many-to-many relationship between jobs and triggers, and can associate multiple jobs with different triggers. Applications integrated with Quartz can reuse jobs from different events and combine multiple jobs for one event. It can also be integrated with Spring configurations. Quartz is far more powerful than the built-in JDK Timer! Okay, go directly to the Code:

/*** @ Description: ** @ Title: QuartzManager. java * @ Package com. joyce. quartz * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 03:15:52 * @ version V2.0 */package com. joyce. quartz; import org. quartz. cronTrigger; import org. quartz. jobDetail; import org. quartz. scheduler; import org. quartz. schedulerFactory; import org. quartz. impl. stdSchedulerFactory;/*** @ Description: scheduled task management class * * @ ClassName: QuartzManager * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 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: adds a scheduled task, Use the default task group name, trigger name, and trigger group name ** @ param jobName * task name * @ param cls * task * @ param time * to set the time, refer to quartz instructions ** @ Title: QuartzManager. java * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 03:47:44 * @ version V2.0 */@ SuppressWarnings ("unchecked ") public static void addJob (String jobName, Class cls, String time) {try {sched1_sched = gSchedulerFactory. getScheduler (); JobDetail jobDe Tail = new JobDetail (jobName, JOB_GROUP_NAME, cls); // Task Name, task group, task execution class // trigger CronTrigger trigger = new CronTrigger (jobName, TRIGGER_GROUP_NAME ); // trigger name, trigger Group trigger. setCronExpression (time); // trigger time setting sched. scheduleJob (jobDetail, trigger); // start if (! Sched. isShutdown () {sched. start () ;}} catch (Exception e) {throw new RuntimeException (e) ;}/ *** @ Description: add a scheduled task ** @ param jobName * task name * @ param jobGroupName * task group name * @ param triggerName * trigger name * @ param triggerGroupName * trigger group name * @ param jobClass * task * @ param time * time settings, refer to quartz instructions ** @ Title: QuartzManager. java * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 3:48:15 * @ version V2.0 */@ SuppressWarnings ("unchecked") public static void addJob (String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String time) {try {sched1_sched = gSchedulerFactory. getScheduler (); jobGroupName = new JobDetail (jobName, jobGroupName, jobClass); // Task Name, task group, task execution class // trigger CronTrigger trigger = new CronTrigger (triggerName, triggerGro UpName); // trigger name, trigger group. setCronExpression (time); // trigger time setting sched. scheduleJob (jobDetail, trigger);} catch (Exception e) {throw new RuntimeException (e) ;}/ *** @ Description: modify the trigger time of a task (use the default task group name, trigger name, and trigger group name) ** @ param jobName * @ param time ** @ Title: QuartzManager. java * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 03:49:21 * @ version V2.0 */@ SuppressWarnings ("unche Cked ") public static void modifyJobTime (String jobName, String time) {try {sched1_sched = gSchedulerFactory. getScheduler (); CronTrigger trigger = (CronTrigger) sched. getTrigger (jobName, TRIGGER_GROUP_NAME); if (trigger = null) {return;} String oldTime = trigger. getCronExpression (); if (! OldTime. required signorecase (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: Modify the trigger time of a task ** @ param triggerName * @ param triggerGroupName * @ param time ** @ Title: QuartzManager. java * @ Copyright: Copyright (C) 2014 ** @ author Comsys-LZP * @ date 03:49:37 * @ version V2.0 */public static void modifyJobTime (String triggerName, String triggerGroupName, String time) {try {sched1_sched = gSchedulerFactory. getScheduler (); CronTrigger trigger = (CronTrigger) sched. getTrigger (triggerName, triggerGroupName); if (trigger = null) {return;} String oldTime = trigger. getCronExpression (); if (! OldTime. repeated signorecase (time) {CronTrigger ct = (CronTrigger) trigger; // modify the time ct. setCronExpression (time); // restart the trigger sched. resumeTrigger (triggerName, triggerGroupName) ;}} catch (Exception e) {throw new RuntimeException (e) ;}/ *** @ Description: remove a task (use the default task group name, trigger name, and trigger group name) ** @ param jobName ** @ Title: QuartzManager. java * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 2014-6-26 pm: 51 * @ version V2.0 */public static void removeJob (String jobName) {try {sched1_sched = gSchedulerFactory. getScheduler (); sched. pauseTrigger (jobName, TRIGGER_GROUP_NAME); // stop the trigger sched. unscheduleJob (jobName, TRIGGER_GROUP_NAME); // remove the trigger sched. deleteJob (jobName, JOB_GROUP_NAME); // Delete task} catch (Exception e) {throw new RuntimeException (e) ;}/ *** @ Description: remove a task ** @ param jobName * @ param jobGr OupName * @ param triggerName * @ param triggerGroupName ** @ Title: QuartzManager. java * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 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, triggerGroup Name); // stop the trigger sched. unscheduleJob (triggerName, triggerGroupName); // remove the trigger sched. deleteJob (jobName, jobGroupName); // Delete task} catch (Exception e) {throw new RuntimeException (e) ;}/ *** @ Description: start all scheduled tasks *** @ Title: QuartzManager. java * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 03:50:18 * @ version V2.0 */public static void startJobs () {try {Scheduler sched = gSchedu LerFactory. getScheduler (); sched. start () ;} catch (Exception e) {throw new RuntimeException (e) ;}/ *** @ Description: Close all scheduled tasks *** @ Title: QuartzManager. java * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 03:50:26 * @ version V2.0 */public static void shutdownJobs () {try {sched1_sched = gSchedulerFactory. getScheduler (); if (! Sched. isShutdown () {sched. shutdown () ;}} catch (Exception e) {throw new RuntimeException (e );}}}

The preceding operations are commonly used for quartz task scheduling. They are encapsulated for external calls! In this way, we need to execute the task:
/*** @ Description: ** @ Title: QuartzJob. java * @ Package com. joyce. quartz * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 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: job execution class ** @ ClassName: QuartzJob * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 03:37:11 * @ version V2.0 */public class QuartzJob implements Job {@ Overridepublic void execute (JobExecutionContext arg0) throws JobExecutionException {System. out. println (new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "). format (new Date () +"★★★★★★★★★★★");}}

OK. Let's test:
/*** @ Description: ** @ Title: QuartzTest. java * @ Package com. joyce. quartz. main * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 03:35:05 * @ version V2.0 */package com. joyce. quartz. main; import com. joyce. quartz. quartzJob; import com. joyce. quartz. quartzManager;/*** @ Description: Test class ** @ ClassName: QuartzTest * @ Copyright: Copyright (c) 2014 ** @ author Comsys-LZP * @ date 03:35:05 * @ version V2.0 */public class QuartzTest {public static void main (String [] args) {try {String job_name = "dynamic task scheduling"; System. out. println ("[system startup] starts (output once every 1 second )... "); QuartzManager. addJob (job_name, QuartzJob. class, "0/1 ****? "); Thread. sleep (1, 5000); System. out. println ("[modification time] Start (output every 2 seconds )... "); QuartzManager. modifyJobTime (job_name, "10/2 ****? "); Thread. sleep (1, 6000); System. out. println ("[remove timing] start... "); QuartzManager. removeJob (job_name); System. out. println ("[removed from Schedule] successful"); System. out. println ("[add scheduled task again] Start (output every 10 seconds )... "); QuartzManager. addJob (job_name, QuartzJob. class, "*/10 ****? "); Thread. sleep (1, 60000); System. out. println ("[remove timing] start... "); QuartzManager. removeJob (job_name); System. out. println ("[removal timing] successful");} catch (Exception e) {e. printStackTrace ();}}}

Run it to see:


If you are interested in the regular configuration, you can check the cron expression of Quartz. I will not introduce it to everyone here! Ask du Niang, this information is comprehensive! Finally to attach my resources: http://download.csdn.net/download/luo201227/7603633

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.