Spring3 and Quartz2 application instances, spring3quartz2

Source: Internet
Author: User

Spring3 and Quartz2 application instances, spring3quartz2

/*** 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 ;}}



Import org. apache. log4j. logger; import org. quartz. job; import org. quartz. jobExecutionContext; import org. quartz. jobExecutionException; import com. hupu. nac. biz. IAuthPolicyClientBiz;/*** client persistent connection disconnection task scheduling factory class * @ author Joyce. luo * @ date 03:38:35 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */public class LinkDisconnectQuartzJobFactory implements Job {private static final Logger logger = Logger. getLogger (LinkDisconnectQuartzJobFactory. class); @ Override public void execute (JobExecutionContext context) throws JobExecutionException {QuartzJob scheduleJob = (QuartzJob) context. getMergedJobDataMap (). get ("linkDisconnectJob"); logger.info ("the task starts to run when the client is disconnected for a long connection. The task name is [" + scheduleJob. getJobName () + "]") ;}}


Import org. apache. 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. stdsched;/*** client persistent connection disconnection task scheduling management class * @ author Joyce. luo * @ date 03:42:30 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyri Ght Copyright (c) 2015 */public class LinkDisconnectQuartzManager {private static final Logger logger = Logger. getLogger (LinkDisconnectQuartzManager. class); private StdScheduler scheduler;/*** @ param schedto 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 */public void initJob () {logger.info ("initialize client persistent connection disconnect task scheduling "); quartzJob job = new QuartzJob (); job. setJobName ("link_disconnect_job"); job. setJobGroup ("link_disconnect_group"); job. setJobStatus (1); job. setCronExpression ("0 0/1 ***? "); Try {TriggerKey triggerKey = TriggerKey. triggerKey (job. getJobName (), job. getJobGroup (); CronTrigger trigger = (CronTrigger) schedgger. getTrigger (triggerKey); if (null = trigger) {addQuartzJob (job, trigger) ;}} catch (Exception e) {logger. error ("An error occurred while scheduling the initialization client persistent connection disconnection task! "+ 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 */private void addQuartzJob (QuartzJob job, CronTrigger trigger) {logger.info ("adding scheduled tasks to task scheduling"); try {JobDetail jobDetail = JobBuilder. newJob (LinkDisconnectQuartzJobFactory. class ). withId Entity (job. getJobName (), job. getJobGroup ()). build (); jobDetail. getJobDataMap (). put ("linkDisconnectJob", 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. error ("An error occurred while adding a scheduled task to task scheduling! "+ 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) ;}}/*** 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. resumeJob (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. resumeTrigger (triggerKey);} catch (Exception e) {logger.info (" An error occurred while deleting the task scheduling timer! "+ E. getMessage (), e );}}}


<bean id="linkDisconnectScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" />    <bean id="linkDisconnectQuartzManager" class="com.hupu.nac.timertask.LinkDisconnectQuartzManager" init-method="initJob">    <property name="scheduler" ref="linkDisconnectScheduler"></property></bean> 


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.