From eedc0644e208fa65d173bfc6d50775577fbff4b3 Mon Sep 17 00:00:00 2001 From: "Emil.Zhang" Date: Thu, 24 Mar 2022 17:54:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=8A=93=E5=8F=96):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 新增对定时任务业务逻辑处理中,可记录错误的记录 --- .../me/zhengjie/exception/TaskException.java | 36 +++++++++++++++++++ .../config/thread/AsyncTaskExecutePool.java | 20 +++++++++++ .../quartz/service/QuartzLogService.java | 19 ++++++++++ .../service/impl/QuartzLogServiceImpl.java | 23 ++++++++++++ .../modules/quartz/task/TestTask.java | 15 ++++++++ 5 files changed, 113 insertions(+) create mode 100644 eladmin-common/src/main/java/me/zhengjie/exception/TaskException.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/QuartzLogService.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/impl/QuartzLogServiceImpl.java diff --git a/eladmin-common/src/main/java/me/zhengjie/exception/TaskException.java b/eladmin-common/src/main/java/me/zhengjie/exception/TaskException.java new file mode 100644 index 00000000..64ae2518 --- /dev/null +++ b/eladmin-common/src/main/java/me/zhengjie/exception/TaskException.java @@ -0,0 +1,36 @@ +package me.zhengjie.exception; + +import lombok.Getter; + +/** + * 异步任务返回错误,用于定时任务执行时返回可记录到日志的错误 + * 方便通过页面上的日志查看功能查看具体原因 + * + * @author Emil.Zhang + * 2022-03-24 + */ +@Getter +public class TaskException extends RuntimeException { + + private String param; + + /** + * 带基本信息的异步错误 + * + * @param msg 错误信息 + */ + public TaskException(String msg) { + super(msg); + } + + /** + * 含有额外参数的异步错误 + * + * @param msg 错误信息 + * @param param 接口调用参数 + */ + public TaskException(String msg, String param) { + super(msg); + this.param = param; + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/config/thread/AsyncTaskExecutePool.java b/eladmin-system/src/main/java/me/zhengjie/config/thread/AsyncTaskExecutePool.java index 133cf55f..bf6bb1d9 100644 --- a/eladmin-system/src/main/java/me/zhengjie/config/thread/AsyncTaskExecutePool.java +++ b/eladmin-system/src/main/java/me/zhengjie/config/thread/AsyncTaskExecutePool.java @@ -15,7 +15,12 @@ */ package me.zhengjie.config.thread; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import me.zhengjie.exception.TaskException; +import me.zhengjie.modules.quartz.domain.QuartzLog; +import me.zhengjie.modules.quartz.service.QuartzLogService; +import me.zhengjie.utils.ThrowableUtil; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; @@ -30,8 +35,11 @@ import java.util.concurrent.ThreadPoolExecutor; */ @Slf4j @Configuration +@RequiredArgsConstructor public class AsyncTaskExecutePool implements AsyncConfigurer { + private final QuartzLogService quartzLogService; + @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); @@ -57,6 +65,18 @@ public class AsyncTaskExecutePool implements AsyncConfigurer { return (throwable, method, objects) -> { log.error("===="+throwable.getMessage()+"====", throwable); log.error("exception method:"+method.getName()); + // 针对返回了异步错误的,额外记录到定时任务日志中 + if (throwable instanceof TaskException) { + QuartzLog quartzLog = new QuartzLog(); + quartzLog.setBeanName(method.getDeclaringClass().getSimpleName()); + quartzLog.setMethodName(method.getName()); + quartzLog.setExceptionDetail(ThrowableUtil.getStackTrace(throwable)); + quartzLog.setParams(((TaskException) throwable).getParam()); + quartzLog.setIsSuccess(false); + quartzLog.setTime(0L); + + quartzLogService.create(quartzLog); + } }; } } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/QuartzLogService.java b/eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/QuartzLogService.java new file mode 100644 index 00000000..5dc8fb53 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/QuartzLogService.java @@ -0,0 +1,19 @@ +package me.zhengjie.modules.quartz.service; + +import me.zhengjie.modules.quartz.domain.QuartzLog; + +/** + * 定时任务日志业务层 + * + * @author Emil.Zhang + * 2022-03-24 + */ +public interface QuartzLogService { + + /** + * 创建 + * + * @param resources / + */ + void create(QuartzLog resources); +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/impl/QuartzLogServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/impl/QuartzLogServiceImpl.java new file mode 100644 index 00000000..4e6ff99f --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/impl/QuartzLogServiceImpl.java @@ -0,0 +1,23 @@ +package me.zhengjie.modules.quartz.service.impl; + +import lombok.RequiredArgsConstructor; +import me.zhengjie.modules.quartz.domain.QuartzLog; +import me.zhengjie.modules.quartz.repository.QuartzLogRepository; +import me.zhengjie.modules.quartz.service.QuartzLogService; +import org.springframework.stereotype.Service; + +/** + * @author Emil.Zhang + * 2022-03-24 + */ +@RequiredArgsConstructor +@Service(value = "quartzLogService") +public class QuartzLogServiceImpl implements QuartzLogService { + + private final QuartzLogRepository quartzLogRepository; + + @Override + public void create(QuartzLog resources) { + quartzLogRepository.save(resources); + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/quartz/task/TestTask.java b/eladmin-system/src/main/java/me/zhengjie/modules/quartz/task/TestTask.java index aff0ac86..3a109524 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/quartz/task/TestTask.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/quartz/task/TestTask.java @@ -16,6 +16,7 @@ package me.zhengjie.modules.quartz.task; import lombok.extern.slf4j.Slf4j; +import me.zhengjie.exception.TaskException; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @@ -27,17 +28,31 @@ import org.springframework.stereotype.Component; @Slf4j @Async @Component +@SuppressWarnings({"unused"}) public class TestTask { + @SuppressWarnings({"unused"}) public void run(){ log.info("run 执行成功"); } + @SuppressWarnings({"unused"}) public void run1(String str){ log.info("run1 执行成功,参数为: {}" + str); } + @SuppressWarnings({"unused"}) public void run2(){ log.info("run2 执行成功"); } + + @SuppressWarnings({"unused"}) + public void runWithException() { + throw new TaskException("返回一个测试错误"); + } + + @SuppressWarnings({"unused"}) + public void runWithException(String param) { + throw new TaskException("返回一个带参数的测试错误", param); + } }