/**
* @Description:
*
* @Title: QuartzManager.java
* @Package com.joyce.quartz
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 涓嬪崍03:15:52
* @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
* 鏃堕棿璁剧疆锛屽弬鑰僸uartz璇存槑鏂囨。
*
* @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
* 鏃堕棿璁剧疆锛屽弬鑰僸uartz璇存槑鏂囨。
*
* @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);
}
}
}