优化代码

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,9 +22,9 @@ 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;
} }
} }
@ -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,7 +88,6 @@ public class ScheduleUtils {
} }
/** /**
* 立即执行任务 * 立即执行任务
*/ */
@ -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

@ -21,7 +21,7 @@ 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());
} }
@ -37,13 +37,13 @@ public class ServerExceptionHandler {
} }
@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,14 +27,14 @@ 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")));