package com.ancun.tdbank.manage.utils; import com.ancun.tdbank.model.TdTask; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class JobUtil { private static final Logger logger = LoggerFactory.getLogger(JobUtil.class); private static Scheduler scheduler; private static final String perfix = "com.ancun.tdbank.manage.system.task."; private static Scheduler getScheduler(){ if (scheduler == null){ try { scheduler = StdSchedulerFactory.getDefaultScheduler(); logger.info("创建scheduler实例成功"); } catch (SchedulerException e) { logger.info("创建scheduler实例失败"); e.printStackTrace(); } } return scheduler; } /** * 触发任务 * @param tdTask * @throws Exception */ public static void addJob(TdTask tdTask)throws Exception{ Scheduler scheduler = getScheduler(); logger.info("开始触发任务"); String jobClassName = perfix + tdTask.getJobClass(); String jobGroupName = tdTask.getGroupName(); String cronExpression = tdTask.getCornExpr(); logger.info("启动调度器"); scheduler.start(); logger.info("构建job信息"); JobDetail jobDetail = JobBuilder.newJob(getClass(jobClassName)).withIdentity(jobClassName, jobGroupName).build(); logger.info("表达式调度构建器(即任务执行的时间)"); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression); logger.info("按新的cronExpression表达式构建一个新的trigger"); CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobClassName, jobGroupName) .withSchedule(scheduleBuilder).build(); try { scheduler.scheduleJob(jobDetail, trigger); logger.info("创建定时任务成功"); } catch (SchedulerException e) { logger.info("创建定时任务失败,失败信息{}",e); logger.error("创建定时任务失败,失败信息{}",e); } } /** * 暂停任务 * @param tdTask * @throws Exception */ public static void pausejob(TdTask tdTask) throws Exception { Scheduler scheduler = getScheduler(); scheduler.pauseJob(JobKey.jobKey(tdTask.getJobClass(),tdTask.getGroupName())); } /** * 恢复任务 * @param tdTask * @throws Exception */ public static void resumejob(TdTask tdTask) throws Exception { Scheduler scheduler = getScheduler(); scheduler.resumeJob(JobKey.jobKey(tdTask.getJobClass(),tdTask.getGroupName())); } /** * 重新排定日程 * @param tdTask * @throws Exception */ public static void rescheduleJob(TdTask tdTask) throws Exception { Scheduler scheduler = getScheduler(); logger.info("开始重新排定日程"); String jobClassName = tdTask.getJobClass(); String jobGroupName = tdTask.getGroupName(); String cronExpression = tdTask.getCornExpr(); TriggerKey triggerKey = TriggerKey.triggerKey(jobClassName, jobGroupName); logger.info("表达式调度构建器"); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); logger.info("按新的cronExpression表达式重新构建trigger"); trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); logger.info("按新的trigger重新设置job执行"); scheduler.rescheduleJob(triggerKey, trigger); } /** * 删除任务 * @param tdTask * @throws Exception */ public static void deletejob(TdTask tdTask) throws Exception { Scheduler scheduler = getScheduler(); logger.info("开始重新排定日程"); String jobClassName = tdTask.getJobClass(); String jobGroupName = tdTask.getGroupName(); scheduler.pauseTrigger(TriggerKey.triggerKey(jobClassName, jobGroupName)); scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName, jobGroupName)); scheduler.deleteJob(JobKey.jobKey(jobClassName, jobGroupName)); } private static Class getClass(String classname){ logger.info("获取以String类型的className实例化类"); Class obj= null; try { obj = Class.forName(classname); } catch (ClassNotFoundException e) { e.printStackTrace(); } return obj; } }
jquartz的job触发demo
最新推荐文章于 2023-05-18 16:27:37 发布