SpringBeanTaskInvoke.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2018-2025, hnqz All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the pig4cloud.com developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: hnqz
  16. */
  17. package net.yyc.quartz.util;
  18. import cn.hutool.core.util.StrUtil;
  19. import lombok.extern.slf4j.Slf4j;
  20. import net.yyc.common.core.util.SpringContextHolder;
  21. import net.yyc.quartz.constants.QuartzEnum;
  22. import net.yyc.quartz.entity.SysJob;
  23. import net.yyc.quartz.exception.TaskException;
  24. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  25. import org.springframework.stereotype.Component;
  26. import org.springframework.util.ReflectionUtils;
  27. import java.lang.reflect.InvocationTargetException;
  28. import java.lang.reflect.Method;
  29. /**
  30. * 定时任务spring bean反射实现
  31. *
  32. * @author 郑健楠
  33. */
  34. @Component("springBeanTaskInvoke")
  35. @Slf4j
  36. public class SpringBeanTaskInvoke implements ITaskInvoke {
  37. @Override
  38. public void invokeMethod(SysJob sysJob) throws TaskException {
  39. Object target;
  40. Method method;
  41. Object returnValue;
  42. // 通过Spring上下文去找 也有可能找不到
  43. try {
  44. target = SpringContextHolder.getBean(sysJob.getClassName());
  45. if (StrUtil.isNotEmpty(sysJob.getMethodParamsValue())) {
  46. method = target.getClass().getDeclaredMethod(sysJob.getMethodName(), String.class);
  47. ReflectionUtils.makeAccessible(method);
  48. int jobId = sysJob.getJobId();
  49. String params = sysJob.getMethodParamsValue();
  50. if (jobId == 27 || jobId == 28 || jobId == 29 || jobId == 30) {
  51. params = params + "," + sysJob.getUuid();
  52. }
  53. returnValue = method.invoke(target, params);
  54. } else {
  55. method = target.getClass().getDeclaredMethod(sysJob.getMethodName());
  56. ReflectionUtils.makeAccessible(method);
  57. returnValue = method.invoke(target);
  58. }
  59. if (StrUtil.isEmpty(returnValue.toString())
  60. || QuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(returnValue.toString())) {
  61. log.error("[QUARTZ] SpringBean任务执行失败 | bean={} method={}", sysJob.getClassName(), sysJob.getMethodName());
  62. throw new TaskException("定时任务springBeanTaskInvoke业务执行失败,任务:" + sysJob.getClassName());
  63. }
  64. } catch (NoSuchBeanDefinitionException e) {
  65. log.error("[QUARTZ] Bean未找到 | bean={} 请检查类名", sysJob.getClassName(), e);
  66. throw new TaskException("Bean未找到, bean=" + sysJob.getClassName());
  67. } catch (NoSuchMethodException e) {
  68. log.error("[QUARTZ] Bean方法未找到 | bean={} method={} 请检查方法名和参数是否匹配", sysJob.getClassName(), sysJob.getMethodName(), e);
  69. throw new TaskException("Bean方法未找到, bean=" + sysJob.getClassName() + ", method=" + sysJob.getMethodName());
  70. } catch (IllegalAccessException e) {
  71. log.error("[QUARTZ] Bean方法无法访问 | bean={} method={}", sysJob.getClassName(), sysJob.getMethodName(), e);
  72. throw new TaskException("Bean方法无法访问, bean=" + sysJob.getClassName() + ", method=" + sysJob.getMethodName());
  73. } catch (InvocationTargetException e) {
  74. log.error("[QUARTZ] Bean方法执行异常 | bean={} method={} error={}", sysJob.getClassName(), sysJob.getMethodName(), e.getMessage(), e);
  75. throw new TaskException("Bean方法执行异常, bean=" + sysJob.getClassName() + ", method=" + sysJob.getMethodName());
  76. }
  77. }
  78. }