导读


由于项目需要,使用的是Gun企业级后台框架,对应gitee地址。可以使用一键生成代码之类的。

使用


I. 样式

页面说明

  • 首页

image.png

  • 侧边栏

image.png

  • 代码生成

image.png

  • 菜单管理

image.png

  • 角色管理

image.png

  • 登录日志

image.png
详细的可以自行去gitree上下载启动。

II. 定时任务配置(重点)

因为项目中使用了定时任务可视化,这里面就将其简单介绍下如何使用。

1、依赖

  1. <!--springboot 2.0 版本以后定时任务quart -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-quartz</artifactId>
  5. </dependency>
  6. <!-- quartz -->
  7. <dependency>
  8. <groupId>org.quartz-scheduler</groupId>
  9. <artifactId>quartz</artifactId>
  10. <version>2.2.3</version>
  11. </dependency>

2、工具类QuartzManager

需要三个文件

  1. QuartzManager
  2. QuartzFactory
  3. QuartzConfig
  • QuartzManager工具类 ```java package cn.common.project.core.scheduleTask;

import org.quartz.*; import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.HashMap; import java.util.List;

public class QuartzManager { private static SchedulerFactoryBean schedulerFactory;

  1. public static void setSchedulerFactory(SchedulerFactoryBean schedulerFactory) {
  2. QuartzManager.schedulerFactory = schedulerFactory;
  3. }
  4. /**
  5. * 功能: 添加一个定时任务
  6. *
  7. * @param jobName 任务名
  8. * @param jobGroupName 任务组名
  9. * @param triggerName 触发器名
  10. * @param triggerGroupName 触发器组名
  11. * @param jobClass 任务的类类型 eg:TimedMassJob.class
  12. * @param cron 时间设置 表达式,参考quartz说明文档
  13. */
  14. public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String cron, HashMap<String, Object> hashMap) {
  15. try {
  16. Scheduler scheduler = schedulerFactory.getScheduler();
  17. // 任务名,任务组,任务执行类
  18. JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
  19. //触发器名,触发器组
  20. TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName);
  21. triggerBuilder.startNow();
  22. if (!hashMap.isEmpty()) {
  23. triggerBuilder.usingJobData("rows", hashMap.get("rows").toString());
  24. }
  25. // 触发器时间设定
  26. triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
  27. // 创建Trigger对象
  28. CronTrigger trigger = (CronTrigger) triggerBuilder.build();
  29. // 调度容器设置JobDetail和Trigger
  30. scheduler.scheduleJob(jobDetail, trigger);
  31. // 启动
  32. if (!scheduler.isShutdown()) {
  33. scheduler.start();
  34. }
  35. } catch (Exception e) {
  36. throw new RuntimeException(e);
  37. }
  38. }
  39. /**
  40. * 功能:修改一个任务的触发时间
  41. *
  42. * @param jobName
  43. * @param jobGroupName
  44. * @param triggerName 触发器名
  45. * @param triggerGroupName 触发器组名
  46. * @param cron 时间设置,参考quartz说明文档
  47. */
  48. public static void modifyJobTime(String jobName, String jobGroupName, String triggerName, String triggerGroupName, String cron, HashMap<String, Object> hashMap) {
  49. try {
  50. Scheduler scheduler = schedulerFactory.getScheduler();
  51. TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
  52. CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
  53. if (trigger == null) {
  54. return;
  55. }
  56. //自定义参数
  57. String oldRows = "";
  58. String newRows = "";
  59. if (!hashMap.isEmpty()) {
  60. oldRows = hashMap.get("oldRows").toString();
  61. newRows = hashMap.get("rows").toString();
  62. }
  63. String oldTime = trigger.getCronExpression();
  64. if (!oldTime.equalsIgnoreCase(cron) || !oldRows.equalsIgnoreCase(newRows)) {
  65. //触发器名,触发器组
  66. TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName);
  67. triggerBuilder.startNow();
  68. //传递参数
  69. triggerBuilder.usingJobData("rows", hashMap.get("rows").toString());
  70. // 设置任务触发的时间表达式
  71. triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
  72. //创建Trigger对象
  73. CronTrigger newTrigger = (CronTrigger) triggerBuilder.build();
  74. scheduler.rescheduleJob(triggerKey, newTrigger);
  75. }
  76. } catch (Exception e) {
  77. throw new RuntimeException(e);
  78. }
  79. }
  80. /**
  81. * 功能: 移除一个任务
  82. *
  83. * @param jobName
  84. * @param jobGroupName
  85. * @param triggerName
  86. * @param triggerGroupName
  87. */
  88. public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
  89. try {
  90. Scheduler scheduler = schedulerFactory.getScheduler();
  91. TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
  92. // 停止触发器
  93. scheduler.pauseTrigger(triggerKey);
  94. // 移除触发器
  95. scheduler.unscheduleJob(triggerKey);
  96. // 删除任务
  97. scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));
  98. } catch (Exception e) {
  99. throw new RuntimeException(e);
  100. }
  101. }
  102. /**
  103. * 功能:启动所有定时任务
  104. */
  105. public static void startJobs() {
  106. try {
  107. Scheduler scheduler = schedulerFactory.getScheduler();
  108. if (!scheduler.isStarted()) {
  109. scheduler.start();
  110. }
  111. } catch (Exception e) {
  112. throw new RuntimeException(e);
  113. }
  114. }
  115. /**
  116. * 功能:关闭所有定时任务
  117. */
  118. public static void shutdownJobs() {
  119. try {
  120. Scheduler scheduler = schedulerFactory.getScheduler();
  121. if (!scheduler.isShutdown()) {
  122. scheduler.shutdown();
  123. }
  124. } catch (Exception e) {
  125. throw new RuntimeException(e);
  126. }
  127. }

}

```

  • 自定义QuartzFactory,解决注入为null的问题