|
@@ -18,11 +18,11 @@
|
|
|
package net.yyc.quartz.util;
|
|
package net.yyc.quartz.util;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import net.yyc.common.core.util.SpringContextHolder;
|
|
import net.yyc.common.core.util.SpringContextHolder;
|
|
|
import net.yyc.quartz.constants.QuartzEnum;
|
|
import net.yyc.quartz.constants.QuartzEnum;
|
|
|
import net.yyc.quartz.entity.SysJob;
|
|
import net.yyc.quartz.entity.SysJob;
|
|
|
import net.yyc.quartz.exception.TaskException;
|
|
import net.yyc.quartz.exception.TaskException;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.ReflectionUtils;
|
|
import org.springframework.util.ReflectionUtils;
|
|
|
|
|
|
|
@@ -38,48 +38,43 @@ import java.lang.reflect.Method;
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class SpringBeanTaskInvoke implements ITaskInvok {
|
|
public class SpringBeanTaskInvoke implements ITaskInvok {
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public void invokMethod(SysJob sysJob) throws TaskException {
|
|
|
|
|
- Object target;
|
|
|
|
|
- Method method;
|
|
|
|
|
- Object returnValue;
|
|
|
|
|
- // 通过Spring上下文去找 也有可能找不到
|
|
|
|
|
- target = SpringContextHolder.getBean(sysJob.getClassName());
|
|
|
|
|
- try {
|
|
|
|
|
- if (StrUtil.isNotEmpty(sysJob.getMethodParamsValue())) {
|
|
|
|
|
- method = target.getClass().getDeclaredMethod(sysJob.getMethodName(), String.class);
|
|
|
|
|
- ReflectionUtils.makeAccessible(method);
|
|
|
|
|
- int jobId = sysJob.getJobId();
|
|
|
|
|
- String params = sysJob.getMethodParamsValue();
|
|
|
|
|
- if (jobId == 27 || jobId == 28 || jobId == 29 || jobId == 30){
|
|
|
|
|
- params = params+ "," +sysJob.getUuid();
|
|
|
|
|
- }
|
|
|
|
|
- returnValue = method.invoke(target, params);
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- method = target.getClass().getDeclaredMethod(sysJob.getMethodName());
|
|
|
|
|
- ReflectionUtils.makeAccessible(method);
|
|
|
|
|
- returnValue = method.invoke(target);
|
|
|
|
|
- }
|
|
|
|
|
- if (StrUtil.isEmpty(returnValue.toString())
|
|
|
|
|
- || QuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(returnValue.toString())) {
|
|
|
|
|
- log.error("定时任务springBeanTaskInvok异常,执行任务:{}", sysJob.getClassName());
|
|
|
|
|
- throw new TaskException("定时任务springBeanTaskInvok业务执行失败,任务:" + sysJob.getClassName());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- catch (NoSuchMethodException e) {
|
|
|
|
|
- log.error("定时任务spring bean反射异常方法未找到,执行任务:{}", sysJob.getClassName());
|
|
|
|
|
- throw new TaskException("定时任务spring bean反射异常方法未找到,执行任务:" + sysJob.getClassName());
|
|
|
|
|
- }
|
|
|
|
|
- catch (IllegalAccessException e) {
|
|
|
|
|
- log.error("定时任务spring bean反射异常,执行任务:{}", sysJob.getClassName());
|
|
|
|
|
- throw new TaskException("定时任务spring bean反射异常,执行任务:" + sysJob.getClassName());
|
|
|
|
|
- }
|
|
|
|
|
- catch (InvocationTargetException e) {
|
|
|
|
|
- log.error("定时任务spring bean反射执行异常,执行任务:{}", sysJob.getClassName());
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- throw new TaskException("定时任务spring bean反射执行异常,执行任务:" + sysJob.getClassName());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void invokMethod(SysJob sysJob) throws TaskException {
|
|
|
|
|
+ Object target;
|
|
|
|
|
+ Method method;
|
|
|
|
|
+ Object returnValue;
|
|
|
|
|
+ // 通过Spring上下文去找 也有可能找不到
|
|
|
|
|
+ target = SpringContextHolder.getBean(sysJob.getClassName());
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (StrUtil.isNotEmpty(sysJob.getMethodParamsValue())) {
|
|
|
|
|
+ method = target.getClass().getDeclaredMethod(sysJob.getMethodName(), String.class);
|
|
|
|
|
+ ReflectionUtils.makeAccessible(method);
|
|
|
|
|
+ int jobId = sysJob.getJobId();
|
|
|
|
|
+ String params = sysJob.getMethodParamsValue();
|
|
|
|
|
+ if (jobId == 27 || jobId == 28 || jobId == 29 || jobId == 30) {
|
|
|
|
|
+ params = params + "," + sysJob.getUuid();
|
|
|
|
|
+ }
|
|
|
|
|
+ returnValue = method.invoke(target, params);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ method = target.getClass().getDeclaredMethod(sysJob.getMethodName());
|
|
|
|
|
+ ReflectionUtils.makeAccessible(method);
|
|
|
|
|
+ returnValue = method.invoke(target);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StrUtil.isEmpty(returnValue.toString())
|
|
|
|
|
+ || QuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(returnValue.toString())) {
|
|
|
|
|
+ log.error("定时任务springBeanTaskInvok异常,执行任务:{}", sysJob.getClassName());
|
|
|
|
|
+ throw new TaskException("定时任务springBeanTaskInvok业务执行失败,任务:" + sysJob.getClassName());
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
|
+ log.error("[QUARTZ] Bean方法未找到 | bean={} method={} 请检查方法名和参数是否匹配", sysJob.getClassName(), sysJob.getMethodName());
|
|
|
|
|
+ throw new TaskException("Bean方法未找到, bean=" + sysJob.getClassName() + ", method=" + sysJob.getMethodName());
|
|
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
|
|
+ log.error("[QUARTZ] Bean方法无法访问 | bean={} method={}", sysJob.getClassName(), sysJob.getMethodName());
|
|
|
|
|
+ throw new TaskException("Bean方法无法访问, bean=" + sysJob.getClassName() + ", method=" + sysJob.getMethodName());
|
|
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
|
|
+ log.error("[QUARTZ] Bean方法执行异常 | bean={} method={} error={}", sysJob.getClassName(), sysJob.getMethodName(), e.getTargetException().getMessage(), e.getTargetException());
|
|
|
|
|
+ throw new TaskException("Bean方法执行异常, bean=" + sysJob.getClassName() + ", method=" + sysJob.getMethodName());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
}
|
|
}
|