2021-06-28

springboot Quartz 定时任务工具类

package org.fh.util;import java.util.Map;import org.quartz.CronScheduleBuilder;import org.quartz.CronTrigger;import org.quartz.Job;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.JobKey;import org.quartz.Scheduler; import org.quartz.SchedulerFactory;import org.quartz.TriggerBuilder;import org.quartz.TriggerKey;import org.quartz.impl.StdSchedulerFactory; /** * 说明:定时任务管理类 * 作者:FH Admin Q313596790 * 官网:www.fhadmin.org */public class QuartzManager {  private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); //创建一个SchedulerFactory工厂实例 private static String JOB_GROUP_NAME = "FH_JOBGROUP_NAME"; 					//任务组 private static String TRIGGER_GROUP_NAME = "FH_TRIGGERGROUP_NAME"; 			//触发器组  /**添加一个定时任务,使用默认的任务组名,触发器名,触发器组名  * @param jobName 任务名  * @param cls 任务  * @param time 时间设置,参考quartz说明文档  */ public static void addJob(String jobName, Class<? extends Job> cls, String time) {   try {    Scheduler sched = gSchedulerFactory.getScheduler(); 										//通过SchedulerFactory构建Scheduler对象   JobDetail jobDetail= JobBuilder.newJob(cls).withIdentity(jobName,JOB_GROUP_NAME).build();	//用于描叙Job实现类及其他的一些静态信息,构建一个作业实例  	CronTrigger trigger = (CronTrigger) TriggerBuilder  			.newTrigger()	 																	//创建一个新的TriggerBuilder来规范一个触发器 				.withIdentity(jobName, TRIGGER_GROUP_NAME)											//给触发器起一个名字和组名 				.withSchedule(CronScheduleBuilder.cronSchedule(time)) 				.build();   sched.scheduleJob(jobDetail, trigger);    if (!sched.isShutdown()) {     sched.start(); 	 // 启动    }   } catch (Exception e) {    throw new RuntimeException(e);   }  }   /**添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 (带参数)  * @param jobName 任务名  * @param cls 任务  * @param time 时间设置,参考quartz说明文档  */ public static void addJob(String jobName, Class<? extends Job> cls, String time, Map<String,Object> parameter) {   try {    Scheduler sched = gSchedulerFactory.getScheduler(); 										//通过SchedulerFactory构建Scheduler对象   JobDetail jobDetail= JobBuilder.newJob(cls).withIdentity(jobName,JOB_GROUP_NAME).build();	//用于描叙Job实现类及其他的一些静态信息,构建一个作业实例   jobDetail.getJobDataMap().put("parameterList", parameter);									//传参数  	CronTrigger trigger = (CronTrigger) TriggerBuilder  			.newTrigger()	 																	//创建一个新的TriggerBuilder来规范一个触发器 				.withIdentity(jobName, TRIGGER_GROUP_NAME)											//给触发器起一个名字和组名 				.withSchedule(CronScheduleBuilder.cronSchedule(time)) 				.build();   sched.scheduleJob(jobDetail, trigger);    if (!sched.isShutdown()) {     sched.start(); 	 // 启动    }   } catch (Exception e) {    throw new RuntimeException(e);   }  }  /**添加一个定时任务  * @param jobName	任务名  * @param jobGroupName	任务组名  * @param triggerName	触发器名  * @param triggerGroupName	触发器组名  * @param jobClass	任务  * @param time	时间设置,参考quartz说明文档  */ public static void addJob(String jobName, String jobGroupName,    String triggerName, String triggerGroupName, Class<? extends Job> jobClass,    String time) {   try {    Scheduler sched = gSchedulerFactory.getScheduler();    JobDetail jobDetail= JobBuilder.newJob(jobClass).withIdentity(jobName,jobGroupName).build();// 任务名,任务组,任务执行类   CronTrigger trigger = (CronTrigger) TriggerBuilder	 // 触发器  				.newTrigger() 				.withIdentity(triggerName, triggerGroupName) 				.withSchedule(CronScheduleBuilder.cronSchedule(time)) 				.build();   sched.scheduleJob(jobDetail, trigger);   if (!sched.isShutdown()) {     sched.start(); 	 // 启动    }   } catch (Exception e) {    throw new RuntimeException(e);   }  }   /**添加一个定时任务 (带参数)  * @param jobName	任务名  * @param jobGroupName	任务组名  * @param triggerName	触发器名  * @param triggerGroupName	触发器组名  * @param jobClass	任务  * @param time	时间设置,参考quartz说明文档  */ public static void addJob(String jobName, String jobGroupName,    String triggerName, String triggerGroupName, Class<? extends Job> jobClass,    String time, Map<String,Object> parameter) {   try {    Scheduler sched = gSchedulerFactory.getScheduler();    JobDetail jobDetail= JobBuilder.newJob(jobClass).withIdentity(jobName,jobGroupName).build();// 任务名,任务组,任务执行类   jobDetail.getJobDataMap().put("parameterList", parameter);								//传参数   CronTrigger trigger = (CronTrigger) TriggerBuilder	 // 触发器  				.newTrigger() 				.withIdentity(triggerName, triggerGroupName) 				.withSchedule(CronScheduleBuilder.cronSchedule(time)) 				.build();   sched.scheduleJob(jobDetail, trigger);   if (!sched.isShutdown()) {     sched.start(); 	 // 启动    }   } catch (Exception e) {    throw new RuntimeException(e);   }  }  /** 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)  * @param jobName	任务名  * @param time	新的时间设置  */ public static void modifyJobTime(String jobName, String time) {   try {    Scheduler sched = gSchedulerFactory.getScheduler(); 							//通过SchedulerFactory构建Scheduler对象   TriggerKey triggerKey = TriggerKey.triggerKey(jobName,TRIGGER_GROUP_NAME); 		//通过触发器名和组名获取TriggerKey   CronTrigger trigger = (CronTrigger)sched.getTrigger(triggerKey);				//通过TriggerKey获取CronTrigger   if (trigger == null) {     return;    }    String oldTime = trigger.getCronExpression();    if (!oldTime.equalsIgnoreCase(time)) {    	JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);						//通过任务名和组名获取JobKey   	JobDetail jobDetail = sched.getJobDetail(jobKey);     Class<? extends Job> objJobClass = jobDetail.getJobClass();     removeJob(jobName);     addJob(jobName, objJobClass, time);    }   } catch (Exception e) {    throw new RuntimeException(e);   }  }   /**修改一个任务的触发时间  * @param triggerName	任务名称  * @param triggerGroupName	传过来的任务名称  * @param time	更新后的时间规则  */ public static void modifyJobTime(String triggerName, String triggerGroupName, String time) {   try {    Scheduler sched = gSchedulerFactory.getScheduler(); 							//通过SchedulerFactory构建Scheduler对象   TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroupName); 	//通过触发器名和组名获取TriggerKey   CronTrigger trigger = (CronTrigger)sched.getTrigger(triggerKey);				//通过TriggerKey获取CronTrigger   if (trigger == null) return;    CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(trigger.getCronExpression());   String oldTime = trigger.getCronExpression();    if (!oldTime.equalsIgnoreCase(time)) {    	trigger = (CronTrigger)trigger.g......

原文转载:http://www.shaoqun.com/a/833953.html

跨境电商:https://www.ikjzd.com/

sonar:https://www.ikjzd.com/w/215

bonanza:https://www.ikjzd.com/w/275.html

福茂:https://www.ikjzd.com/w/1633


packageorg.fh.util;importjava.util.Map;importorg.quartz.CronScheduleBuilder;importorg.quartz.CronTrigger;importorg.quartz.Job;importorg.quartz.JobBuilder;importorg.quartz.JobDetail;importorg.quartz.Jo
张洁:https://www.ikjzd.com/w/1663
识货:https://www.ikjzd.com/w/1745
亚马逊纳税双杀,这波卖家要被割了!:https://www.ikjzd.com/articles/22103
年增速接近200%!Lazada这个主力品类在不断爆发:https://www.ikjzd.com/articles/22106
贝索斯:要想80岁不后悔 问自己12个问题:https://www.ikjzd.com/articles/22107
来啊,互相伤害啊! 欧盟拟对美国加征215亿美元关税:https://www.ikjzd.com/articles/22109
岳让我扒她内裤 又紧又深又爽又湿又浪:http://www.30bags.com/m/a/249823.html
和新婚夫妇玩交换游戏 那一夜我让她喷了三次:http://www.30bags.com/m/a/249861.html
又一波价格战来袭,你怕了吗?:https://www.ikjzd.com/articles/146105
疫情加剧,线上需求暴涨,杭州海赢科技解读东南亚家居潜力热销商品:https://www.ikjzd.com/articles/146104
提升Amazon搜索排名的因素:https://www.ikjzd.com/articles/146124
中国跨境电商进出口行业发展数据分析:https://www.ikjzd.com/articles/146110

No comments:

Post a Comment