Quartz2.x增、刪、改工具類
阿新 • • 發佈:2019-01-02
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import org.apache.logging.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
/**
Quartz增、刪、改工具類
*/
public class QuartzUtils {
private Logger log = null;
private Scheduler scheduler = null;
public static final String DATA_KEY = "STATE_DATA";
public QuartzUtils (){
try {
log = org.apache.logging.log4j.LogManager.getLogger();
scheduler = new StdSchedulerFactory().getScheduler();
log.info("初始化排程器 ");
} catch (SchedulerException ex) {
log.error("初始化排程器=> [失敗]:" + ex.getLocalizedMessage());
}
}
public void addJob(String name, String group, Class<? extends Job> clazz,String cronExpression) {
try {
//構造任務
JobDetail job = newJob(clazz)
.withIdentity(name, group)
.build();
//構造任務觸發器
Trigger trg = newTrigger()
.withIdentity(name, group)
.withSchedule(cronSchedule(cronExpression))
.build();
//將作業新增到排程器
scheduler.scheduleJob(job, trg);
log.info("建立作業=> [作業名稱:" + name + " 作業組:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("建立作業=> [作業名稱:" + name + " 作業組:" + group + "]=> [失敗]");
}
}
public void removeJob(String name, String group){
try {
TriggerKey tk = TriggerKey.triggerKey(name, group);
scheduler.pauseTrigger(tk);//停止觸發器
scheduler.unscheduleJob(tk);//移除觸發器
JobKey jobKey = JobKey.jobKey(name, group);
scheduler.deleteJob(jobKey);//刪除作業
log.info("刪除作業=> [作業名稱:" + name + " 作業組:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("刪除作業=> [作業名稱:" + name + " 作業組:" + group + "]=> [失敗]");
}
}
public void pauseJob(String name, String group){
try {
JobKey jobKey = JobKey.jobKey(name, group);
scheduler.pauseJob(jobKey);
log.info("暫停作業=> [作業名稱:" + name + " 作業組:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("暫停作業=> [作業名稱:" + name + " 作業組:" + group + "]=> [失敗]");
}
}
public void resumeJob(String name, String group){
try {
JobKey jobKey = JobKey.jobKey(name, group);
scheduler.resumeJob(jobKey);
log.info("恢復作業=> [作業名稱:" + name + " 作業組:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("恢復作業=> [作業名稱:" + name + " 作業組:" + group + "]=> [失敗]");
}
}
public void modifyTime(String name, String group, String cronExpression){
try {
TriggerKey tk = TriggerKey.triggerKey(name, group);
//構造任務觸發器
Trigger trg = newTrigger()
.withIdentity(name, group)
.withSchedule(cronSchedule(cronExpression))
.build();
scheduler.rescheduleJob(tk, trg);
log.info("修改作業觸發時間=> [作業名稱:" + name + " 作業組:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("修改作業觸發時間=> [作業名稱:" + name + " 作業組:" + group + "]=> [失敗]");
}
}
public void start() {
try {
scheduler.start();
log.info("啟動排程器 ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("啟動排程器=> [失敗]");
}
}
public void shutdown() {
try {
scheduler.shutdown();
log.info("停止排程器 ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("停止排程器=> [失敗]");
}
}
}
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import org.apache.logging.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
/**
Quartz增、刪、改工具類
*/
public class QuartzUtils {
private Logger log = null;
private Scheduler scheduler = null;
public static final String DATA_KEY = "STATE_DATA";
public QuartzUtils (){
try {
log = org.apache.logging.log4j.LogManager.getLogger();
scheduler = new StdSchedulerFactory().getScheduler();
log.info("初始化排程器 ");
} catch (SchedulerException ex) {
log.error("初始化排程器=> [失敗]:" + ex.getLocalizedMessage());
}
}
public void addJob(String name, String group, Class<? extends Job> clazz,String cronExpression) {
try {
//構造任務
JobDetail job = newJob(clazz)
.withIdentity(name, group)
.build();
//構造任務觸發器
Trigger trg = newTrigger()
.withIdentity(name, group)
.withSchedule(cronSchedule(cronExpression))
.build();
//將作業新增到排程器
scheduler.scheduleJob(job, trg);
log.info("建立作業=> [作業名稱:" + name + " 作業組:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("建立作業=> [作業名稱:" + name + " 作業組:" + group + "]=> [失敗]");
}
}
public void removeJob(String name, String group){
try {
TriggerKey tk = TriggerKey.triggerKey(name, group);
scheduler.pauseTrigger(tk);//停止觸發器
scheduler.unscheduleJob(tk);//移除觸發器
JobKey jobKey = JobKey.jobKey(name, group);
scheduler.deleteJob(jobKey);//刪除作業
log.info("刪除作業=> [作業名稱:" + name + " 作業組:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("刪除作業=> [作業名稱:" + name + " 作業組:" + group + "]=> [失敗]");
}
}
public void pauseJob(String name, String group){
try {
JobKey jobKey = JobKey.jobKey(name, group);
scheduler.pauseJob(jobKey);
log.info("暫停作業=> [作業名稱:" + name + " 作業組:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("暫停作業=> [作業名稱:" + name + " 作業組:" + group + "]=> [失敗]");
}
}
public void resumeJob(String name, String group){
try {
JobKey jobKey = JobKey.jobKey(name, group);
scheduler.resumeJob(jobKey);
log.info("恢復作業=> [作業名稱:" + name + " 作業組:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("恢復作業=> [作業名稱:" + name + " 作業組:" + group + "]=> [失敗]");
}
}
public void modifyTime(String name, String group, String cronExpression){
try {
TriggerKey tk = TriggerKey.triggerKey(name, group);
//構造任務觸發器
Trigger trg = newTrigger()
.withIdentity(name, group)
.withSchedule(cronSchedule(cronExpression))
.build();
scheduler.rescheduleJob(tk, trg);
log.info("修改作業觸發時間=> [作業名稱:" + name + " 作業組:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("修改作業觸發時間=> [作業名稱:" + name + " 作業組:" + group + "]=> [失敗]");
}
}
public void start() {
try {
scheduler.start();
log.info("啟動排程器 ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("啟動排程器=> [失敗]");
}
}
public void shutdown() {
try {
scheduler.shutdown();
log.info("停止排程器 ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("停止排程器=> [失敗]");
}
}
}