package com.yxkong.core.utils; import static org.quartz.CronScheduleBuilder.cronSchedule; import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; import java.util.Date; import java.util.List; import org.quartz.CronScheduleBuilder; 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.TriggerKey; import org.quartz.impl.StdSchedulerFactory; import com.yxkong.core.model.SysTask; import com.jfinal.log.Logger; public class JobTaskUtil { protected static Logger logger = Logger.getLogger(JobTaskUtil.class); private static SchedulerFactory sf = new StdSchedulerFactory(); private static Scheduler sched=null; static{ try { sched=sf.getScheduler(); } catch (SchedulerException e) { logger.error("获取Scheduler失败 ",e); } } /** * 防止重复initTask */ private static Boolean locked=false; private JobTaskUtil(){ } /** * 初始化数据库中status=0的任务,不要随便用 */ public static void initTask(){ if(locked){ logger.error("请不要重复初始化Job"); return ; } List<SysTask> tasks = SysTask.findAll(); if(tasks!=null&&tasks.size()>0){ String jobClass=null; String jobGroupName=null; String jobCronExp=null; for(SysTask t:tasks){ jobClass=t.getStr("jobClass"); jobGroupName=t.getStr("jobGroupName"); jobCronExp=t.getStr("jobCron"); addOrupdateTask(jobClass, jobGroupName, jobCronExp); } } locked=true; } /** * 暂停定时任务 * @param jobClass 定时任务class路径 * @param jobGroupName 定时任务组名 * @return */ public static boolean stopTask(String jobClass,String jobGroupName){ try { JobKey jobKey = JobKey.jobKey(jobClass, jobGroupName); sched.pauseJob(jobKey); } catch (SchedulerException e) { logger.error("获取Scheduler失败 ",e); return false; } return true; } /** * 暂停所有任务 * @return */ public static boolean stapAll(){ try { sched.pauseAll(); } catch (SchedulerException e) { logger.error("获取Scheduler失败 ",e); return false; } return true; } /** * 恢复定时任务 * @param jobClass 定时任务class路径 * @param jobGroupName 定时任务组名 * @return */ public static boolean resumeTask(String jobClass,String jobGroupName){ try { JobKey jobKey = JobKey.jobKey(jobClass, jobGroupName); sched.resumeJob(jobKey); } catch (SchedulerException e) { logger.error("获取Scheduler失败 ",e); return false; } return true; } /** * 恢复所有任务 * @return */ public static boolean resumeAll(){ try { sched.resumeAll(); } catch (SchedulerException e) { logger.error("获取Scheduler失败 ",e); return false; } return true; } /** * 删除定时任务 * @param jobClass 定时任务class路径 * @param jobGroupName 定时任务组名 * @return */ public static boolean deleteTask(String jobClass,String jobGroupName){ try { JobKey jobKey = JobKey.jobKey(jobClass, jobGroupName); sched.deleteJob(jobKey); } catch (SchedulerException e) { logger.error("获取Scheduler失败 ",e); return false; } return true; } /** * 添加或更新一个任务 * @param jobClass 定时任务cass路径 * @param jobGroupName job组名 * @param cronEx 表达式 * @return */ public static boolean addOrupdateTask(String jobClass,String jobGroupName,String cronEx){ try { JobKey jobKey = JobKey.jobKey(jobClass, jobGroupName); TriggerKey triggerKey = TriggerKey.triggerKey(jobClass, jobGroupName); CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey); //已存在 if(trigger!=null){ // trigger已存在,则更新相应的定时设置 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronEx); // 按新的cronExpression表达式重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); // 按新的trigger重新设置job执行 sched.rescheduleJob(triggerKey, trigger); }else{ Class clazz = Class.forName(jobClass); JobDetail jobDetail = jobDetail= newJob(clazz).withIdentity(jobClass,jobGroupName ).build(); // 按表达式构建一个新的trigger trigger= newTrigger().withIdentity(jobClass, jobGroupName) .withSchedule(cronSchedule(cronEx)).build(); Date ft = sched.scheduleJob(jobDetail, trigger); sched.start(); logger.info(jobDetail.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression()); } } catch (SchedulerException e) { return false; } catch (ClassNotFoundException e) { logger.error("未找到Job任务的Class",e); return false; } return true; } }
文章评论
提前祝您新年快乐。