mirror of https://github.com/elunez/eladmin
parent
f4e9f4fd66
commit
eedc0644e2
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue