From 7f2bac7d760ca43b8d9d6b9c94a06f7ebfbf9939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E6=B2=90?= Date: Sat, 29 Oct 2022 18:09:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/net/maku/quartz/utils/ScheduleUtils.java | 25 ++++++----- .../maku/framework/common/exception/ErrorCode.java | 5 +-- .../common/exception/ServerException.java | 34 +++++++-------- .../common/exception/ServerExceptionHandler.java | 50 +++++++++++----------- .../net/maku/framework/config/SwaggerConfig.java | 24 +++++------ 5 files changed, 68 insertions(+), 70 deletions(-) diff --git a/maku-boot-module/maku-module-quartz/src/main/java/net/maku/quartz/utils/ScheduleUtils.java b/maku-boot-module/maku-module-quartz/src/main/java/net/maku/quartz/utils/ScheduleUtils.java index 31dea46..6a5a5fb 100644 --- a/maku-boot-module/maku-module-quartz/src/main/java/net/maku/quartz/utils/ScheduleUtils.java +++ b/maku-boot-module/maku-module-quartz/src/main/java/net/maku/quartz/utils/ScheduleUtils.java @@ -22,13 +22,13 @@ public class ScheduleUtils { * 获取quartz任务类 */ public static Class getJobClass(ScheduleJobEntity scheduleJob) { - if(scheduleJob.getConcurrent().equals(ScheduleConcurrentEnum.NO.getValue())){ + if (scheduleJob.getConcurrent().equals(ScheduleConcurrentEnum.NO.getValue())) { return ScheduleDisallowConcurrentExecution.class; - }else { + } else { return ScheduleConcurrentExecution.class; } } - + /** * 获取触发器key */ @@ -50,7 +50,7 @@ public class ScheduleUtils { try { // job key JobKey jobKey = getJobKey(scheduleJob); - // 构建job信息 + // 构建job信息 JobDetail jobDetail = JobBuilder.newJob(getJobClass(scheduleJob)).withIdentity(jobKey).build(); // 表达式调度构建器 @@ -63,23 +63,23 @@ public class ScheduleUtils { // 放入参数,运行时的方法可以获取 jobDetail.getJobDataMap().put(JOB_PARAM_KEY, scheduleJob); - + // 把任务添加到Quartz中 scheduler.scheduleJob(jobDetail, trigger); // 判断是否存在 - if (scheduler.checkExists(jobKey)){ + if (scheduler.checkExists(jobKey)) { // 防止创建时存在数据问题,先移除,然后再执行创建操作 scheduler.deleteJob(jobKey); } // 判断任务是否过期 - if (CronUtils.getNextExecution(scheduleJob.getCronExpression()) != null){ + if (CronUtils.getNextExecution(scheduleJob.getCronExpression()) != null) { // 执行调度任务 scheduler.scheduleJob(jobDetail, trigger); } // 暂停任务 - if (scheduleJob.getStatus().equals(ScheduleStatusEnum.PAUSE.getValue())){ + if (scheduleJob.getStatus().equals(ScheduleStatusEnum.PAUSE.getValue())) { scheduler.pauseJob(jobKey); } } catch (SchedulerException e) { @@ -88,15 +88,14 @@ public class ScheduleUtils { } - /** * 立即执行任务 */ public static void run(Scheduler scheduler, ScheduleJobEntity scheduleJob) { try { - // 参数 - JobDataMap dataMap = new JobDataMap(); - dataMap.put(JOB_PARAM_KEY, scheduleJob); + // 参数 + JobDataMap dataMap = new JobDataMap(); + dataMap.put(JOB_PARAM_KEY, scheduleJob); JobKey jobKey = getJobKey(scheduleJob); if (scheduler.checkExists(jobKey)) { @@ -141,7 +140,7 @@ public class ScheduleUtils { if (scheduler.checkExists(jobKey)) { scheduler.deleteJob(jobKey); } - }catch (SchedulerException e){ + } catch (SchedulerException e) { throw new ServerException("更新定时任务失败", e); } diff --git a/maku-framework/src/main/java/net/maku/framework/common/exception/ErrorCode.java b/maku-framework/src/main/java/net/maku/framework/common/exception/ErrorCode.java index af25107..7c01559 100644 --- a/maku-framework/src/main/java/net/maku/framework/common/exception/ErrorCode.java +++ b/maku-framework/src/main/java/net/maku/framework/common/exception/ErrorCode.java @@ -7,14 +7,13 @@ import lombok.Getter; * 错误编码 * * @author 阿沐 babamu@126.com -*/ + */ @Getter @AllArgsConstructor public enum ErrorCode { UNAUTHORIZED(401, "还未授权,不能访问"), FORBIDDEN(403, "没有权限,禁止访问"), - INTERNAL_SERVER_ERROR(500, "服务器异常,请稍后再试"), - ACCOUNT_PASSWORD_ERROR(1001, "账号或密码错误"); + INTERNAL_SERVER_ERROR(500, "服务器异常,请稍后再试"); private final int code; private final String msg; diff --git a/maku-framework/src/main/java/net/maku/framework/common/exception/ServerException.java b/maku-framework/src/main/java/net/maku/framework/common/exception/ServerException.java index f6c92e3..e3380a1 100644 --- a/maku-framework/src/main/java/net/maku/framework/common/exception/ServerException.java +++ b/maku-framework/src/main/java/net/maku/framework/common/exception/ServerException.java @@ -11,27 +11,27 @@ import lombok.EqualsAndHashCode; @Data @EqualsAndHashCode(callSuper = true) public class ServerException extends RuntimeException { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; private int code; - private String msg; + private String msg; - public ServerException(String msg) { - super(msg); - this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode(); - this.msg = msg; - } + public ServerException(String msg) { + super(msg); + this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode(); + this.msg = msg; + } - public ServerException(ErrorCode errorCode) { - super(errorCode.getMsg()); - this.code = errorCode.getCode(); - this.msg = errorCode.getMsg(); - } + public ServerException(ErrorCode errorCode) { + super(errorCode.getMsg()); + this.code = errorCode.getCode(); + this.msg = errorCode.getMsg(); + } - public ServerException(String msg, Throwable e) { - super(msg, e); - this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode(); - this.msg = msg; - } + public ServerException(String msg, Throwable e) { + super(msg, e); + this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode(); + this.msg = msg; + } } \ No newline at end of file diff --git a/maku-framework/src/main/java/net/maku/framework/common/exception/ServerExceptionHandler.java b/maku-framework/src/main/java/net/maku/framework/common/exception/ServerExceptionHandler.java index fbcf5e5..ccbadb2 100644 --- a/maku-framework/src/main/java/net/maku/framework/common/exception/ServerExceptionHandler.java +++ b/maku-framework/src/main/java/net/maku/framework/common/exception/ServerExceptionHandler.java @@ -17,35 +17,35 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; @Slf4j @RestControllerAdvice public class ServerExceptionHandler { - /** - * 处理自定义异常 - */ - @ExceptionHandler(ServerException.class) - public Result handleException(ServerException ex){ + /** + * 处理自定义异常 + */ + @ExceptionHandler(ServerException.class) + public Result handleException(ServerException ex) { - return Result.error(ex.getCode(), ex.getMsg()); - } + return Result.error(ex.getCode(), ex.getMsg()); + } - /** - * SpringMVC参数绑定,Validator校验不正确 - */ - @ExceptionHandler(BindException.class) - public Result bindException(BindException ex) { - FieldError fieldError = ex.getFieldError(); - assert fieldError != null; - return Result.error(fieldError.getDefaultMessage()); - } + /** + * SpringMVC参数绑定,Validator校验不正确 + */ + @ExceptionHandler(BindException.class) + public Result bindException(BindException ex) { + FieldError fieldError = ex.getFieldError(); + assert fieldError != null; + return Result.error(fieldError.getDefaultMessage()); + } - @ExceptionHandler(AccessDeniedException.class) - public Result handleAccessDeniedException(Exception ex){ + @ExceptionHandler(AccessDeniedException.class) + public Result handleAccessDeniedException(Exception ex) { - return Result.error(ErrorCode.FORBIDDEN); - } + return Result.error(ErrorCode.FORBIDDEN); + } - @ExceptionHandler(Exception.class) - public Result handleException(Exception ex){ - log.error(ex.getMessage(), ex); - return Result.error(ErrorCode.INTERNAL_SERVER_ERROR); - } + @ExceptionHandler(Exception.class) + public Result handleException(Exception ex) { + log.error(ex.getMessage(), ex); + return Result.error(ErrorCode.INTERNAL_SERVER_ERROR); + } } \ No newline at end of file diff --git a/maku-framework/src/main/java/net/maku/framework/config/SwaggerConfig.java b/maku-framework/src/main/java/net/maku/framework/config/SwaggerConfig.java index e82832e..d9d451d 100644 --- a/maku-framework/src/main/java/net/maku/framework/config/SwaggerConfig.java +++ b/maku-framework/src/main/java/net/maku/framework/config/SwaggerConfig.java @@ -14,12 +14,12 @@ import org.springframework.context.annotation.Configuration; * @author 阿沐 babamu@126.com */ @Configuration -public class SwaggerConfig{ +public class SwaggerConfig { @Bean - public GroupedOpenApi userApi(){ - String[] paths = { "/**" }; - String[] packagedToMatch = { "net.maku" }; + public GroupedOpenApi userApi() { + String[] paths = {"/**"}; + String[] packagedToMatch = {"net.maku"}; return GroupedOpenApi.builder().group("MakuBoot") .pathsToMatch(paths) .packagesToScan(packagedToMatch).build(); @@ -27,17 +27,17 @@ public class SwaggerConfig{ @Bean public OpenAPI customOpenAPI() { - Contact contact= new Contact(); + Contact contact = new Contact(); contact.setName("阿沐 babamu@126.com"); return new OpenAPI().info(new Info() - .title("MakuBoot") - .description( "MakuBoot") - .contact(contact) - .version("1.0") - .termsOfService("https://maku.net") - .license(new License().name("MIT") - .url("https://maku.net"))); + .title("MakuBoot") + .description("MakuBoot") + .contact(contact) + .version("2.0") + .termsOfService("https://maku.net") + .license(new License().name("MIT") + .url("https://maku.net"))); } } \ No newline at end of file