优化代码

This commit is contained in:
阿沐 2022-10-29 18:09:59 +08:00
parent 4ac9e33739
commit 7f2bac7d76
5 changed files with 68 additions and 70 deletions

View File

@ -22,13 +22,13 @@ public class ScheduleUtils {
* 获取quartz任务类 * 获取quartz任务类
*/ */
public static Class<? extends Job> getJobClass(ScheduleJobEntity scheduleJob) { public static Class<? extends Job> getJobClass(ScheduleJobEntity scheduleJob) {
if(scheduleJob.getConcurrent().equals(ScheduleConcurrentEnum.NO.getValue())){ if (scheduleJob.getConcurrent().equals(ScheduleConcurrentEnum.NO.getValue())) {
return ScheduleDisallowConcurrentExecution.class; return ScheduleDisallowConcurrentExecution.class;
}else { } else {
return ScheduleConcurrentExecution.class; return ScheduleConcurrentExecution.class;
} }
} }
/** /**
* 获取触发器key * 获取触发器key
*/ */
@ -50,7 +50,7 @@ public class ScheduleUtils {
try { try {
// job key // job key
JobKey jobKey = getJobKey(scheduleJob); JobKey jobKey = getJobKey(scheduleJob);
// 构建job信息 // 构建job信息
JobDetail jobDetail = JobBuilder.newJob(getJobClass(scheduleJob)).withIdentity(jobKey).build(); JobDetail jobDetail = JobBuilder.newJob(getJobClass(scheduleJob)).withIdentity(jobKey).build();
// 表达式调度构建器 // 表达式调度构建器
@ -63,23 +63,23 @@ public class ScheduleUtils {
// 放入参数运行时的方法可以获取 // 放入参数运行时的方法可以获取
jobDetail.getJobDataMap().put(JOB_PARAM_KEY, scheduleJob); jobDetail.getJobDataMap().put(JOB_PARAM_KEY, scheduleJob);
// 把任务添加到Quartz中
scheduler.scheduleJob(jobDetail, trigger); scheduler.scheduleJob(jobDetail, trigger);
// 判断是否存在 // 判断是否存在
if (scheduler.checkExists(jobKey)){ if (scheduler.checkExists(jobKey)) {
// 防止创建时存在数据问题先移除然后再执行创建操作 // 防止创建时存在数据问题先移除然后再执行创建操作
scheduler.deleteJob(jobKey); scheduler.deleteJob(jobKey);
} }
// 判断任务是否过期 // 判断任务是否过期
if (CronUtils.getNextExecution(scheduleJob.getCronExpression()) != null){ if (CronUtils.getNextExecution(scheduleJob.getCronExpression()) != null) {
// 执行调度任务 // 执行调度任务
scheduler.scheduleJob(jobDetail, trigger); scheduler.scheduleJob(jobDetail, trigger);
} }
// 暂停任务 // 暂停任务
if (scheduleJob.getStatus().equals(ScheduleStatusEnum.PAUSE.getValue())){ if (scheduleJob.getStatus().equals(ScheduleStatusEnum.PAUSE.getValue())) {
scheduler.pauseJob(jobKey); scheduler.pauseJob(jobKey);
} }
} catch (SchedulerException e) { } catch (SchedulerException e) {
@ -88,15 +88,14 @@ public class ScheduleUtils {
} }
/** /**
* 立即执行任务 * 立即执行任务
*/ */
public static void run(Scheduler scheduler, ScheduleJobEntity scheduleJob) { public static void run(Scheduler scheduler, ScheduleJobEntity scheduleJob) {
try { try {
// 参数 // 参数
JobDataMap dataMap = new JobDataMap(); JobDataMap dataMap = new JobDataMap();
dataMap.put(JOB_PARAM_KEY, scheduleJob); dataMap.put(JOB_PARAM_KEY, scheduleJob);
JobKey jobKey = getJobKey(scheduleJob); JobKey jobKey = getJobKey(scheduleJob);
if (scheduler.checkExists(jobKey)) { if (scheduler.checkExists(jobKey)) {
@ -141,7 +140,7 @@ public class ScheduleUtils {
if (scheduler.checkExists(jobKey)) { if (scheduler.checkExists(jobKey)) {
scheduler.deleteJob(jobKey); scheduler.deleteJob(jobKey);
} }
}catch (SchedulerException e){ } catch (SchedulerException e) {
throw new ServerException("更新定时任务失败", e); throw new ServerException("更新定时任务失败", e);
} }

View File

@ -7,14 +7,13 @@ import lombok.Getter;
* 错误编码 * 错误编码
* *
* @author 阿沐 babamu@126.com * @author 阿沐 babamu@126.com
*/ */
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public enum ErrorCode { public enum ErrorCode {
UNAUTHORIZED(401, "还未授权,不能访问"), UNAUTHORIZED(401, "还未授权,不能访问"),
FORBIDDEN(403, "没有权限,禁止访问"), FORBIDDEN(403, "没有权限,禁止访问"),
INTERNAL_SERVER_ERROR(500, "服务器异常,请稍后再试"), INTERNAL_SERVER_ERROR(500, "服务器异常,请稍后再试");
ACCOUNT_PASSWORD_ERROR(1001, "账号或密码错误");
private final int code; private final int code;
private final String msg; private final String msg;

View File

@ -11,27 +11,27 @@ import lombok.EqualsAndHashCode;
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class ServerException extends RuntimeException { public class ServerException extends RuntimeException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private int code; private int code;
private String msg; private String msg;
public ServerException(String msg) { public ServerException(String msg) {
super(msg); super(msg);
this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode(); this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode();
this.msg = msg; this.msg = msg;
} }
public ServerException(ErrorCode errorCode) { public ServerException(ErrorCode errorCode) {
super(errorCode.getMsg()); super(errorCode.getMsg());
this.code = errorCode.getCode(); this.code = errorCode.getCode();
this.msg = errorCode.getMsg(); this.msg = errorCode.getMsg();
} }
public ServerException(String msg, Throwable e) { public ServerException(String msg, Throwable e) {
super(msg, e); super(msg, e);
this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode(); this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode();
this.msg = msg; this.msg = msg;
} }
} }

View File

@ -17,35 +17,35 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j @Slf4j
@RestControllerAdvice @RestControllerAdvice
public class ServerExceptionHandler { public class ServerExceptionHandler {
/** /**
* 处理自定义异常 * 处理自定义异常
*/ */
@ExceptionHandler(ServerException.class) @ExceptionHandler(ServerException.class)
public Result<String> handleException(ServerException ex){ public Result<String> handleException(ServerException ex) {
return Result.error(ex.getCode(), ex.getMsg()); return Result.error(ex.getCode(), ex.getMsg());
} }
/** /**
* SpringMVC参数绑定Validator校验不正确 * SpringMVC参数绑定Validator校验不正确
*/ */
@ExceptionHandler(BindException.class) @ExceptionHandler(BindException.class)
public Result<String> bindException(BindException ex) { public Result<String> bindException(BindException ex) {
FieldError fieldError = ex.getFieldError(); FieldError fieldError = ex.getFieldError();
assert fieldError != null; assert fieldError != null;
return Result.error(fieldError.getDefaultMessage()); return Result.error(fieldError.getDefaultMessage());
} }
@ExceptionHandler(AccessDeniedException.class) @ExceptionHandler(AccessDeniedException.class)
public Result<String> handleAccessDeniedException(Exception ex){ public Result<String> handleAccessDeniedException(Exception ex) {
return Result.error(ErrorCode.FORBIDDEN); return Result.error(ErrorCode.FORBIDDEN);
} }
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public Result<String> handleException(Exception ex){ public Result<String> handleException(Exception ex) {
log.error(ex.getMessage(), ex); log.error(ex.getMessage(), ex);
return Result.error(ErrorCode.INTERNAL_SERVER_ERROR); return Result.error(ErrorCode.INTERNAL_SERVER_ERROR);
} }
} }

View File

@ -14,12 +14,12 @@ import org.springframework.context.annotation.Configuration;
* @author 阿沐 babamu@126.com * @author 阿沐 babamu@126.com
*/ */
@Configuration @Configuration
public class SwaggerConfig{ public class SwaggerConfig {
@Bean @Bean
public GroupedOpenApi userApi(){ public GroupedOpenApi userApi() {
String[] paths = { "/**" }; String[] paths = {"/**"};
String[] packagedToMatch = { "net.maku" }; String[] packagedToMatch = {"net.maku"};
return GroupedOpenApi.builder().group("MakuBoot") return GroupedOpenApi.builder().group("MakuBoot")
.pathsToMatch(paths) .pathsToMatch(paths)
.packagesToScan(packagedToMatch).build(); .packagesToScan(packagedToMatch).build();
@ -27,17 +27,17 @@ public class SwaggerConfig{
@Bean @Bean
public OpenAPI customOpenAPI() { public OpenAPI customOpenAPI() {
Contact contact= new Contact(); Contact contact = new Contact();
contact.setName("阿沐 babamu@126.com"); contact.setName("阿沐 babamu@126.com");
return new OpenAPI().info(new Info() return new OpenAPI().info(new Info()
.title("MakuBoot") .title("MakuBoot")
.description( "MakuBoot") .description("MakuBoot")
.contact(contact) .contact(contact)
.version("1.0") .version("2.0")
.termsOfService("https://maku.net") .termsOfService("https://maku.net")
.license(new License().name("MIT") .license(new License().name("MIT")
.url("https://maku.net"))); .url("https://maku.net")));
} }
} }