优化代码

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任务类
*/
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;
}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);
}

View File

@ -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;

View File

@ -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;
}
}

View File

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

View File

@ -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")));
}
}