| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- /*
- * Copyright (c) 2018-2025, hnqz All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * Neither the name of the pig4cloud.com developer nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- * Author: hnqz
- */
- package net.yyc.quartz.util;
- import cn.hutool.core.util.StrUtil;
- import lombok.extern.slf4j.Slf4j;
- import net.yyc.common.core.util.SpringContextHolder;
- import net.yyc.quartz.constants.QuartzEnum;
- import net.yyc.quartz.entity.SysJob;
- import net.yyc.quartz.exception.TaskException;
- import org.springframework.beans.factory.NoSuchBeanDefinitionException;
- import org.springframework.stereotype.Component;
- import org.springframework.util.ReflectionUtils;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- /**
- * 定时任务spring bean反射实现
- *
- * @author 郑健楠
- */
- @Component("springBeanTaskInvoke")
- @Slf4j
- public class SpringBeanTaskInvoke implements ITaskInvoke {
- @Override
- public void invokeMethod(SysJob sysJob) throws TaskException {
- Object target;
- Method method;
- Object returnValue;
- // 通过Spring上下文去找 也有可能找不到
- try {
- target = SpringContextHolder.getBean(sysJob.getClassName());
- 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("[QUARTZ] SpringBean任务执行失败 | bean={} method={}", sysJob.getClassName(), sysJob.getMethodName());
- throw new TaskException("定时任务springBeanTaskInvoke业务执行失败,任务:" + sysJob.getClassName());
- }
- } catch (NoSuchBeanDefinitionException e) {
- log.error("[QUARTZ] Bean未找到 | bean={} 请检查类名", sysJob.getClassName(), e);
- throw new TaskException("Bean未找到, bean=" + sysJob.getClassName());
- } catch (NoSuchMethodException e) {
- log.error("[QUARTZ] Bean方法未找到 | bean={} method={} 请检查方法名和参数是否匹配", sysJob.getClassName(), sysJob.getMethodName(), e);
- throw new TaskException("Bean方法未找到, bean=" + sysJob.getClassName() + ", method=" + sysJob.getMethodName());
- } catch (IllegalAccessException e) {
- log.error("[QUARTZ] Bean方法无法访问 | bean={} method={}", sysJob.getClassName(), sysJob.getMethodName(), e);
- 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.getMessage(), e);
- throw new TaskException("Bean方法执行异常, bean=" + sysJob.getClassName() + ", method=" + sysJob.getMethodName());
- }
- }
- }
|