导读
由于项目需要,使用的是Gun企业级后台框架,对应gitee地址。可以使用一键生成代码之类的。
使用
I. 样式
页面说明
- 首页
- 侧边栏
- 代码生成
- 菜单管理
- 角色管理
- 登录日志
详细的可以自行去gitree上下载启动。
II. 定时任务配置(重点)
因为项目中使用了定时任务可视化,这里面就将其简单介绍下如何使用。
1、依赖
<!--springboot 2.0 版本以后定时任务quart -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<!-- quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
2、工具类QuartzManager
需要三个文件
- QuartzManager
- QuartzFactory
- 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;
public static void setSchedulerFactory(SchedulerFactoryBean schedulerFactory) {
QuartzManager.schedulerFactory = schedulerFactory;
}
/**
* 功能: 添加一个定时任务
*
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param jobClass 任务的类类型 eg:TimedMassJob.class
* @param cron 时间设置 表达式,参考quartz说明文档
*/
public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String cron, HashMap<String, Object> hashMap) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
// 任务名,任务组,任务执行类
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
//触发器名,触发器组
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName);
triggerBuilder.startNow();
if (!hashMap.isEmpty()) {
triggerBuilder.usingJobData("rows", hashMap.get("rows").toString());
}
// 触发器时间设定
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
// 创建Trigger对象
CronTrigger trigger = (CronTrigger) triggerBuilder.build();
// 调度容器设置JobDetail和Trigger
scheduler.scheduleJob(jobDetail, trigger);
// 启动
if (!scheduler.isShutdown()) {
scheduler.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 功能:修改一个任务的触发时间
*
* @param jobName
* @param jobGroupName
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param cron 时间设置,参考quartz说明文档
*/
public static void modifyJobTime(String jobName, String jobGroupName, String triggerName, String triggerGroupName, String cron, HashMap<String, Object> hashMap) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
if (trigger == null) {
return;
}
//自定义参数
String oldRows = "";
String newRows = "";
if (!hashMap.isEmpty()) {
oldRows = hashMap.get("oldRows").toString();
newRows = hashMap.get("rows").toString();
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(cron) || !oldRows.equalsIgnoreCase(newRows)) {
//触发器名,触发器组
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName);
triggerBuilder.startNow();
//传递参数
triggerBuilder.usingJobData("rows", hashMap.get("rows").toString());
// 设置任务触发的时间表达式
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
//创建Trigger对象
CronTrigger newTrigger = (CronTrigger) triggerBuilder.build();
scheduler.rescheduleJob(triggerKey, newTrigger);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 功能: 移除一个任务
*
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*/
public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
// 停止触发器
scheduler.pauseTrigger(triggerKey);
// 移除触发器
scheduler.unscheduleJob(triggerKey);
// 删除任务
scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 功能:启动所有定时任务
*/
public static void startJobs() {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
if (!scheduler.isStarted()) {
scheduler.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 功能:关闭所有定时任务
*/
public static void shutdownJobs() {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
if (!scheduler.isShutdown()) {
scheduler.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
```
- 自定义QuartzFactory,解决注入为null的问题