您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“SpringBoot如何實現quartz定時任務可視化管理功能”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“SpringBoot如何實現quartz定時任務可視化管理功能”這篇文章吧。
在實際框架或產品開發過程中,springboot中集成quarzt方式基本是以job和trigger的bean對象方式直接硬編碼完成的,例如以下代碼示例。對于系統內定義的所有定時任務類型,具體執行類,執行策略,運行狀態都沒有一個動態全局的管理,所有決定將quartz做成可視化配置管理,便于統一管理,也降低了使用門檻,只需要關心job類的實現即可
@Bean public JobDetail SMSJobDetail() { return JobBuilder.newJob(SMSJob.class).withIdentity("SMSJob").storeDurably().build(); } // 把jobDetail注冊到trigger上去 @Bean public Trigger myJobTrigger() { SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(1).repeatForever(); return TriggerBuilder.newTrigger() .forJob(SMSJobDetail()) .withIdentity("myJobTrigger") .withSchedule(scheduleBuilder) .build(); }
用于存儲quartz配置
DROP TABLE IF EXISTS `f_quartztask`; CREATE TABLE `f_quartztask` ( `TaskID` varchar(50) NOT NULL, `TaskName` varchar(200) DEFAULT NULL, `TaskType` int(11) DEFAULT NULL, `TaskTag` varchar(100) DEFAULT NULL, `JobClassPath` varchar(200) DEFAULT NULL, `ExecutePeroid` int(11) DEFAULT NULL, `ExecuteUnit` int(11) DEFAULT NULL, `CornExpress` varchar(200) DEFAULT NULL, `Enviroment` varchar(50) DEFAULT NULL, `TaskStatus` int(11) DEFAULT NULL, `SortNum` int(11) DEFAULT NULL, `Remark` varchar(500) DEFAULT NULL, PRIMARY KEY (`TaskID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
public class QuartzEnum { public enum TaskType implements IConvertEnumToCodeItem { Cycle(10, "循環任務"), Corn(20, "Corn表達式任務"); private int _value; private String _name; private TaskType(int value, String name) { set_value(value); set_name((name)); } public int get_value() { return _value; } public void set_value(int _value) { this._value = _value; } public String get_name() { return _name; } public void set_name(String _name) { this._name = _name; } @Override public String toString() { return _name; } @Override public String getCodeName() { return "Quartz任務類別"; } } public enum ExecuteUnit implements IConvertEnumToCodeItem { Second(10, "秒"), Minute(20, "分"), Hour(30, "時"); private int _value; private String _name; private ExecuteUnit(int value, String name) { set_value(value); set_name((name)); } public int get_value() { return _value; } public void set_value(int _value) { this._value = _value; } public String get_name() { return _name; } public void set_name(String _name) { this._name = _name; } @Override public String toString() { return _name; } @Override public String getCodeName() { return "Quartz間隔單位"; } } public enum TaskStatus implements IConvertEnumToCodeItem { Open(10, "開啟"), Close(20, "關閉"); private int _value; private String _name; private TaskStatus(int value, String name) { set_value(value); set_name((name)); } public int get_value() { return _value; } public void set_value(int _value) { this._value = _value; } public String get_name() { return _name; } public void set_name(String _name) { this._name = _name; } @Override public String toString() { return _name; } @Override public String getCodeName() { return "Quartz任務狀態"; } } public enum TaskEnviroment implements IConvertEnumToCodeItem { All("全部", "全部"), Dev("dev", "開發環境"), Pro("pro", "正式環境"); private String _value; private String _name; private TaskEnviroment(String value, String name) { set_value(value); set_name((name)); } public String get_value() { return _value; } public void set_value(String _value) { this._value = _value; } public String get_name() { return _name; } public void set_name(String _name) { this._name = _name; } @Override public String toString() { return _name; } @Override public String getCodeName() { return "Quartz任務執行環境"; } } }
支持Job類注bean入對象
@Component public class QuartzFactory extends AdaptableJobFactory { @Autowired private AutowireCapableBeanFactory capableBeanFactory; @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { // 調用父類的方法 Object jobInstance = super.createJobInstance(bundle); // 進行注入 capableBeanFactory.autowireBean(jobInstance); return jobInstance; } }
注入QuartzFactory對象
@Configuration public class QuartzConfig { @Autowired private QuartzFactory quartzFactory; @Bean public SchedulerFactoryBean schedulerFactoryBean(){ SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); schedulerFactoryBean.setJobFactory(quartzFactory); //將job實例化,能夠操作進行Spring 注入 return schedulerFactoryBean; } }
定時任務動態添加/刪除操作類,initQuartzTask方法在系統啟動時執行,根據配置自動開啟相關符合條件的任務
@Component public class QuartzUtil { @Autowired private SchedulerFactoryBean schedulerFactoryBean; @Autowired private F_QuartzTaskService quartzTaskService; @Value("${spring.profiles.active}") private String active; private static String JOB_GROUP_NAME = "DEFAULT_JOB_GROUP_NAME"; private static String TRIGGER_GROUP_NAME = "DEFAULT_TRIGGER_GROUP_NAME"; public void initQuartzTask() { List<F_QuartzTaskDO> openTaskList = quartzTaskService.selectAllList(); if(openTaskList.size()>0){ openTaskList = openTaskList.stream().filter(a -> a.getTaskStatus() == QuartzEnum.TaskStatus.Open.get_value() && (a.getEnviroment().equals(QuartzEnum.TaskEnviroment.All.get_name()) || a.getEnviroment().equals(active))).collect(Collectors.toList()); } for (F_QuartzTaskDO taskDO : openTaskList) { try { Class<Job> jobClass = (Class<Job>) Class.forName(taskDO.getJobClassPath()); if (taskDO.getTaskType() == QuartzEnum.TaskType.Cycle.get_value()) { addIntervalJob(taskDO.getTaskTag(), jobClass, taskDO.getExecutePeroid(), taskDO.getExecuteUnit()); } else { addCornJob(taskDO.getTaskTag(), jobClass, taskDO.getCornExpress()); } } catch (Exception e) { e.printStackTrace(); } } if (openTaskList.size() > 0) { System.out.println("掃描并初始化開啟quartz定時任務成功,任務數量:" + openTaskList.size() + "個"); } } public void startTask( F_QuartzTaskDO taskDO){ try { Class<Job> jobClass = (Class<Job>) Class.forName(taskDO.getJobClassPath()); if (taskDO.getTaskType() == QuartzEnum.TaskType.Cycle.get_value()) { addIntervalJob(taskDO.getTaskTag(), jobClass, taskDO.getExecutePeroid(), taskDO.getExecuteUnit()); } else { addCornJob(taskDO.getTaskTag(), jobClass, taskDO.getCornExpress()); } } catch (Exception e) { e.printStackTrace(); } } //增加定時任務任務 public void addIntervalJob(String jobName, Class<? extends Job> cls, int peroid, int timeUnit) { try { SimpleScheduleBuilder scheduleBuilder = null; if (timeUnit == QuartzEnum.ExecuteUnit.Second.get_value()) { scheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(peroid).repeatForever(); } else if (timeUnit == QuartzEnum.ExecuteUnit.Minute.get_value()) { scheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInMinutes(peroid).repeatForever(); } else if (timeUnit == QuartzEnum.ExecuteUnit.Hour.get_value()) { scheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInHours(peroid).repeatForever(); } Scheduler sched = schedulerFactoryBean.getScheduler(); JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME).storeDurably().build(); Trigger trigger = TriggerBuilder.newTrigger().forJob(jobDetail).withIdentity(jobName, TRIGGER_GROUP_NAME).withSchedule(scheduleBuilder).build(); sched.scheduleJob(jobDetail, trigger); if (!sched.isShutdown()) { sched.start(); // 啟動 } } catch (Exception e) { throw new RuntimeException(e); } } //增加corn表達式任務 public void addCornJob(String jobName, Class<? extends Job> cls, String cornExpress) { try { Scheduler sched = schedulerFactoryBean.getScheduler(); JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME).build(); CronTrigger trigger = (CronTrigger) TriggerBuilder .newTrigger() .withIdentity(jobName, TRIGGER_GROUP_NAME) .withSchedule(CronScheduleBuilder.cronSchedule(cornExpress)) .build(); sched.scheduleJob(jobDetail, trigger); if (!sched.isShutdown()) { sched.start(); // 啟動 } } catch (Exception e) { throw new RuntimeException(e); } } //停止任務 public void deleteJob(String jobName) { try { Scheduler sched = schedulerFactoryBean.getScheduler(); TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME); JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME); sched.pauseTrigger(triggerKey); // 停止觸發器 sched.unscheduleJob(triggerKey);// 移除觸發器 sched.deleteJob(jobKey); // 刪除任務 } catch (Exception e) { throw new RuntimeException(e); } } }
界面展示
以上是“SpringBoot如何實現quartz定時任務可視化管理功能”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。