The open-source Quartz framework dynamically adds, modifies, and deletes scheduled tasks (2). the quartz framework

Source: Internet
Author: User

The open-source Quartz framework dynamically adds, modifies, and deletes scheduled tasks (2). the quartz framework

It seems that every time I look back at some of the previously written items, I don't always feel perfect ~~ Although you are not a perfect person, you have to do the perfect thing! This time, we mainly focused on the Dynamic Maintenance of Quartz and Spring integration. It was a little simple and crude. We went straight to the Code and asked me if I knew anything about it.

Prepare a scheduled task

Package com. hupu. timertask;/*** job scheduling class * @ author Joyce. luo * @ date 03:32:04 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */public class QuartzJob {/*** task ID */private Integer jobId;/*** task name */private String jobName; /*** Task Group */private String jobGroup;/*** task status 0 disabled 1 enable 2 delete */private Integer jobStatus; /*** task running time expression */private String cronExpression;/*** @ return the jobId */public Integer getJobId () {return jobId ;} /*** @ param jobId * the jobId to set */public void setJobId (Integer jobId) {this. jobId = jobId;}/*** @ return the jobName */public String getJobName () {return jobName ;} /*** @ param jobName * the jobName to set */public void setJobName (String jobName) {this. jobName = jobName;}/*** @ return the jobGroup */public String getJobGroup () {return jobGroup ;} /*** @ param jobGroup * the jobGroup to set */public void setJobGroup (String jobGroup) {this. jobGroup = jobGroup;}/*** @ return the jobStatus */public Integer getJobStatus () {return jobStatus ;} /*** @ param jobStatus * the jobStatus to set */public void setJobStatus (Integer jobStatus) {this. jobStatus = jobStatus;}/*** @ return the cronExpression */public String getCronExpression () {return cronExpression ;} /*** @ param cronExpression * the cronExpression to set */public void setCronExpression (String cronExpression) {this. cronExpression = cronExpression ;}}

Scheduled task

Package com. hupu. timertask; import org. apache. logging. log4j. logManager; import org. apache. logging. log4j. logger; import org. quartz. cronScheduleBuilder; import org. quartz. cronTrigger; import org. quartz. jobBuilder; import org. quartz. jobDetail; import org. quartz. jobKey; import org. quartz. triggerBuilder; import org. quartz. triggerKey; import org. quartz. impl. stdScheduler;/*** job scheduling management class * @ author Joyce. luo * @ date 20 15-3-31 03:42:30 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */public class QuartzManager {private static final Logger logger = LogManager. getLogger (QuartzManager. class. getName (); private StdScheduler scheduler;/*** @ param scheduler the schedto to set */public void setScheduler (StdScheduler scheduler) {this. sched = sched;}/*** initialize task scheduling ** @ author Joyce. luo * @ Date 03:48:55 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */@ SuppressWarnings ("unchecked") public void initJob (QuartzJob job, class cls) {logger.info ("initialization task scheduling"); try {TriggerKey triggerKey = TriggerKey. triggerKey (job. getJobName (), job. getJobGroup (); CronTrigger trigger = (CronTrigger) schedgger. getTrigger (triggerKey); if (null = trigger) {addQuartzJob (job, Trigger, cls) ;}} catch (Exception e) {logger. error ("initialization task scheduling Exception! "+ E. getMessage (), e) ;}}/*** add scheduled tasks to task scheduling * @ param job scheduled task information * @ param trigger scheduled scheduling trigger * @ author Joyce. luo * @ date 04:04:58 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */@ SuppressWarnings ("unchecked ") private void addQuartzJob (QuartzJob job, CronTrigger trigger, Class cls) {logger.info ("add scheduled tasks to task scheduling"); try {JobDetail jobDetail = JobBuilder. newJob (cls ). withId Entity (job. getJobName (), job. getJobGroup ()). build (); jobDetail. getJobDataMap (). put (job. getJobName (), job); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder. cronSchedule (job. getCronExpression (); trigger = TriggerBuilder. newTrigger (). withIdentity (job. getJobName (), job. getJobGroup ()). withSchedule (scheduleBuilder ). build (); scheduler. scheduleJob (jobDetail, trigger);} catch (Exception e) {logger. e Rror ("An error occurred while adding a scheduled task to task scheduling! "+ E. getMessage (), e) ;}}/*** run scheduled task now * @ param job scheduled task information * @ author Joyce. luo * @ date 02:08:41 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */void runJob (QuartzJob job) {logger.info ("scheduled tasks in Task Scheduling now"); try {if (null = job) {logger.info ("scheduled task information is empty and cannot be run immediately "); return;} JobKey jobKey = JobKey. jobKey (job. getJobName (), job. getJobGroup (); if (null = jobKey) {logger.info ("[" + Job. getJobName () + "] scheduled task does not exist in task scheduling. Do not run it immediately! "); Return;} scheduler. triggerJob (jobKey);} catch (Exception e) {logger. error (" An error occurred while running the scheduled task immediately! "+ E. getMessage (), e );}} /*** modify the scheduled task in Task Scheduling * @ param job scheduled task information * @ param triggerKey: scheduled scheduling trigger key * @ param trigger: scheduled scheduling trigger * @ author Joyce. luo * @ date 04:16:54 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */void updateQuartzJob (QuartzJob job, TriggerKey triggerKey, CronTrigger trigger) {logger.info ("modifying scheduled tasks in Task Scheduling"); try {if (null = job | null = triggerKey | null = Trigger) {logger.info ("modifying scheduling task parameters is not normal! "); Return;} logger.info (" original task expression: "+ trigger. getCronExpression () + ", current task expression:" + job. getCronExpression (); if (trigger. getCronExpression (). equals (job. getCronExpression () {logger.info ("the task scheduling expression is consistent and will not be modified! "); Return;} logger.info (" Modify because the scheduling expression is inconsistent "); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder. cronSchedule (job. getCronExpression (); trigger = trigger. getTriggerBuilder (). withIdentity (triggerKey ). withSchedule (scheduleBuilder ). build (); scheduler. rescheduleJob (triggerKey, trigger);} catch (Exception e) {logger. error ("An error occurred while modifying scheduled tasks! "+ E. getMessage (), e) ;}}/*** pause scheduled task in Task Scheduling * @ param job scheduled task information * @ author Joyce. luo * @ date 02:22:53 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */void pauseJob (QuartzJob job) {logger.info ("Pause scheduled tasks in Task Scheduling"); try {if (null = job) {logger.info ("the parameters of the paused scheduling task are not normal! "); Return;} JobKey jobKey = JobKey. jobKey (job. getJobName (), job. getJobGroup (); if (null = jobKey) {logger.info ("the task scheduling does not contain [" + job. the getJobName () + "] scheduled task is not paused! "); Return;} scheduler. pauseJob (jobKey);} catch (Exception e) {logger. error (" An error occurred while pausing the scheduled task! "+ E. getMessage (), e) ;}}/*** resume scheduled task * @ param job scheduled task information * @ author Joyce. luo * @ date 02:26:08 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */void resumeJob (QuartzJob job) {logger.info ("restoring scheduled tasks in Task Scheduling"); try {if (null = job) {logger.info ("the parameters of the recovery scheduling task are not normal! "); Return;} JobKey jobKey = JobKey. jobKey (job. getJobName (), job. getJobGroup (); if (null = jobKey) {logger.info ("the task scheduling does not contain [" + job. getJobName () + "] scheduled task, cannot be recovered! "); Return;} scheduler. resumeJob (jobKey);} catch (Exception e) {logger. error (" the scheduled task in task scheduling has an Exception! "+ E. getMessage (), e) ;}}/*** Delete scheduled tasks in Task Scheduling * @ param job scheduled task information * @ author Joyce. luo * @ date 04:30:03 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */void deleteJob (QuartzJob job) {logger.info (""); try {if (null = job) {logger.info ("the parameter for deleting a scheduling task is abnormal! "); Return;} JobKey jobKey = JobKey. jobKey (job. getJobName (), job. getJobGroup (); if (null = jobKey) {logger.info ("the task scheduling does not contain [" + job. getJobName () + "] The scheduled task is not deleted! "); Return;} scheduler. deleteJob (jobKey);} catch (Exception e) {logger. error (" An error occurred while deleting the scheduled task in task scheduling! "+ E. getMessage (), e) ;}}/*** Delete task scheduling timer * @ param triggerKey * @ author Joyce. luo * @ date 04:35:33 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */void deleteJob (TriggerKey triggerKey) {logger.info ("delete task scheduling timer"); try {if (null = triggerKey) {logger.info ("the stop task timer parameter is abnormal and will not be stopped! "); Return;} logger.info (" Stop task timer "); sched. pauseTrigger (triggerKey); schedgger. unscheduleJob (triggerKey);} catch (Exception e) {logger.info ("An error occurred while deleting the task scheduling timer! "+ E. getMessage (), e );}}}

Task Scheduling Factory

Package com. hupu. timertask; import java. util. date; import org. apache. logging. log4j. logManager; import org. apache. logging. log4j. logger; import org. quartz. job; import org. quartz. jobExecutionContext; import org. quartz. jobExecutionException; import com. hupu. util. formatUtil;/*** Job Scheduling factory class * @ author Joyce. luo * @ date 03:38:35 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 * /Public class QuartzJobFactory implements Job {private static final Logger logger = LogManager. getLogger (QuartzJobFactory. class. getName (); @ Overridepublic void execute (JobExecutionContext context) throws JobExecutionException {QuartzJob scheduleJob = (QuartzJob) context. getMergedJobDataMap (). get ("backup_job"); logger.info ("scheduled task execution started, task name [" + scheduleJob. getJobName () + "]"); Date previusfiretime = con Text. getpreviusfiretime (); if (null! = Previusfiretime) {logger.info ("scheduled task last scheduling time:" + FormatUtil. formatDate (previusfiretime);} logger.info ("scheduled task next scheduling time:" + FormatUtil. formatDate (context. getNextFireTime (); // executes the business logic }}

At this point, the code is basically over. As for Spring configuration, you don't need to talk about it more ~~~

I have to explain it. Let's just talk about it here.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.