spring4+quartz2.x Dynamic Configuration Tool class

Source: Internet
Author: User
Tags log log

Import static org.quartz.CronScheduleBuilder.cronSchedule;

Import static Org.quartz.JobBuilder.newJob;

Import static Org.quartz.TriggerBuilder.newTrigger;



Import Org.apache.commons.logging.Log;

Import Org.apache.commons.logging.LogFactory;

Import Org.quartz.CronTrigger;

Import Org.quartz.JobDetail;

Import Org.quartz.JobKey;

Import Org.quartz.Scheduler;

Import org.quartz.SchedulerException;

Import Org.quartz.SchedulerFactory;

Import Org.quartz.Trigger;

Import Org.quartz.TriggerKey;

Import Org.quartz.impl.StdSchedulerFactory;



/**

* Timer Tool Class

* @author Zxs

*

*/

public class Quartzmanager {

private static log log = Logfactory.getlog (Quartzmanager.class);

public static Schedulerfactory schedulerfactory = new Stdschedulerfactory ();

public static Scheduler Getscheduler () {

try {

return Schedulerfactory.getscheduler ();

} catch (Schedulerexception e) {

Log.error ("");

return null;

}

}


/**

* Add a Timer

* @param jobName

* @param groupName

* @param CLZ

* @param cron

*/

public static void AddJob (String jobname,string groupName, Class clz, string cron) {

try {

Scheduler sched = Schedulerfactory.getscheduler ();

Jobdetail job = Newjob (CLZ). Withidentity (JobName, GroupName). Build ();

Trigger Trigger = Newtrigger (). Withidentity (JobName, GroupName). Startnow (). Withschedule ( Cronschedule (cron)). Build ();

Sched.schedulejob (Job, trigger);

Start

if (!sched.isshutdown ()) {

Sched.start ();

}

} catch (Schedulerexception e) {

E.printstacktrace ();

}

}

/**

* Pause Task

* @param jobName

* @param groupName

*/

public static void Pausejob (String jobname,string groupName) {

try {

Scheduler sched = Schedulerfactory.getscheduler ();

Jobkey jobkey = Jobkey.jobkey (JobName, groupName);

Sched.pausejob (Jobkey);

} catch (Schedulerexception e) {

Log.error ("", e);

}

}

public static void Resumejob (String jobname,string groupName) {

try {

Scheduler sched = Schedulerfactory.getscheduler ();

Jobkey jobkey = Jobkey.jobkey (JobName, groupName);

sched.resumejob (Jobkey);

} catch (Exception e) {

Log.error ("", e);

}

}

/**

* Delete Task

* @param jobName

* @param groupName

* @throws Exception

*/

public static void Deletejob (String jobname,string groupName) throws exception{

try {

Scheduler sched = Schedulerfactory.getscheduler ();

Jobkey jobkey = Jobkey.jobkey (JobName, groupName);

//Pause task first

pausejob (JobName, groupName);

//delete tasks again

sched.deletejob (Jobkey);

} catch (Exception e) {

Log.error ("", e);

}

}

/**

* Modify Task Run Configuration

* @param jobName

* @param groupName

* @param cron

*/

public static void Modifyjob (String jobname,string groupname,string cron) {

try {

Scheduler sched = Schedulerfactory.getscheduler ();

Triggerkey triggerkey = Triggerkey.triggerkey (JobName, groupName);

Crontrigger trigger = (Crontrigger) sched.gettrigger (Triggerkey);

Jobkey jobkey = Jobkey.jobkey (JobName, groupName);

if (trigger = = null) {

Return

}

String Oldcron = Trigger.getcronexpression ();

if (!oldcron.equalsignorecase (cron)) {

jobdetail job=sched.getjobdetail (Jobkey);

Class objjobclass = Job.getjobclass ();

deletejob (JobName, groupName);

AddJob (JobName, GroupName, Objjobclass, cron);

}

} catch (Exception e) {

Log.error ("", e);

}

}

}


spring4+quartz2.x Dynamic Configuration Tool class

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.