feat(定时任务错误抓取):

1. 新增对定时任务业务逻辑处理中,可记录错误的记录
pull/725/head
Emil.Zhang 2022-03-24 17:54:53 +08:00
parent f4e9f4fd66
commit eedc0644e2
5 changed files with 113 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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