import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
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.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzManager {
private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();

/**
* @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
*
* @param jobName
* 任务名
* @param cls
* 任务
* @param time
* 时间设置,参考quartz说明文档

*/
@SuppressWarnings("unchecked")
public static void addJob(String jobName,String groupName,String triggerGroupName, Class jobClass, String cronExpression) {

Scheduler scheduler;
try {
scheduler = schedulerFactory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, groupName).build();
// 触发器
Trigger trigger = TriggerBuilder.newTrigger().startNow().withIdentity(jobName, triggerGroupName).withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();
scheduler.scheduleJob(jobDetail, trigger);
// 启动
if (!scheduler.isShutdown()) {
scheduler.start();
}
} catch (SchedulerException e) {
e.printStackTrace();
}
}

/**
* @Description: 添加一个定时任务
*
* @param jobName
* 任务名
* @param jobGroupName
* 任务组名
* @param triggerName
* 触发器名
* @param triggerGroupName
* 触发器组名
* @param jobClass
* 任务
* @param time
* 时间设置,参考quartz说明文档
*
*/
@SuppressWarnings("unchecked")
public static void addJob(String jobName, String groupName,
String triggerName, String triggerGroupName, Class jobClass,
String cronExpression) {
Scheduler scheduler;
try {
scheduler = schedulerFactory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, groupName).build();
// 触发器
Trigger trigger = TriggerBuilder.newTrigger().startNow().withIdentity(triggerName, triggerGroupName).withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();
scheduler.scheduleJob(jobDetail, trigger);
// 启动
if (!scheduler.isShutdown()) {
scheduler.start();
}
} catch (SchedulerException e) {
e.printStackTrace();
}
}



/**
* @Description: 修改一个任务的触发时间
*
* @param triggerName
* @param triggerGroupName
* @param time
*
*/
public static void modifyJobTime(String triggerName,
String triggerGroupName, String newCronExpression) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(new TriggerKey(triggerName,triggerGroupName));
if (trigger == null) {
return;
}
String cronExpression = trigger.getCronExpression();
if (!cronExpression.equalsIgnoreCase(newCronExpression)) {
Trigger newTrigger = TriggerBuilder.newTrigger().startNow().withIdentity(triggerName, triggerGroupName).withSchedule(CronScheduleBuilder.cronSchedule(newCronExpression)).build();
scheduler.rescheduleJob(new TriggerKey(triggerName,triggerGroupName), newTrigger);
}
} catch (SchedulerException e) {
e.printStackTrace();
}
}


/**
* @Description: 移除一个任务
*
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*
*/
public static void removeJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.pauseTrigger(new TriggerKey(triggerName, triggerGroupName));// 停止触发器
scheduler.unscheduleJob(new TriggerKey(triggerName, triggerGroupName));// 移除触发器
scheduler.pauseJob(new JobKey(jobName, jobGroupName));
scheduler.deleteJob(new JobKey(jobName, jobGroupName));// 删除任务
} catch (SchedulerException e) {
e.printStackTrace();
}
}

/**
* @Description:启动所有定时任务
*
*
*/
public static void startJobs() {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}

/**
* @Description:关闭所有定时任务
*
*
*/
public static void shutdownJobs() {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
if (!scheduler.isShutdown()) {
scheduler.shutdown();
}
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}